2017-10-28 14:31:37 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-03-13 14:21:23 +01:00
|
|
|
#define LIBSSH_STATIC
|
|
|
|
|
2011-01-01 20:20:42 +01:00
|
|
|
#ifndef _WIN32
|
2011-01-11 17:48:48 +01:00
|
|
|
#define _POSIX_PTHREAD_SEMANTICS
|
2011-01-01 20:20:42 +01:00
|
|
|
# include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
2010-03-13 14:21:23 +01:00
|
|
|
#include "torture.h"
|
2018-01-09 21:30:50 +01:00
|
|
|
#include "torture_key.h"
|
2011-01-01 20:20:42 +01:00
|
|
|
#include <libssh/session.h>
|
|
|
|
#include <libssh/misc.h>
|
2018-08-07 11:34:30 +02:00
|
|
|
#include <libssh/pki_priv.h>
|
2018-11-23 16:38:54 +01:00
|
|
|
#include <libssh/options.h>
|
2019-02-22 17:10:38 +01:00
|
|
|
#ifdef WITH_SERVER
|
|
|
|
#include <libssh/bind.h>
|
2019-03-07 17:00:44 +01:00
|
|
|
#define LIBSSH_CUSTOM_BIND_CONFIG_FILE "my_bind_config"
|
2019-02-22 17:10:38 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DSA
|
|
|
|
#define LIBSSH_DSA_TESTKEY "libssh_testkey.id_dsa"
|
|
|
|
#endif
|
|
|
|
#define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa"
|
|
|
|
#define LIBSSH_ED25519_TESTKEY "libssh_testkey.id_ed25519"
|
|
|
|
#ifdef HAVE_ECC
|
|
|
|
#define LIBSSH_ECDSA_521_TESTKEY "libssh_testkey.id_ecdsa521"
|
|
|
|
#endif
|
2010-03-13 14:21:23 +01:00
|
|
|
|
2015-09-07 10:39:51 +02:00
|
|
|
static int setup(void **state)
|
|
|
|
{
|
2018-09-04 15:29:01 +02:00
|
|
|
ssh_session session;
|
|
|
|
int verbosity;
|
|
|
|
|
|
|
|
session = ssh_new();
|
|
|
|
|
|
|
|
verbosity = torture_libssh_verbosity();
|
|
|
|
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
|
|
|
|
|
2019-05-16 17:07:57 +02:00
|
|
|
session->client = 1;
|
|
|
|
|
2010-12-28 15:05:56 +01:00
|
|
|
*state = session;
|
2015-09-07 10:39:51 +02:00
|
|
|
|
|
|
|
return 0;
|
2010-06-03 16:44:47 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 10:39:51 +02:00
|
|
|
static int teardown(void **state)
|
|
|
|
{
|
2010-12-28 15:05:56 +01:00
|
|
|
ssh_free(*state);
|
2015-09-07 10:39:51 +02:00
|
|
|
|
|
|
|
return 0;
|
2010-03-13 14:21:23 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:56 +01:00
|
|
|
static void torture_options_set_host(void **state) {
|
|
|
|
ssh_session session = *state;
|
2010-03-13 14:21:23 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.host, "localhost");
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HOST, "guru@meditation");
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.host, "meditation");
|
|
|
|
assert_string_equal(session->opts.username, "guru");
|
2010-03-13 14:21:23 +01:00
|
|
|
}
|
|
|
|
|
2017-08-24 15:33:11 +02:00
|
|
|
static void torture_options_set_ciphers(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Test known ciphers */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, "aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S], "aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
|
|
|
|
/* Test one unknown cipher */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, "aes128-ctr,unknown-crap@example.com,aes192-ctr,aes256-ctr");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S], "aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
|
|
|
|
/* Test all unknown ciphers */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, "unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_false(rc == 0);
|
|
|
|
}
|
|
|
|
|
2017-10-23 14:53:55 +02:00
|
|
|
static void torture_options_set_key_exchange(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Test known kexes */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_KEY_EXCHANGE,
|
2018-09-20 10:39:13 +02:00
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
2017-10-23 14:53:55 +02:00
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_KEX],
|
2018-09-20 10:39:13 +02:00
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
2017-10-23 14:53:55 +02:00
|
|
|
|
|
|
|
/* Test one unknown kex */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_KEY_EXCHANGE,
|
2018-09-20 10:39:13 +02:00
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,unknown-crap@example.com,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
2017-10-23 14:53:55 +02:00
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_KEX],
|
2018-09-20 10:39:13 +02:00
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
2017-10-23 14:53:55 +02:00
|
|
|
|
|
|
|
/* Test all unknown kexes */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_KEY_EXCHANGE,
|
|
|
|
"unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_false(rc == 0);
|
|
|
|
}
|
|
|
|
|
2017-11-13 12:03:52 +01:00
|
|
|
static void torture_options_set_hostkey(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Test known host keys */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_HOSTKEYS,
|
|
|
|
"ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
|
|
|
|
"ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
|
|
|
|
|
2018-07-02 14:34:16 +02:00
|
|
|
/* Test one unknown host key */
|
2017-11-13 12:03:52 +01:00
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_HOSTKEYS,
|
|
|
|
"ssh-ed25519,unknown-crap@example.com,ssh-rsa");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS],
|
|
|
|
"ssh-ed25519,ssh-rsa");
|
|
|
|
|
2018-07-02 14:34:16 +02:00
|
|
|
/* Test all unknown host keys */
|
2017-11-13 12:03:52 +01:00
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_HOSTKEYS,
|
|
|
|
"unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_false(rc == 0);
|
|
|
|
}
|
|
|
|
|
2018-08-07 11:34:30 +02:00
|
|
|
static void torture_options_set_pubkey_accepted_types(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
enum ssh_digest_e type;
|
|
|
|
|
|
|
|
/* Test known public key algorithms */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
|
|
|
|
"ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.pubkey_accepted_types,
|
|
|
|
"ssh-ed25519,ecdsa-sha2-nistp384,ssh-rsa");
|
|
|
|
|
|
|
|
/* Test one unknown public key algorithms */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
|
|
|
|
"ssh-ed25519,unknown-crap@example.com,ssh-rsa");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.pubkey_accepted_types,
|
|
|
|
"ssh-ed25519,ssh-rsa");
|
|
|
|
|
|
|
|
/* Test all unknown public key algorithms */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
|
|
|
|
"unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_false(rc == 0);
|
|
|
|
|
|
|
|
/* Test that the option affects the algorithm selection for RSA keys */
|
|
|
|
/* simulate the SHA2 extension was negotiated */
|
|
|
|
session->extensions = SSH_EXT_SIG_RSA_SHA256;
|
|
|
|
|
|
|
|
/* previous configuration did not list the SHA2 extension algoritms, so
|
|
|
|
* it should not be used */
|
|
|
|
type = ssh_key_type_to_hash(session, SSH_KEYTYPE_RSA);
|
|
|
|
assert_int_equal(type, SSH_DIGEST_SHA1);
|
|
|
|
|
|
|
|
/* now, lets allow the signature from SHA2 extension and expect
|
|
|
|
* it to be used */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES,
|
|
|
|
"rsa-sha2-256,ssh-rsa");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.pubkey_accepted_types,
|
|
|
|
"rsa-sha2-256,ssh-rsa");
|
|
|
|
type = ssh_key_type_to_hash(session, SSH_KEYTYPE_RSA);
|
|
|
|
assert_int_equal(type, SSH_DIGEST_SHA256);
|
|
|
|
}
|
|
|
|
|
2017-08-24 15:33:11 +02:00
|
|
|
static void torture_options_set_macs(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Test known MACs */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "hmac-sha1");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], "hmac-sha1");
|
|
|
|
|
2017-10-23 16:33:28 +02:00
|
|
|
/* Test multiple known MACs */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_HMAC_S_C,
|
2019-02-12 08:56:37 +00:00
|
|
|
"hmac-sha1-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha1,hmac-sha2-256");
|
2017-10-23 16:33:28 +02:00
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C],
|
2019-02-12 08:56:37 +00:00
|
|
|
"hmac-sha1-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha1,hmac-sha2-256");
|
2017-10-23 16:33:28 +02:00
|
|
|
|
2017-08-24 15:33:11 +02:00
|
|
|
/* Test unknown MACs */
|
2019-02-12 08:56:37 +00:00
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "unknown-crap@example.com,hmac-sha1-etm@openssh.com,unknown@example.com");
|
2017-08-24 15:33:11 +02:00
|
|
|
assert_true(rc == 0);
|
2019-02-12 08:56:37 +00:00
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], "hmac-sha1-etm@openssh.com");
|
2017-08-24 15:33:11 +02:00
|
|
|
|
|
|
|
/* Test all unknown MACs */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "unknown-crap@example.com");
|
|
|
|
assert_false(rc == 0);
|
|
|
|
}
|
|
|
|
|
2011-10-28 10:25:03 +02:00
|
|
|
static void torture_options_get_host(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
char* host = NULL;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
|
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.host, "localhost");
|
2011-10-28 10:25:03 +02:00
|
|
|
|
|
|
|
assert_false(ssh_options_get(session, SSH_OPTIONS_HOST, &host));
|
|
|
|
|
|
|
|
assert_string_equal(host, "localhost");
|
2012-10-07 22:15:20 +02:00
|
|
|
free(host);
|
2011-10-28 10:25:03 +02:00
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:56 +01:00
|
|
|
static void torture_options_set_port(void **state) {
|
|
|
|
ssh_session session = *state;
|
2010-03-13 14:21:23 +01:00
|
|
|
int rc;
|
|
|
|
unsigned int port = 42;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_PORT, &port);
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_true(session->opts.port == port);
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_PORT_STR, "23");
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_true(session->opts.port == 23);
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_PORT_STR, "five");
|
2011-09-06 09:14:21 +02:00
|
|
|
assert_true(rc == -1);
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_PORT, NULL);
|
2011-09-06 09:14:21 +02:00
|
|
|
assert_true(rc == -1);
|
2010-03-13 14:21:23 +01:00
|
|
|
}
|
|
|
|
|
2011-10-28 10:26:32 +02:00
|
|
|
static void torture_options_get_port(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
unsigned int given_port = 1234;
|
|
|
|
unsigned int port_container;
|
|
|
|
int rc;
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_PORT, &given_port);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
rc = ssh_options_get_port(session, &port_container);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_int_equal(port_container, 1234);
|
|
|
|
}
|
|
|
|
|
2011-10-28 10:25:03 +02:00
|
|
|
static void torture_options_get_user(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
char* user = NULL;
|
|
|
|
int rc;
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_USER, "magicaltrevor");
|
2018-09-13 13:53:45 +02:00
|
|
|
assert_int_equal(rc, SSH_OK);
|
2011-10-28 10:25:03 +02:00
|
|
|
rc = ssh_options_get(session, SSH_OPTIONS_USER, &user);
|
2018-09-13 13:53:45 +02:00
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
assert_non_null(user);
|
2011-10-28 10:25:03 +02:00
|
|
|
assert_string_equal(user, "magicaltrevor");
|
2012-10-07 22:15:20 +02:00
|
|
|
free(user);
|
2011-10-28 10:25:03 +02:00
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:56 +01:00
|
|
|
static void torture_options_set_fd(void **state) {
|
|
|
|
ssh_session session = *state;
|
2010-03-13 14:21:23 +01:00
|
|
|
socket_t fd = 42;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_FD, &fd);
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_true(session->opts.fd == fd);
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_FD, NULL);
|
2011-10-28 22:52:03 +02:00
|
|
|
assert_true(rc == SSH_ERROR);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_true(session->opts.fd == SSH_INVALID_SOCKET);
|
2010-03-13 14:21:23 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:56 +01:00
|
|
|
static void torture_options_set_user(void **state) {
|
|
|
|
ssh_session session = *state;
|
2010-03-13 14:21:23 +01:00
|
|
|
int rc;
|
2010-12-28 15:05:56 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
# ifndef NSS_BUFLEN_PASSWD
|
|
|
|
# define NSS_BUFLEN_PASSWD 4096
|
|
|
|
# endif /* NSS_BUFLEN_PASSWD */
|
|
|
|
struct passwd pwd;
|
|
|
|
struct passwd *pwdbuf;
|
|
|
|
char buf[NSS_BUFLEN_PASSWD];
|
|
|
|
|
|
|
|
/* get local username */
|
|
|
|
rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
#endif /* _WIN32 */
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_USER, "guru");
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.username, "guru");
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_USER, NULL);
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.username, pwd.pw_name);
|
2010-03-13 14:21:23 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO */
|
|
|
|
#if 0
|
2010-12-28 15:05:56 +01:00
|
|
|
static voidtorture_options_set_sshdir)
|
2010-03-13 14:21:23 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
#endif
|
|
|
|
|
2010-12-28 15:05:56 +01:00
|
|
|
static void torture_options_set_identity(void **state) {
|
|
|
|
ssh_session session = *state;
|
2010-03-13 14:21:23 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_ADD_IDENTITY, "identity1");
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.identity->root->data, "identity1");
|
2010-03-13 14:21:23 +01:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "identity2");
|
2010-12-28 15:05:56 +01:00
|
|
|
assert_true(rc == 0);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.identity->root->data, "identity2");
|
|
|
|
assert_string_equal(session->opts.identity->root->next->data, "identity1");
|
2010-03-13 14:21:23 +01:00
|
|
|
}
|
|
|
|
|
2011-10-28 10:25:03 +02:00
|
|
|
static void torture_options_get_identity(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
char *identity = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_ADD_IDENTITY, "identity1");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
|
2018-09-13 13:53:45 +02:00
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
assert_non_null(identity);
|
2011-10-28 10:25:03 +02:00
|
|
|
assert_string_equal(identity, "identity1");
|
2012-10-07 22:15:20 +02:00
|
|
|
SAFE_FREE(identity);
|
2011-10-28 10:25:03 +02:00
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "identity2");
|
2018-09-13 13:53:45 +02:00
|
|
|
assert_int_equal(rc, SSH_OK);
|
2012-02-05 11:50:49 +01:00
|
|
|
assert_string_equal(session->opts.identity->root->data, "identity2");
|
2011-10-28 10:25:03 +02:00
|
|
|
rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
|
2018-09-13 13:53:45 +02:00
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
assert_non_null(identity);
|
2011-10-28 10:25:03 +02:00
|
|
|
assert_string_equal(identity, "identity2");
|
2012-10-07 22:15:20 +02:00
|
|
|
free(identity);
|
2011-10-28 10:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 10:19:41 +02:00
|
|
|
static void torture_options_set_global_knownhosts(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_GLOBAL_KNOWNHOSTS,
|
|
|
|
"/etc/libssh/known_hosts");
|
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
assert_string_equal(session->opts.global_knownhosts,
|
|
|
|
"/etc/libssh/known_hosts");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_options_get_global_knownhosts(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session = *state;
|
|
|
|
char *str = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_GLOBAL_KNOWNHOSTS,
|
|
|
|
"/etc/libssh/known_hosts");
|
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
assert_string_equal(session->opts.global_knownhosts,
|
|
|
|
"/etc/libssh/known_hosts");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_options_get(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, &str);
|
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
assert_string_equal(session->opts.global_knownhosts,
|
|
|
|
"/etc/libssh/known_hosts");
|
|
|
|
|
|
|
|
SSH_STRING_FREE_CHAR(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_options_set_knownhosts(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_KNOWNHOSTS,
|
|
|
|
"/home/libssh/.ssh/known_hosts");
|
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
assert_string_equal(session->opts.knownhosts,
|
|
|
|
"/home/libssh/.ssh/known_hosts");
|
2018-11-23 16:38:54 +01:00
|
|
|
|
|
|
|
/* The NULL value should not crash the libssh */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, NULL);
|
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
assert_null(session->opts.knownhosts);
|
|
|
|
|
|
|
|
/* ssh_options_apply() should set the path to correct value */
|
|
|
|
rc = ssh_options_apply(session);
|
|
|
|
assert_ssh_return_code(session, rc);
|
2019-01-15 11:19:49 +01:00
|
|
|
assert_non_null(session->opts.knownhosts);
|
2018-10-19 10:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_options_get_knownhosts(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session = *state;
|
|
|
|
char *str = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_KNOWNHOSTS,
|
|
|
|
"/home/libssh/.ssh/known_hosts");
|
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
assert_string_equal(session->opts.knownhosts,
|
|
|
|
"/home/libssh/.ssh/known_hosts");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_options_get(session, SSH_OPTIONS_KNOWNHOSTS, &str);
|
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
assert_string_equal(session->opts.knownhosts,
|
|
|
|
"/home/libssh/.ssh/known_hosts");
|
|
|
|
|
|
|
|
SSH_STRING_FREE_CHAR(str);
|
|
|
|
}
|
|
|
|
|
2013-07-11 15:15:34 +02:00
|
|
|
static void torture_options_proxycommand(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Enable ProxyCommand */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, "ssh -q -A -X -W %h:%p JUMPHOST");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
assert_string_equal(session->opts.ProxyCommand, "ssh -q -A -X -W %h:%p JUMPHOST");
|
|
|
|
|
|
|
|
/* Disable ProxyCommand */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, "none");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
assert_null(session->opts.ProxyCommand);
|
|
|
|
}
|
|
|
|
|
2014-08-03 22:29:49 +02:00
|
|
|
static void torture_options_config_host(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
FILE *config = NULL;
|
|
|
|
|
|
|
|
/* create a new config file */
|
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
2018-09-04 10:30:09 +02:00
|
|
|
fputs("Host testhost1\nPort 42\n"
|
|
|
|
"Host testhost2,testhost3\nPort 43\n"
|
|
|
|
"Host testhost4 testhost5\nPort 44\n",
|
|
|
|
config);
|
2014-08-03 22:29:49 +02:00
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost1");
|
|
|
|
ssh_options_parse_config(session, "test_config");
|
|
|
|
|
|
|
|
assert_int_equal(session->opts.port, 42);
|
|
|
|
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2014-08-03 22:29:49 +02:00
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost2");
|
|
|
|
ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_int_equal(session->opts.port, 43);
|
|
|
|
|
|
|
|
session->opts.port = 0;
|
|
|
|
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2014-08-03 22:29:49 +02:00
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost3");
|
|
|
|
ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_int_equal(session->opts.port, 43);
|
|
|
|
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 10:30:09 +02:00
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost4");
|
|
|
|
ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_int_equal(session->opts.port, 44);
|
|
|
|
|
|
|
|
session->opts.port = 0;
|
|
|
|
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 10:30:09 +02:00
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost5");
|
|
|
|
ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_int_equal(session->opts.port, 44);
|
|
|
|
|
2014-08-03 22:29:49 +02:00
|
|
|
unlink("test_config");
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:09:08 +02:00
|
|
|
static void torture_options_config_match(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session = *state;
|
|
|
|
FILE *config = NULL;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* Required for options_parse_config() */
|
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost1");
|
|
|
|
|
|
|
|
/* The Match keyword requires argument */
|
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code_equal(session, rv, SSH_ERROR);
|
|
|
|
|
|
|
|
/* The Match all keyword needs to be the only one (start) */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match all host local\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code_equal(session, rv, SSH_ERROR);
|
|
|
|
|
|
|
|
/* The Match all keyword needs to be the only one (end) */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match host local all\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code_equal(session, rv, SSH_ERROR);
|
|
|
|
|
|
|
|
/* The Match host keyword requires an argument */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match host\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code_equal(session, rv, SSH_ERROR);
|
|
|
|
|
|
|
|
/* The Match user keyword requires an argument */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match user\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code_equal(session, rv, SSH_ERROR);
|
|
|
|
|
2019-01-23 14:58:14 +01:00
|
|
|
/* The Match canonical keyword is the same as match all */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match canonical\n"
|
|
|
|
"\tPort 33\n"
|
|
|
|
"Match all\n"
|
|
|
|
"\tPort 34\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code_equal(session, rv, SSH_OK);
|
2019-01-23 14:58:14 +01:00
|
|
|
assert_int_equal(session->opts.port, 33);
|
2018-09-04 19:09:08 +02:00
|
|
|
|
|
|
|
session->opts.port = 0;
|
|
|
|
|
|
|
|
/* The Match originalhost keyword is ignored */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match originalhost origin\n"
|
|
|
|
"\tPort 33\n"
|
|
|
|
"Match all\n"
|
|
|
|
"\tPort 34\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code(session, rv);
|
|
|
|
assert_int_equal(session->opts.port, 34);
|
|
|
|
|
|
|
|
session->opts.port = 0;
|
|
|
|
|
|
|
|
/* The Match localuser keyword is ignored */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match originalhost origin\n"
|
|
|
|
"\tPort 33\n"
|
|
|
|
"Match all\n"
|
|
|
|
"\tPort 34\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code(session, rv);
|
|
|
|
assert_int_equal(session->opts.port, 34);
|
|
|
|
|
|
|
|
session->opts.port = 0;
|
|
|
|
|
|
|
|
/* The Match exec keyword is ignored */
|
2018-12-05 12:09:07 +01:00
|
|
|
torture_reset_config(session);
|
2018-09-04 19:09:08 +02:00
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("Match exec /bin/true\n"
|
|
|
|
"\tPort 33\n"
|
|
|
|
"Match all\n"
|
|
|
|
"\tPort 34\n",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code(session, rv);
|
|
|
|
assert_int_equal(session->opts.port, 34);
|
|
|
|
|
|
|
|
session->opts.port = 0;
|
|
|
|
|
|
|
|
unlink("test_config");
|
|
|
|
}
|
|
|
|
|
2018-12-04 13:42:50 +01:00
|
|
|
static void torture_options_copy(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session = *state, new = NULL;
|
|
|
|
struct ssh_iterator *it = NULL, *it2 = NULL;
|
|
|
|
FILE *config = NULL;
|
|
|
|
int i, level = 9;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* Required for options_parse_config() */
|
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "example");
|
|
|
|
|
|
|
|
/* Impossible to set through the configuration */
|
|
|
|
rv = ssh_options_set(session, SSH_OPTIONS_COMPRESSION_LEVEL, &level);
|
|
|
|
assert_ssh_return_code(session, rv);
|
|
|
|
level = 1;
|
|
|
|
rv = ssh_options_set(session, SSH_OPTIONS_NODELAY, &level);
|
|
|
|
assert_ssh_return_code(session, rv);
|
|
|
|
|
|
|
|
/* The Match keyword requires argument */
|
|
|
|
config = fopen("test_config", "w");
|
|
|
|
assert_non_null(config);
|
|
|
|
fputs("IdentityFile ~/.ssh/id_ecdsa\n"
|
|
|
|
"User tester\n"
|
|
|
|
"Hostname example.com\n"
|
|
|
|
"BindAddress 127.0.0.2\n"
|
|
|
|
"GlobalKnownHostsFile /etc/ssh/known_hosts2\n"
|
|
|
|
"UserKnownHostsFile ~/.ssh/known_hosts2\n"
|
|
|
|
"KexAlgorithms curve25519-sha256\n"
|
|
|
|
"Ciphers aes256-ctr\n"
|
|
|
|
"MACs hmac-sha2-256\n"
|
|
|
|
"HostKeyAlgorithms ssh-ed25519\n"
|
|
|
|
"Compression yes\n"
|
|
|
|
"PubkeyAcceptedTypes ssh-ed25519\n"
|
|
|
|
"ProxyCommand nc 127.0.0.10 22\n"
|
|
|
|
/* ops.custombanner */
|
|
|
|
"ConnectTimeout 42\n"
|
|
|
|
"Port 222\n"
|
|
|
|
"StrictHostKeyChecking no\n"
|
|
|
|
"GSSAPIServerIdentity my.example.com\n"
|
|
|
|
"GSSAPIClientIdentity home.sweet\n"
|
|
|
|
"GSSAPIDelegateCredentials yes\n"
|
|
|
|
"PubkeyAuthentication yes\n" /* sets flags */
|
|
|
|
"GSSAPIAuthentication no\n" /* sets flags */
|
|
|
|
"",
|
|
|
|
config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
rv = ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_ssh_return_code(session, rv);
|
|
|
|
|
|
|
|
rv = ssh_options_copy(session, &new);
|
|
|
|
assert_ssh_return_code(session, rv);
|
|
|
|
assert_non_null(new);
|
|
|
|
|
|
|
|
/* Check the identities match */
|
|
|
|
it = ssh_list_get_iterator(session->opts.identity);
|
|
|
|
assert_non_null(it);
|
|
|
|
it2 = ssh_list_get_iterator(new->opts.identity);
|
|
|
|
assert_non_null(it2);
|
|
|
|
while (it != NULL && it2 != NULL) {
|
|
|
|
assert_string_equal(it->data, it2->data);
|
|
|
|
it = it->next;
|
|
|
|
it2 = it2->next;
|
|
|
|
}
|
|
|
|
assert_null(it);
|
|
|
|
assert_null(it2);
|
|
|
|
|
|
|
|
assert_string_equal(session->opts.username, new->opts.username);
|
|
|
|
assert_string_equal(session->opts.host, new->opts.host);
|
|
|
|
assert_string_equal(session->opts.bindaddr, new->opts.bindaddr);
|
|
|
|
assert_string_equal(session->opts.sshdir, new->opts.sshdir);
|
|
|
|
assert_string_equal(session->opts.knownhosts, new->opts.knownhosts);
|
|
|
|
assert_string_equal(session->opts.global_knownhosts,
|
|
|
|
new->opts.global_knownhosts);
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
if (session->opts.wanted_methods[i] == NULL) {
|
|
|
|
assert_null(new->opts.wanted_methods[i]);
|
|
|
|
} else {
|
|
|
|
assert_string_equal(session->opts.wanted_methods[i],
|
|
|
|
new->opts.wanted_methods[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_string_equal(session->opts.pubkey_accepted_types,
|
|
|
|
new->opts.pubkey_accepted_types);
|
|
|
|
assert_string_equal(session->opts.ProxyCommand, new->opts.ProxyCommand);
|
|
|
|
/* TODO custombanner */
|
|
|
|
assert_int_equal(session->opts.timeout, new->opts.timeout);
|
|
|
|
assert_int_equal(session->opts.timeout_usec, new->opts.timeout_usec);
|
|
|
|
assert_int_equal(session->opts.port, new->opts.port);
|
|
|
|
assert_int_equal(session->opts.StrictHostKeyChecking,
|
|
|
|
new->opts.StrictHostKeyChecking);
|
|
|
|
assert_int_equal(session->opts.compressionlevel,
|
|
|
|
new->opts.compressionlevel);
|
|
|
|
assert_string_equal(session->opts.gss_server_identity,
|
|
|
|
new->opts.gss_server_identity);
|
|
|
|
assert_string_equal(session->opts.gss_client_identity,
|
|
|
|
new->opts.gss_client_identity);
|
|
|
|
assert_int_equal(session->opts.gss_delegate_creds,
|
|
|
|
new->opts.gss_delegate_creds);
|
|
|
|
assert_int_equal(session->opts.flags, new->opts.flags);
|
|
|
|
assert_int_equal(session->opts.nodelay, new->opts.nodelay);
|
|
|
|
assert_true(session->opts.config_processed == new->opts.config_processed);
|
|
|
|
assert_memory_equal(session->opts.options_seen, new->opts.options_seen,
|
|
|
|
sizeof(session->opts.options_seen));
|
|
|
|
|
|
|
|
ssh_free(new);
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:10:38 +01:00
|
|
|
#ifdef WITH_SERVER
|
|
|
|
const char template[] = "temp_dir_XXXXXX";
|
|
|
|
|
|
|
|
struct bind_st {
|
|
|
|
char *cwd;
|
|
|
|
char *temp_dir;
|
|
|
|
ssh_bind bind;
|
|
|
|
};
|
|
|
|
|
2019-03-07 17:00:44 +01:00
|
|
|
static int ssh_bind_setup_files(void **state)
|
2019-02-22 17:10:38 +01:00
|
|
|
{
|
|
|
|
struct bind_st *test_state = NULL;
|
|
|
|
char *cwd = NULL;
|
|
|
|
char *tmp_dir = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
test_state = (struct bind_st *)malloc(sizeof(struct bind_st));
|
|
|
|
assert_non_null(test_state);
|
|
|
|
|
|
|
|
cwd = torture_get_current_working_dir();
|
|
|
|
assert_non_null(cwd);
|
|
|
|
|
|
|
|
tmp_dir = torture_make_temp_dir(template);
|
|
|
|
assert_non_null(tmp_dir);
|
|
|
|
|
|
|
|
test_state->cwd = cwd;
|
|
|
|
test_state->temp_dir = tmp_dir;
|
|
|
|
|
|
|
|
*state = test_state;
|
|
|
|
|
|
|
|
rc = torture_change_dir(tmp_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
printf("Changed directory to: %s\n", tmp_dir);
|
|
|
|
|
|
|
|
/* For ed25519 the test keys are not available in legacy PEM format. Using
|
|
|
|
* the new OpenSSH format for all algorithms */
|
|
|
|
torture_write_file(LIBSSH_RSA_TESTKEY,
|
2019-03-12 10:27:35 -06:00
|
|
|
torture_get_openssh_testkey(SSH_KEYTYPE_RSA, 0));
|
2019-02-22 17:10:38 +01:00
|
|
|
|
|
|
|
torture_write_file(LIBSSH_ED25519_TESTKEY,
|
2019-03-12 10:25:49 -06:00
|
|
|
torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0));
|
2019-02-22 17:10:38 +01:00
|
|
|
#ifdef HAVE_ECC
|
|
|
|
torture_write_file(LIBSSH_ECDSA_521_TESTKEY,
|
2019-03-12 10:25:49 -06:00
|
|
|
torture_get_openssh_testkey(SSH_KEYTYPE_ECDSA_P521, 0));
|
2019-02-22 17:10:38 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DSA
|
|
|
|
torture_write_file(LIBSSH_DSA_TESTKEY,
|
2019-03-12 10:25:49 -06:00
|
|
|
torture_get_openssh_testkey(SSH_KEYTYPE_DSS, 0));
|
2019-02-22 17:10:38 +01:00
|
|
|
#endif
|
2019-03-07 17:00:44 +01:00
|
|
|
torture_write_file(LIBSSH_CUSTOM_BIND_CONFIG_FILE,
|
|
|
|
"Port 42\n");
|
2019-02-22 17:10:38 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-09-04 19:09:08 +02:00
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
|
|
|
|
/* sshbind options */
|
|
|
|
static int sshbind_setup(void **state)
|
|
|
|
{
|
2019-02-22 17:10:38 +01:00
|
|
|
int rc;
|
|
|
|
struct bind_st *test_state = NULL;
|
|
|
|
|
2019-03-07 17:00:44 +01:00
|
|
|
rc = ssh_bind_setup_files((void **)&test_state);
|
2019-02-22 17:10:38 +01:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
|
|
|
|
test_state->bind = ssh_bind_new();
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
|
|
|
|
*state = test_state;
|
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sshbind_teardown(void **state)
|
|
|
|
{
|
2019-02-22 17:10:38 +01:00
|
|
|
struct bind_st *test_state = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->cwd);
|
|
|
|
assert_non_null(test_state->temp_dir);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
|
|
|
|
rc = torture_change_dir(test_state->cwd);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
rc = torture_rmdirs(test_state->temp_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
SAFE_FREE(test_state->temp_dir);
|
|
|
|
SAFE_FREE(test_state->cwd);
|
|
|
|
ssh_bind_free(test_state->bind);
|
|
|
|
SAFE_FREE(test_state);
|
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_import_key(void **state)
|
|
|
|
{
|
2019-02-22 17:10:38 +01:00
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
2016-11-12 01:33:37 +00:00
|
|
|
int rc;
|
|
|
|
const char *base64_key;
|
tests: plug leak in torture_bind_options_import_key
Summary:
Hello, this is a resend for a quick memory leak fix for one of the unit
tests, originally sent to the mailing list here:
* https://www.libssh.org/archive/libssh/2017-07/0000017.html
Test Plan:
* Before the fix and running the test with valgrind:
```
[simonsj@simonsj-lx5 : unittests] valgrind --leak-check=full ./torture_options >/dev/null
==93134== Memcheck, a memory error detector
==93134== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==93134== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==93134== Command: ./torture_options
==93134==
[ PASSED ] 10 test(s).
[ PASSED ] 1 test(s).
==93134==
==93134== HEAP SUMMARY:
==93134== in use at exit: 80 bytes in 1 blocks
==93134== total heap usage: 977 allocs, 976 frees, 75,029 bytes allocated
==93134==
==93134== 80 bytes in 1 blocks are definitely lost in loss record 1 of 1
==93134== at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==93134== by 0x41BAB0: ssh_key_new (pki.c:107)
==93134== by 0x40DF90: torture_bind_options_import_key (torture_options.c:222)
==93134== by 0x4E3AA3A: cmocka_run_one_test_or_fixture (cmocka.c:2304)
==93134== by 0x4E3ACEA: cmocka_run_one_tests (cmocka.c:2412)
==93134== by 0x4E3B036: _cmocka_run_group_tests (cmocka.c:2517)
==93134== by 0x40E9E3: torture_run_tests (torture_options.c:276)
==93134== by 0x40DE68: main (torture.c:1100)
==93134==
==93134== LEAK SUMMARY:
==93134== definitely lost: 80 bytes in 1 blocks
==93134== indirectly lost: 0 bytes in 0 blocks
==93134== possibly lost: 0 bytes in 0 blocks
==93134== still reachable: 0 bytes in 0 blocks
==93134== suppressed: 0 bytes in 0 blocks
==93134==
==93134== For counts of detected and suppressed errors, rerun with: -v
==93134== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
```
* And after:
```
[simonsj@simonsj-lx5 : unittests] valgrind --leak-check=full ./torture_options >/dev/null
==93294== Memcheck, a memory error detector
==93294== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==93294== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==93294== Command: ./torture_options
==93294==
[ PASSED ] 10 test(s).
[ PASSED ] 1 test(s).
==93294==
==93294== HEAP SUMMARY:
==93294== in use at exit: 0 bytes in 0 blocks
==93294== total heap usage: 977 allocs, 977 frees, 75,029 bytes allocated
==93294==
==93294== All heap blocks were freed -- no leaks are possible
==93294==
==93294== For counts of detected and suppressed errors, rerun with: -v
==93294== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
```
Reviewers: asn
Reviewed By: asn
Differential Revision: https://bugs.libssh.org/D3
2017-08-17 09:26:21 +02:00
|
|
|
ssh_key key = ssh_key_new();
|
2016-11-12 01:33:37 +00:00
|
|
|
|
2019-02-22 17:10:38 +01:00
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
/* set null */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, NULL);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
/* set invalid key */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
|
|
|
assert_int_equal(rc, -1);
|
2018-09-24 18:42:22 +02:00
|
|
|
SSH_KEY_FREE(key);
|
2016-11-12 01:33:37 +00:00
|
|
|
|
|
|
|
/* set rsa key */
|
2019-03-12 10:25:49 -06:00
|
|
|
base64_key = torture_get_testkey(SSH_KEYTYPE_RSA, 0);
|
2018-08-06 08:44:28 +02:00
|
|
|
rc = ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
|
|
|
|
assert_int_equal(rc, SSH_OK);
|
2018-09-13 13:53:45 +02:00
|
|
|
assert_non_null(key);
|
2018-08-06 08:44:28 +02:00
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
|
|
|
assert_int_equal(rc, 0);
|
2017-12-28 11:10:43 +01:00
|
|
|
#ifdef HAVE_DSA
|
2016-11-12 01:33:37 +00:00
|
|
|
/* set dsa key */
|
2019-03-12 10:25:49 -06:00
|
|
|
base64_key = torture_get_testkey(SSH_KEYTYPE_DSS, 0);
|
2018-09-13 13:53:45 +02:00
|
|
|
rc = ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
|
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
assert_non_null(key);
|
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
|
|
|
assert_int_equal(rc, 0);
|
2017-12-28 11:10:43 +01:00
|
|
|
#endif
|
2019-02-22 17:10:38 +01:00
|
|
|
#ifdef HAVE_ECC
|
2016-11-12 01:33:37 +00:00
|
|
|
/* set ecdsa key */
|
2019-03-12 10:25:49 -06:00
|
|
|
base64_key = torture_get_testkey(SSH_KEYTYPE_ECDSA_P521, 0);
|
2018-09-13 13:53:45 +02:00
|
|
|
rc = ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
|
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
assert_non_null(key);
|
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
|
|
|
assert_int_equal(rc, 0);
|
2019-02-22 17:10:38 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_hostkey(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
/* Test RSA key */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HOSTKEY,
|
|
|
|
LIBSSH_RSA_TESTKEY);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->rsakey);
|
|
|
|
assert_string_equal(bind->rsakey, LIBSSH_RSA_TESTKEY);
|
|
|
|
|
|
|
|
/* Test ED25519 key */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HOSTKEY,
|
|
|
|
LIBSSH_ED25519_TESTKEY);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->ed25519key);
|
|
|
|
assert_string_equal(bind->ed25519key, LIBSSH_ED25519_TESTKEY);
|
|
|
|
|
|
|
|
#ifdef HAVE_ECC
|
|
|
|
/* Test ECDSA key */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HOSTKEY,
|
|
|
|
LIBSSH_ECDSA_521_TESTKEY);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->ecdsakey);
|
|
|
|
assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DSA
|
|
|
|
/* Test DSA key */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HOSTKEY,
|
|
|
|
LIBSSH_DSA_TESTKEY);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->dsakey);
|
|
|
|
assert_string_equal(bind->dsakey, LIBSSH_DSA_TESTKEY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_bindaddr(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
const char *address = "127.0.0.1";
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDADDR, address);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->bindaddr);
|
|
|
|
assert_string_equal(bind->bindaddr, address);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_bindport(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
unsigned int given_port = 1234;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDPORT, &given_port);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_int_equal(bind->bindport, 1234);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_bindport_str(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDPORT_STR, "23");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_int_equal(bind->bindport, 23);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_log_verbosity(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int verbosity = SSH_LOG_PACKET;
|
|
|
|
int previous_level, new_level;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
previous_level = ssh_get_log_level();
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_LOG_VERBOSITY, &verbosity);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
new_level = ssh_get_log_level();
|
|
|
|
assert_int_equal(new_level, verbosity);
|
|
|
|
|
|
|
|
rc = ssh_set_log_level(previous_level);
|
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_log_verbosity_str(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
int previous_level, new_level;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
previous_level = ssh_get_log_level();
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR, "3");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
new_level = ssh_get_log_level();
|
|
|
|
assert_int_equal(new_level, SSH_LOG_PACKET);
|
|
|
|
|
|
|
|
rc = ssh_set_log_level(previous_level);
|
|
|
|
assert_int_equal(rc, SSH_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_DSA
|
|
|
|
static void torture_bind_options_dsakey(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_DSAKEY,
|
|
|
|
LIBSSH_DSA_TESTKEY);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->dsakey);
|
|
|
|
assert_string_equal(bind->dsakey, LIBSSH_DSA_TESTKEY);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void torture_bind_options_rsakey(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_RSAKEY,
|
|
|
|
LIBSSH_RSA_TESTKEY);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->rsakey);
|
|
|
|
assert_string_equal(bind->rsakey, LIBSSH_RSA_TESTKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_ECC
|
|
|
|
static void torture_bind_options_ecdsakey(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_ECDSAKEY,
|
|
|
|
LIBSSH_ECDSA_521_TESTKEY);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->ecdsakey);
|
|
|
|
assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void torture_bind_options_banner(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
const char *banner = "This is the new banner";
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_BANNER,
|
|
|
|
banner);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->banner);
|
|
|
|
assert_string_equal(bind->banner, banner);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_set_ciphers(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
assert_non_null(bind->wanted_methods);
|
|
|
|
|
|
|
|
/* Test known ciphers */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_C_S,
|
|
|
|
"aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_CRYPT_C_S]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S],
|
|
|
|
"aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
|
|
|
|
/* Test one unknown cipher */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_C_S,
|
|
|
|
"aes128-ctr,unknown-crap@example.com,aes192-ctr,aes256-ctr");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_CRYPT_C_S]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S],
|
|
|
|
"aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
|
|
|
|
/* Test all unknown ciphers */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_C_S,
|
|
|
|
"unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_int_not_equal(rc, 0);
|
|
|
|
|
|
|
|
/* Test known ciphers */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_S_C,
|
|
|
|
"aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_CRYPT_S_C]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C],
|
|
|
|
"aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
|
|
|
|
/* Test one unknown cipher */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_S_C,
|
|
|
|
"aes128-ctr,unknown-crap@example.com,aes192-ctr,aes256-ctr");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_CRYPT_S_C]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C],
|
|
|
|
"aes128-ctr,aes192-ctr,aes256-ctr");
|
|
|
|
|
|
|
|
/* Test all unknown ciphers */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_CIPHERS_S_C,
|
|
|
|
"unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_int_not_equal(rc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_set_key_exchange(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
assert_non_null(bind->wanted_methods);
|
|
|
|
|
|
|
|
/* Test known kexes */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_KEY_EXCHANGE,
|
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_KEX]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_KEX],
|
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
|
|
|
|
|
|
|
/* Test one unknown kex */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_KEY_EXCHANGE,
|
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,unknown-crap@example.com,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_KEX]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_KEX],
|
|
|
|
"curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1");
|
|
|
|
|
|
|
|
/* Test all unknown kexes */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_KEY_EXCHANGE,
|
|
|
|
"unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_int_not_equal(rc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_set_macs(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
assert_non_null(bind->wanted_methods);
|
|
|
|
|
|
|
|
/* Test known MACs */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_HMAC_S_C, "hmac-sha1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_MAC_S_C], "hmac-sha1");
|
|
|
|
|
|
|
|
/* Test multiple known MACs */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HMAC_S_C,
|
|
|
|
"hmac-sha1,hmac-sha2-256");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_MAC_S_C],
|
|
|
|
"hmac-sha1,hmac-sha2-256");
|
|
|
|
|
|
|
|
/* Test unknown MACs */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HMAC_S_C,
|
|
|
|
"unknown-crap@example.com,hmac-sha1,unknown@example.com");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_MAC_S_C], "hmac-sha1");
|
|
|
|
|
|
|
|
/* Test all unknown MACs */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_HMAC_S_C, "unknown-crap@example.com");
|
|
|
|
assert_int_not_equal(rc, 0);
|
|
|
|
|
|
|
|
/* Test known MACs */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_HMAC_C_S, "hmac-sha1");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_MAC_C_S], "hmac-sha1");
|
|
|
|
|
|
|
|
/* Test multiple known MACs */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HMAC_C_S,
|
|
|
|
"hmac-sha1,hmac-sha2-256");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_MAC_C_S],
|
|
|
|
"hmac-sha1,hmac-sha2-256");
|
|
|
|
|
|
|
|
/* Test unknown MACs */
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_HMAC_C_S,
|
|
|
|
"unknown-crap@example.com,hmac-sha1,unknown@example.com");
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
|
|
|
|
assert_string_equal(bind->wanted_methods[SSH_MAC_C_S], "hmac-sha1");
|
|
|
|
|
|
|
|
/* Test all unknown MACs */
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_HMAC_C_S, "unknown-crap@example.com");
|
|
|
|
assert_int_not_equal(rc, 0);
|
2016-11-12 01:33:37 +00:00
|
|
|
}
|
2019-02-22 17:10:38 +01:00
|
|
|
|
2019-03-07 17:00:44 +01:00
|
|
|
static void torture_bind_options_parse_config(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
2019-03-07 13:04:29 +01:00
|
|
|
char *cwd = NULL;
|
2019-03-07 17:00:44 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
2019-03-07 13:04:29 +01:00
|
|
|
cwd = torture_get_current_working_dir();
|
|
|
|
assert_non_null(cwd);
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_CONFIG_DIR,
|
|
|
|
(const char *)cwd);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->config_dir);
|
|
|
|
assert_string_equal(bind->config_dir, cwd);
|
|
|
|
|
|
|
|
rc = ssh_bind_options_parse_config(bind, "%d/"LIBSSH_CUSTOM_BIND_CONFIG_FILE);
|
2019-03-07 17:00:44 +01:00
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_int_equal(bind->bindport, 42);
|
2019-03-07 13:04:29 +01:00
|
|
|
|
|
|
|
SAFE_FREE(cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_config_dir(void **state)
|
|
|
|
{
|
|
|
|
struct bind_st *test_state;
|
|
|
|
ssh_bind bind;
|
|
|
|
const char *new_dir = "/new/dir/";
|
|
|
|
const char *replacement_dir = "/replacement/dir/";
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert_non_null(state);
|
|
|
|
test_state = *((struct bind_st **)state);
|
|
|
|
assert_non_null(test_state);
|
|
|
|
assert_non_null(test_state->bind);
|
|
|
|
bind = test_state->bind;
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_CONFIG_DIR,
|
|
|
|
new_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->config_dir);
|
|
|
|
assert_string_equal(bind->config_dir, new_dir);
|
|
|
|
|
|
|
|
rc = ssh_bind_options_set(bind,
|
|
|
|
SSH_BIND_OPTIONS_CONFIG_DIR,
|
|
|
|
replacement_dir);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_non_null(bind->config_dir);
|
|
|
|
assert_string_equal(bind->config_dir, replacement_dir);
|
2019-03-07 17:00:44 +01:00
|
|
|
}
|
|
|
|
|
2019-03-07 13:04:29 +01:00
|
|
|
|
2017-07-14 08:53:15 +02:00
|
|
|
#endif /* WITH_SERVER */
|
2016-11-12 01:33:37 +00:00
|
|
|
|
|
|
|
|
2010-12-28 15:05:56 +01:00
|
|
|
int torture_run_tests(void) {
|
2011-01-02 17:25:51 +01:00
|
|
|
int rc;
|
2015-09-07 10:39:51 +02:00
|
|
|
struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_host, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_get_host, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_port, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_get_port, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_fd, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_user, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_get_user, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_identity, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_get_identity, setup, teardown),
|
2018-10-19 10:19:41 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_global_knownhosts, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_get_global_knownhosts, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_knownhosts, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_get_knownhosts, setup, teardown),
|
2015-09-07 10:39:51 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_proxycommand, setup, teardown),
|
2017-08-24 15:33:11 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_ciphers, setup, teardown),
|
2017-10-23 14:53:55 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_key_exchange, setup, teardown),
|
2017-11-13 12:03:52 +01:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_hostkey, setup, teardown),
|
2018-08-07 11:34:30 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_pubkey_accepted_types, setup, teardown),
|
2017-08-24 15:33:11 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_macs, setup, teardown),
|
2018-12-04 13:42:50 +01:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_copy, setup, teardown),
|
2018-09-04 19:09:08 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_config_host, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_options_config_match,
|
|
|
|
setup, teardown)
|
2010-12-28 15:05:56 +01:00
|
|
|
};
|
2010-03-13 14:21:23 +01:00
|
|
|
|
2017-07-14 08:52:08 +02:00
|
|
|
#ifdef WITH_SERVER
|
2016-11-12 01:33:37 +00:00
|
|
|
struct CMUnitTest sshbind_tests[] = {
|
2019-02-22 17:10:38 +01:00
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_import_key,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_hostkey,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_bindaddr,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_bindport,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_bindport_str,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_log_verbosity,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_log_verbosity_str,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
#ifdef HAVE_DSA
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_dsakey,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
#endif
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_rsakey,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
#ifdef HAVE_ECC
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_ecdsakey,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
#endif
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_banner,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_set_ciphers,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_set_key_exchange,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_set_macs,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
2019-03-07 17:00:44 +01:00
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_parse_config,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
2019-03-07 13:04:29 +01:00
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_config_dir,
|
|
|
|
sshbind_setup, sshbind_teardown),
|
2016-11-12 01:33:37 +00:00
|
|
|
};
|
2017-07-14 08:52:08 +02:00
|
|
|
#endif /* WITH_SERVER */
|
2016-11-12 01:33:37 +00:00
|
|
|
|
2011-01-02 17:25:51 +01:00
|
|
|
ssh_init();
|
2014-09-02 09:07:17 +02:00
|
|
|
torture_filter_tests(tests);
|
2015-09-07 10:39:51 +02:00
|
|
|
rc = cmocka_run_group_tests(tests, NULL, NULL);
|
2017-07-14 08:52:08 +02:00
|
|
|
#ifdef WITH_SERVER
|
2016-11-12 01:33:37 +00:00
|
|
|
rc += cmocka_run_group_tests(sshbind_tests, NULL, NULL);
|
2017-07-14 08:52:08 +02:00
|
|
|
#endif /* WITH_SERVER */
|
2011-01-02 17:25:51 +01:00
|
|
|
ssh_finalize();
|
|
|
|
return rc;
|
2010-12-28 15:05:56 +01:00
|
|
|
}
|