2017-10-28 15:31:37 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2017-08-24 16:09:08 +03:00
|
|
|
#define LIBSSH_STATIC
|
|
|
|
|
|
|
|
#include "torture.h"
|
|
|
|
#include "libssh/options.h"
|
2017-10-23 16:06:12 +03:00
|
|
|
#include "libssh/session.h"
|
2017-08-24 16:09:08 +03:00
|
|
|
|
|
|
|
#define LIBSSH_TESTCONFIG1 "libssh_testconfig1.tmp"
|
|
|
|
#define LIBSSH_TESTCONFIG2 "libssh_testconfig2.tmp"
|
|
|
|
#define LIBSSH_TESTCONFIG3 "libssh_testconfig3.tmp"
|
2017-09-10 06:11:47 +03:00
|
|
|
#define LIBSSH_TESTCONFIG4 "libssh_testconfig4.tmp"
|
2017-10-25 15:20:52 +03:00
|
|
|
#define LIBSSH_TESTCONFIG5 "libssh_testconfig5.tmp"
|
|
|
|
#define LIBSSH_TESTCONFIG6 "libssh_testconfig6.tmp"
|
|
|
|
#define LIBSSH_TESTCONFIGGLOB "libssh_testc*[36].tmp"
|
2017-08-24 16:09:08 +03:00
|
|
|
|
|
|
|
#define USERNAME "testuser"
|
|
|
|
#define PROXYCMD "ssh -q -W %h:%p gateway.example.com"
|
|
|
|
#define ID_FILE "/etc/xxx"
|
2017-10-23 16:06:12 +03:00
|
|
|
#define KEXALGORITHMS "ecdh-sha2-nistp521,diffie-hellman-group14-sha1"
|
2017-10-23 17:33:28 +03:00
|
|
|
#define MACS "hmac-sha1,hmac-sha2-256"
|
2017-08-24 16:09:08 +03:00
|
|
|
|
|
|
|
static int setup_config_files(void **state)
|
|
|
|
{
|
|
|
|
ssh_session session;
|
|
|
|
|
|
|
|
unlink(LIBSSH_TESTCONFIG1);
|
|
|
|
unlink(LIBSSH_TESTCONFIG2);
|
|
|
|
unlink(LIBSSH_TESTCONFIG3);
|
2017-09-10 06:11:47 +03:00
|
|
|
unlink(LIBSSH_TESTCONFIG4);
|
2017-10-25 15:20:52 +03:00
|
|
|
unlink(LIBSSH_TESTCONFIG5);
|
|
|
|
unlink(LIBSSH_TESTCONFIG6);
|
2017-08-24 16:09:08 +03:00
|
|
|
|
|
|
|
torture_write_file(LIBSSH_TESTCONFIG1,
|
|
|
|
"User "USERNAME"\nInclude "LIBSSH_TESTCONFIG2"\n\n");
|
|
|
|
torture_write_file(LIBSSH_TESTCONFIG2,
|
|
|
|
"Include "LIBSSH_TESTCONFIG3"\n"
|
|
|
|
"ProxyCommand "PROXYCMD"\n\n");
|
|
|
|
torture_write_file(LIBSSH_TESTCONFIG3,
|
2017-10-23 16:06:12 +03:00
|
|
|
"\n\nIdentityFile "ID_FILE"\n"
|
2017-10-23 17:33:28 +03:00
|
|
|
"\n\nKexAlgorithms "KEXALGORITHMS"\n"
|
|
|
|
"\n\nMACs "MACS"\n");
|
2017-08-24 16:09:08 +03:00
|
|
|
|
2017-09-10 06:11:47 +03:00
|
|
|
/* Multiple Port settings -> parsing returns early. */
|
|
|
|
torture_write_file(LIBSSH_TESTCONFIG4,
|
|
|
|
"Port 123\nPort 456\n");
|
|
|
|
|
2017-10-25 15:20:52 +03:00
|
|
|
/* Testing glob include */
|
|
|
|
torture_write_file(LIBSSH_TESTCONFIG5,
|
|
|
|
"User "USERNAME"\nInclude "LIBSSH_TESTCONFIGGLOB"\n\n");
|
|
|
|
|
|
|
|
torture_write_file(LIBSSH_TESTCONFIG6,
|
|
|
|
"ProxyCommand "PROXYCMD"\n\n");
|
|
|
|
|
2017-08-24 16:09:08 +03:00
|
|
|
session = ssh_new();
|
|
|
|
*state = session;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int teardown(void **state)
|
|
|
|
{
|
|
|
|
unlink(LIBSSH_TESTCONFIG1);
|
|
|
|
unlink(LIBSSH_TESTCONFIG2);
|
|
|
|
unlink(LIBSSH_TESTCONFIG3);
|
2017-09-10 06:11:47 +03:00
|
|
|
unlink(LIBSSH_TESTCONFIG4);
|
2017-10-25 15:20:52 +03:00
|
|
|
unlink(LIBSSH_TESTCONFIG5);
|
|
|
|
unlink(LIBSSH_TESTCONFIG6);
|
2017-08-24 16:09:08 +03:00
|
|
|
|
|
|
|
ssh_free(*state);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-09-10 06:11:47 +03:00
|
|
|
* @brief tests ssh_config_parse_file with Include directives
|
2017-08-24 16:09:08 +03:00
|
|
|
*/
|
|
|
|
static void torture_config_from_file(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int ret;
|
|
|
|
char *v;
|
|
|
|
|
|
|
|
ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG1);
|
|
|
|
assert_true(ret == 0);
|
|
|
|
|
|
|
|
/* Test the variable presence */
|
|
|
|
|
|
|
|
ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v);
|
|
|
|
assert_true(ret == 0);
|
|
|
|
|
|
|
|
assert_string_equal(v, PROXYCMD);
|
|
|
|
ssh_string_free_char(v);
|
|
|
|
|
|
|
|
ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v);
|
|
|
|
assert_true(ret == 0);
|
|
|
|
|
|
|
|
assert_string_equal(v, ID_FILE);
|
|
|
|
ssh_string_free_char(v);
|
|
|
|
|
|
|
|
ret = ssh_options_get(session, SSH_OPTIONS_USER, &v);
|
|
|
|
assert_true(ret == 0);
|
|
|
|
|
|
|
|
assert_string_equal(v, USERNAME);
|
|
|
|
ssh_string_free_char(v);
|
|
|
|
|
2017-10-23 16:06:12 +03:00
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_KEX], KEXALGORITHMS);
|
2017-10-23 17:33:28 +03:00
|
|
|
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], MACS);
|
|
|
|
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], MACS);
|
2017-08-24 16:09:08 +03:00
|
|
|
}
|
|
|
|
|
2017-09-10 06:11:47 +03:00
|
|
|
/**
|
|
|
|
* @brief tests ssh_config_parse_file with multiple Port settings.
|
|
|
|
*/
|
|
|
|
static void torture_config_double_ports(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG4);
|
|
|
|
assert_true(ret == 0);
|
|
|
|
}
|
|
|
|
|
2017-10-25 15:20:52 +03:00
|
|
|
static void torture_config_glob(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
int ret;
|
2017-12-11 17:37:02 +03:00
|
|
|
#ifdef HAVE_GLOB
|
2017-10-25 15:20:52 +03:00
|
|
|
char *v;
|
2017-12-11 17:37:02 +03:00
|
|
|
#endif
|
2017-10-25 15:20:52 +03:00
|
|
|
|
|
|
|
ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG5);
|
2017-12-11 17:37:02 +03:00
|
|
|
assert_true(ret == 0); /* non-existing files should not error */
|
2017-10-25 15:20:52 +03:00
|
|
|
|
|
|
|
/* Test the variable presence */
|
|
|
|
|
2017-12-11 17:37:02 +03:00
|
|
|
#ifdef HAVE_GLOB
|
2017-10-25 15:20:52 +03:00
|
|
|
ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v);
|
|
|
|
assert_true(ret == 0);
|
|
|
|
|
|
|
|
assert_string_equal(v, PROXYCMD);
|
|
|
|
ssh_string_free_char(v);
|
|
|
|
|
|
|
|
ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v);
|
|
|
|
assert_true(ret == 0);
|
|
|
|
|
|
|
|
assert_string_equal(v, ID_FILE);
|
|
|
|
ssh_string_free_char(v);
|
2017-12-11 17:37:02 +03:00
|
|
|
#endif /* HAVE_GLOB */
|
2017-10-25 15:20:52 +03:00
|
|
|
}
|
|
|
|
|
2017-08-24 16:09:08 +03:00
|
|
|
int torture_run_tests(void) {
|
|
|
|
int rc;
|
|
|
|
struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(torture_config_from_file,
|
|
|
|
setup_config_files,
|
|
|
|
teardown),
|
2017-09-10 06:11:47 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_config_double_ports,
|
|
|
|
setup_config_files,
|
|
|
|
teardown),
|
2017-10-25 15:20:52 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_config_glob,
|
|
|
|
setup_config_files,
|
|
|
|
teardown),
|
2017-08-24 16:09:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ssh_init();
|
|
|
|
torture_filter_tests(tests);
|
|
|
|
rc = cmocka_run_group_tests(tests, NULL, NULL);
|
|
|
|
ssh_finalize();
|
|
|
|
return rc;
|
|
|
|
}
|