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"
|
2011-01-01 20:20:42 +01:00
|
|
|
#include <libssh/session.h>
|
|
|
|
#include <libssh/misc.h>
|
2010-03-13 14:21:23 +01:00
|
|
|
|
2015-09-07 10:39:51 +02:00
|
|
|
static int setup(void **state)
|
|
|
|
{
|
2010-12-28 15:05:56 +01:00
|
|
|
ssh_session session = ssh_new();
|
|
|
|
*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,
|
|
|
|
"curve25519-sha256@libssh.org,ecdh-sha2-nistp256,diffie-hellman-group14-sha1");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_KEX],
|
|
|
|
"curve25519-sha256@libssh.org,ecdh-sha2-nistp256,diffie-hellman-group14-sha1");
|
|
|
|
|
|
|
|
/* Test one unknown kex */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_KEY_EXCHANGE,
|
|
|
|
"curve25519-sha256@libssh.org,unknown-crap@example.com,diffie-hellman-group14-sha1");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_KEX],
|
|
|
|
"curve25519-sha256@libssh.org,diffie-hellman-group14-sha1");
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
|
|
|
|
/* Test one unknown kex */
|
|
|
|
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");
|
|
|
|
|
|
|
|
/* Test all unknown kexes */
|
|
|
|
rc = ssh_options_set(session,
|
|
|
|
SSH_OPTIONS_HOSTKEYS,
|
|
|
|
"unknown-crap@example.com,more-crap@example.com");
|
|
|
|
assert_false(rc == 0);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
"hmac-sha1,hmac-sha2-256");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C],
|
|
|
|
"hmac-sha1,hmac-sha2-256");
|
|
|
|
|
2017-08-24 15:33:11 +02:00
|
|
|
/* Test unknown MACs */
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "unknown-crap@example.com,hmac-sha1,unknown@example.com");
|
|
|
|
assert_true(rc == 0);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], "hmac-sha1");
|
|
|
|
|
|
|
|
/* 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");
|
2011-10-28 22:52:03 +02:00
|
|
|
assert_true(rc == SSH_OK);
|
2011-10-28 10:25:03 +02:00
|
|
|
rc = ssh_options_get(session, SSH_OPTIONS_USER, &user);
|
|
|
|
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);
|
2011-10-28 22:52:03 +02:00
|
|
|
assert_true(rc == SSH_OK);
|
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");
|
|
|
|
assert_true(rc == 0);
|
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);
|
2011-10-28 22:52:03 +02:00
|
|
|
assert_true(rc == SSH_OK);
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
fputs("Host testhost1\nPort 42\nHost testhost2,testhost3\nPort 43\n", config);
|
|
|
|
fclose(config);
|
|
|
|
|
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost1");
|
|
|
|
ssh_options_parse_config(session, "test_config");
|
|
|
|
|
|
|
|
assert_int_equal(session->opts.port, 42);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost3");
|
|
|
|
ssh_options_parse_config(session, "test_config");
|
|
|
|
assert_int_equal(session->opts.port, 43);
|
|
|
|
|
|
|
|
unlink("test_config");
|
|
|
|
}
|
|
|
|
|
2016-11-12 01:33:37 +00:00
|
|
|
|
2017-07-14 08:52:08 +02:00
|
|
|
#ifdef WITH_SERVER
|
2016-11-12 01:33:37 +00:00
|
|
|
/* sshbind options */
|
|
|
|
static int sshbind_setup(void **state)
|
|
|
|
{
|
|
|
|
ssh_bind bind = ssh_bind_new();
|
|
|
|
*state = bind;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sshbind_teardown(void **state)
|
|
|
|
{
|
|
|
|
ssh_bind_free(*state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_bind_options_import_key(void **state)
|
|
|
|
{
|
|
|
|
ssh_bind bind = *state;
|
|
|
|
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
|
|
|
|
|
|
|
/* 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);
|
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_free(key);
|
2016-11-12 01:33:37 +00:00
|
|
|
|
|
|
|
/* set rsa key */
|
|
|
|
base64_key = torture_get_testkey(SSH_KEYTYPE_RSA, 0, 0);
|
|
|
|
ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
/* set dsa key */
|
|
|
|
base64_key = torture_get_testkey(SSH_KEYTYPE_DSS, 0, 0);
|
|
|
|
ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
/* set ecdsa key */
|
|
|
|
base64_key = torture_get_testkey(SSH_KEYTYPE_ECDSA, 512, 0);
|
|
|
|
ssh_pki_import_privkey_base64(base64_key, NULL, NULL, NULL, &key);
|
|
|
|
rc = ssh_bind_options_set(bind, SSH_BIND_OPTIONS_IMPORT_KEY, key);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
}
|
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),
|
|
|
|
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),
|
2017-08-24 15:33:11 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_set_macs, setup, teardown),
|
2014-08-03 22:29:49 +02:00
|
|
|
cmocka_unit_test_setup_teardown(torture_options_config_host, 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[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(torture_bind_options_import_key, sshbind_setup, sshbind_teardown),
|
|
|
|
};
|
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
|
|
|
}
|