2017-10-28 15:31:37 +03:00
|
|
|
#include "config.h"
|
2011-01-11 19:48:48 +03:00
|
|
|
|
2018-04-12 18:03:06 +03:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2010-03-02 15:47:14 +03:00
|
|
|
#include <sys/types.h>
|
2010-12-29 22:35:55 +03:00
|
|
|
#ifndef _WIN32
|
2011-01-11 19:48:48 +03:00
|
|
|
|
|
|
|
#define _POSIX_PTHREAD_SEMANTICS
|
2010-03-02 15:47:14 +03:00
|
|
|
#include <pwd.h>
|
2010-12-29 22:35:55 +03:00
|
|
|
#endif
|
2010-03-03 01:48:45 +03:00
|
|
|
|
|
|
|
#define LIBSSH_STATIC
|
2010-03-02 15:47:14 +03:00
|
|
|
#include <libssh/priv.h>
|
|
|
|
|
|
|
|
#include "misc.c"
|
2022-06-30 17:43:35 +03:00
|
|
|
#include "torture.h"
|
2011-02-13 13:55:31 +03:00
|
|
|
#include "error.c"
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
#define TORTURE_TEST_DIR "/usr/local/bin/truc/much/.."
|
2010-03-02 15:47:14 +03:00
|
|
|
|
2019-08-01 13:45:01 +03:00
|
|
|
const char template[] = "temp_dir_XXXXXX";
|
2010-05-11 03:12:08 +04:00
|
|
|
|
2015-09-07 11:39:51 +03:00
|
|
|
static int setup(void **state)
|
|
|
|
{
|
2010-12-28 16:57:42 +03:00
|
|
|
ssh_session session = ssh_new();
|
|
|
|
*state = session;
|
2015-09-07 11:39:51 +03:00
|
|
|
|
|
|
|
return 0;
|
2010-05-11 03:12:08 +04:00
|
|
|
}
|
|
|
|
|
2015-09-07 11:39:51 +03:00
|
|
|
static int teardown(void **state)
|
|
|
|
{
|
2010-12-28 16:57:42 +03:00
|
|
|
ssh_free(*state);
|
2015-09-07 11:39:51 +03:00
|
|
|
|
|
|
|
return 0;
|
2010-05-11 03:12:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void torture_get_user_home_dir(void **state) {
|
2010-12-29 22:35:55 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
struct passwd *pwd = getpwuid(getuid());
|
|
|
|
#endif /* _WIN32 */
|
2010-03-02 15:47:14 +03:00
|
|
|
char *user;
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
(void) state;
|
|
|
|
|
2010-03-02 15:47:14 +03:00
|
|
|
user = ssh_get_user_home_dir();
|
2019-01-15 15:45:10 +03:00
|
|
|
assert_non_null(user);
|
2010-12-29 22:35:55 +03:00
|
|
|
#ifndef _WIN32
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(user, pwd->pw_dir);
|
2010-12-29 22:35:55 +03:00
|
|
|
#endif /* _WIN32 */
|
2010-03-13 18:59:26 +03:00
|
|
|
|
|
|
|
SAFE_FREE(user);
|
2010-03-02 15:47:14 +03:00
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void torture_basename(void **state) {
|
2010-05-09 00:13:35 +04:00
|
|
|
char *path;
|
2010-12-28 16:57:42 +03:00
|
|
|
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
path=ssh_basename(TORTURE_TEST_DIR "/test");
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(path);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(path, "test");
|
2010-05-09 00:13:35 +04:00
|
|
|
SAFE_FREE(path);
|
2010-12-28 16:57:42 +03:00
|
|
|
path=ssh_basename(TORTURE_TEST_DIR "/test/");
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(path);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(path, "test");
|
2010-05-09 00:13:35 +04:00
|
|
|
SAFE_FREE(path);
|
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void torture_dirname(void **state) {
|
2010-05-09 00:13:35 +04:00
|
|
|
char *path;
|
2010-12-28 16:57:42 +03:00
|
|
|
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
path=ssh_dirname(TORTURE_TEST_DIR "/test");
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(path);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(path, TORTURE_TEST_DIR );
|
2010-05-09 00:13:35 +04:00
|
|
|
SAFE_FREE(path);
|
2010-12-28 16:57:42 +03:00
|
|
|
path=ssh_dirname(TORTURE_TEST_DIR "/test/");
|
2019-01-15 13:19:49 +03:00
|
|
|
assert_non_null(path);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(path, TORTURE_TEST_DIR);
|
2010-05-09 00:13:35 +04:00
|
|
|
SAFE_FREE(path);
|
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void torture_ntohll(void **state) {
|
2010-05-09 00:23:05 +04:00
|
|
|
uint64_t value = 0x0123456789abcdef;
|
2010-12-28 16:57:42 +03:00
|
|
|
uint32_t sample = 1;
|
|
|
|
unsigned char *ptr = (unsigned char *) &sample;
|
2010-05-09 00:23:05 +04:00
|
|
|
uint64_t check;
|
2010-12-28 16:57:42 +03:00
|
|
|
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
if (ptr[0] == 1){
|
2017-07-12 00:10:26 +03:00
|
|
|
/* we're in little endian */
|
|
|
|
check = 0xefcdab8967452301;
|
2010-05-09 00:22:06 +04:00
|
|
|
} else {
|
2017-07-12 00:10:26 +03:00
|
|
|
/* big endian */
|
|
|
|
check = value;
|
2010-05-09 00:22:06 +04:00
|
|
|
}
|
2010-12-28 16:57:42 +03:00
|
|
|
value = ntohll(value);
|
|
|
|
assert_true(value == check);
|
2010-05-09 00:22:06 +04:00
|
|
|
}
|
|
|
|
|
2011-01-01 21:23:38 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
static void torture_path_expand_tilde_win(void **state) {
|
|
|
|
char *d;
|
|
|
|
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
d = ssh_path_expand_tilde("~\\.ssh");
|
2019-01-15 15:45:10 +03:00
|
|
|
assert_non_null(d);
|
2011-01-01 21:23:38 +03:00
|
|
|
print_message("Expanded path: %s\n", d);
|
|
|
|
free(d);
|
|
|
|
|
|
|
|
d = ssh_path_expand_tilde("/guru/meditation");
|
|
|
|
assert_string_equal(d, "/guru/meditation");
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
|
|
|
static void torture_path_expand_tilde_unix(void **state) {
|
2010-05-11 00:47:21 +04:00
|
|
|
char h[256];
|
|
|
|
char *d;
|
2015-02-12 13:26:50 +03:00
|
|
|
char *user;
|
|
|
|
char *home;
|
2010-05-11 00:47:21 +04:00
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
(void) state;
|
|
|
|
|
2015-02-12 13:26:50 +03:00
|
|
|
user = getenv("USER");
|
|
|
|
if (user == NULL){
|
|
|
|
user = getenv("LOGNAME");
|
|
|
|
}
|
2018-04-12 18:03:06 +03:00
|
|
|
/* in certain CIs there no such variables */
|
|
|
|
if (!user){
|
|
|
|
struct passwd *pw = getpwuid(getuid());
|
|
|
|
if (pw){
|
|
|
|
user = pw->pw_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 13:26:50 +03:00
|
|
|
home = getenv("HOME");
|
|
|
|
assert_non_null(home);
|
|
|
|
snprintf(h, 256 - 1, "%s/.ssh", home);
|
2010-05-11 00:47:21 +04:00
|
|
|
|
|
|
|
d = ssh_path_expand_tilde("~/.ssh");
|
2015-02-12 13:26:50 +03:00
|
|
|
assert_non_null(d);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(d, h);
|
2010-05-11 00:47:21 +04:00
|
|
|
free(d);
|
|
|
|
|
|
|
|
d = ssh_path_expand_tilde("/guru/meditation");
|
2015-02-12 13:26:50 +03:00
|
|
|
assert_non_null(d);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(d, "/guru/meditation");
|
2010-05-11 00:47:21 +04:00
|
|
|
free(d);
|
|
|
|
|
2015-02-12 13:26:50 +03:00
|
|
|
snprintf(h, 256 - 1, "~%s/.ssh", user);
|
2010-05-11 00:47:21 +04:00
|
|
|
d = ssh_path_expand_tilde(h);
|
2015-02-12 13:26:50 +03:00
|
|
|
assert_non_null(d);
|
2010-05-11 00:47:21 +04:00
|
|
|
|
2015-02-12 13:26:50 +03:00
|
|
|
snprintf(h, 256 - 1, "%s/.ssh", home);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(d, h);
|
2010-05-11 00:47:21 +04:00
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
2011-01-01 21:23:38 +03:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void torture_path_expand_escape(void **state) {
|
|
|
|
ssh_session session = *state;
|
2022-08-29 11:08:58 +03:00
|
|
|
const char *s = "%d/%h/%p/by/%r";
|
2010-05-11 03:12:08 +04:00
|
|
|
char *e;
|
|
|
|
|
2012-02-05 14:50:49 +04:00
|
|
|
session->opts.sshdir = strdup("guru");
|
|
|
|
session->opts.host = strdup("meditation");
|
2022-08-29 11:08:58 +03:00
|
|
|
session->opts.port = 0;
|
2012-02-05 14:50:49 +04:00
|
|
|
session->opts.username = strdup("root");
|
2010-05-11 03:12:08 +04:00
|
|
|
|
|
|
|
e = ssh_path_expand_escape(session, s);
|
2015-02-12 13:26:50 +03:00
|
|
|
assert_non_null(e);
|
2022-08-29 11:08:58 +03:00
|
|
|
assert_string_equal(e, "guru/meditation/22/by/root");
|
|
|
|
ssh_string_free_char(e);
|
|
|
|
|
|
|
|
session->opts.port = 222;
|
|
|
|
|
|
|
|
e = ssh_path_expand_escape(session, s);
|
|
|
|
assert_non_null(e);
|
|
|
|
assert_string_equal(e, "guru/meditation/222/by/root");
|
|
|
|
ssh_string_free_char(e);
|
2010-05-11 03:12:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void torture_path_expand_known_hosts(void **state) {
|
|
|
|
ssh_session session = *state;
|
2010-05-31 11:17:54 +04:00
|
|
|
char *tmp;
|
|
|
|
|
2012-02-05 14:50:49 +04:00
|
|
|
session->opts.sshdir = strdup("/home/guru/.ssh");
|
2010-05-31 11:17:54 +04:00
|
|
|
|
|
|
|
tmp = ssh_path_expand_escape(session, "%d/known_hosts");
|
2015-02-12 13:26:50 +03:00
|
|
|
assert_non_null(tmp);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(tmp, "/home/guru/.ssh/known_hosts");
|
2010-05-31 11:17:54 +04:00
|
|
|
free(tmp);
|
|
|
|
}
|
2010-03-02 15:47:14 +03:00
|
|
|
|
2019-07-30 17:02:46 +03:00
|
|
|
static void torture_path_expand_percent(void **state) {
|
|
|
|
ssh_session session = *state;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
session->opts.sshdir = strdup("/home/guru/.ssh");
|
|
|
|
|
|
|
|
tmp = ssh_path_expand_escape(session, "%d/config%%1");
|
|
|
|
assert_non_null(tmp);
|
|
|
|
assert_string_equal(tmp, "/home/guru/.ssh/config%1");
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
|
2011-05-25 01:26:18 +04:00
|
|
|
static void torture_timeout_elapsed(void **state){
|
|
|
|
struct ssh_timestamp ts;
|
|
|
|
(void) state;
|
|
|
|
ssh_timestamp_init(&ts);
|
2021-05-26 18:06:42 +03:00
|
|
|
usleep(30000);
|
|
|
|
|
2011-05-25 01:26:18 +04:00
|
|
|
assert_true(ssh_timeout_elapsed(&ts,25));
|
|
|
|
assert_false(ssh_timeout_elapsed(&ts,30000));
|
|
|
|
assert_false(ssh_timeout_elapsed(&ts,75));
|
|
|
|
assert_true(ssh_timeout_elapsed(&ts,0));
|
|
|
|
assert_false(ssh_timeout_elapsed(&ts,-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_timeout_update(void **state){
|
|
|
|
struct ssh_timestamp ts;
|
|
|
|
(void) state;
|
|
|
|
ssh_timestamp_init(&ts);
|
|
|
|
usleep(50000);
|
|
|
|
assert_int_equal(ssh_timeout_update(&ts,25), 0);
|
|
|
|
assert_in_range(ssh_timeout_update(&ts,30000),29000,29960);
|
|
|
|
assert_in_range(ssh_timeout_update(&ts,75),1,40);
|
|
|
|
assert_int_equal(ssh_timeout_update(&ts,0),0);
|
|
|
|
assert_int_equal(ssh_timeout_update(&ts,-1),-1);
|
|
|
|
}
|
|
|
|
|
2017-07-12 01:46:54 +03:00
|
|
|
static void torture_ssh_analyze_banner(void **state) {
|
|
|
|
int rc = 0;
|
|
|
|
ssh_session session = NULL;
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
#define reset_banner_test() \
|
|
|
|
do { \
|
|
|
|
rc = 0; \
|
|
|
|
ssh_free(session); \
|
|
|
|
session = ssh_new(); \
|
|
|
|
assert_non_null(session); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define assert_banner_rejected(is_server) \
|
|
|
|
do { \
|
2018-06-28 18:52:58 +03:00
|
|
|
rc = ssh_analyze_banner(session, is_server); \
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_not_equal(0, rc); \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
#define assert_client_banner_rejected(banner) \
|
|
|
|
do { \
|
|
|
|
reset_banner_test(); \
|
|
|
|
session->clientbanner = strdup(banner); \
|
|
|
|
assert_non_null(session->clientbanner); \
|
|
|
|
assert_banner_rejected(1 /*server*/); \
|
|
|
|
SAFE_FREE(session->clientbanner); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define assert_server_banner_rejected(banner) \
|
|
|
|
do { \
|
|
|
|
reset_banner_test(); \
|
|
|
|
session->serverbanner = strdup(banner); \
|
|
|
|
assert_non_null(session->serverbanner); \
|
|
|
|
assert_banner_rejected(0 /*client*/); \
|
|
|
|
SAFE_FREE(session->serverbanner); \
|
|
|
|
} while (0)
|
|
|
|
|
2018-06-28 18:52:58 +03:00
|
|
|
#define assert_banner_accepted(is_server) \
|
2017-07-12 01:46:54 +03:00
|
|
|
do { \
|
2018-06-28 18:52:58 +03:00
|
|
|
rc = ssh_analyze_banner(session, is_server); \
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(0, rc); \
|
|
|
|
} while (0)
|
|
|
|
|
2018-06-28 18:52:58 +03:00
|
|
|
#define assert_client_banner_accepted(banner) \
|
2017-07-12 01:46:54 +03:00
|
|
|
do { \
|
|
|
|
reset_banner_test(); \
|
|
|
|
session->clientbanner = strdup(banner); \
|
|
|
|
assert_non_null(session->clientbanner); \
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_banner_accepted(1 /*server*/); \
|
2017-07-12 01:46:54 +03:00
|
|
|
SAFE_FREE(session->clientbanner); \
|
|
|
|
} while (0)
|
|
|
|
|
2018-06-28 18:52:58 +03:00
|
|
|
#define assert_server_banner_accepted(banner) \
|
2017-07-12 01:46:54 +03:00
|
|
|
do { \
|
|
|
|
reset_banner_test(); \
|
|
|
|
session->serverbanner = strdup(banner); \
|
|
|
|
assert_non_null(session->serverbanner); \
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_banner_accepted(0 /*client*/); \
|
2017-07-12 01:46:54 +03:00
|
|
|
SAFE_FREE(session->serverbanner); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* no banner is set */
|
|
|
|
reset_banner_test();
|
|
|
|
assert_banner_rejected(0 /*client*/);
|
|
|
|
reset_banner_test();
|
|
|
|
assert_banner_rejected(1 /*server*/);
|
|
|
|
|
|
|
|
/* banner is too short */
|
|
|
|
assert_client_banner_rejected("abc");
|
|
|
|
assert_server_banner_rejected("abc");
|
|
|
|
|
|
|
|
/* banner doesn't start "SSH-" */
|
|
|
|
assert_client_banner_rejected("abc-2.0");
|
|
|
|
assert_server_banner_rejected("abc-2.0");
|
|
|
|
|
|
|
|
/* SSH v1 */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_rejected("SSH-1.0");
|
|
|
|
assert_server_banner_rejected("SSH-1.0");
|
2017-07-12 01:46:54 +03:00
|
|
|
|
|
|
|
/* SSH v1.9 gets counted as both v1 and v2 */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-1.9");
|
|
|
|
assert_server_banner_accepted("SSH-1.9");
|
2017-07-12 01:46:54 +03:00
|
|
|
|
|
|
|
/* SSH v2 */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0");
|
|
|
|
assert_server_banner_accepted("SSH-2.0");
|
2017-07-12 01:46:54 +03:00
|
|
|
|
|
|
|
/* OpenSSH banners: too short to extract major and minor versions */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
|
|
|
|
|
|
|
/* OpenSSH banners: big enough to extract major and minor versions */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH_5.9p1");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(SSH_VERSION_INT(5, 9, 0), session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH_5.9p1");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(SSH_VERSION_INT(5, 9, 0), session->openssh);
|
|
|
|
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH_1.99");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(SSH_VERSION_INT(1, 99, 0), session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH_1.99");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(SSH_VERSION_INT(1, 99, 0), session->openssh);
|
|
|
|
|
2017-07-12 02:34:18 +03:00
|
|
|
/* OpenSSH banners: major, minor version limits result in zero */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH_0.99p1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH_0.99p1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH_1.101p1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH_1.101p1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2017-07-12 01:46:54 +03:00
|
|
|
|
2017-07-12 02:34:18 +03:00
|
|
|
/* OpenSSH banners: bogus major results in zero */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH_X.9p1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH_X.9p1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2017-07-12 01:46:54 +03:00
|
|
|
|
2017-07-12 02:34:18 +03:00
|
|
|
/* OpenSSH banners: bogus minor results in zero */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH_5.Yp1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH_5.Yp1");
|
2017-07-12 02:34:18 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2017-07-12 01:46:54 +03:00
|
|
|
|
|
|
|
/* OpenSSH banners: ssh-keyscan(1) */
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_client_banner_accepted("SSH-2.0-OpenSSH-keyscan");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
2018-06-28 18:52:58 +03:00
|
|
|
assert_server_banner_accepted("SSH-2.0-OpenSSH-keyscan");
|
2017-07-12 01:46:54 +03:00
|
|
|
assert_int_equal(0, session->openssh);
|
|
|
|
|
|
|
|
ssh_free(session);
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:45:01 +03:00
|
|
|
static void torture_ssh_dir_writeable(UNUSED_PARAM(void **state))
|
|
|
|
{
|
|
|
|
char *tmp_dir = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
FILE *file = NULL;
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
tmp_dir = torture_make_temp_dir(template);
|
|
|
|
assert_non_null(tmp_dir);
|
|
|
|
|
|
|
|
rc = ssh_dir_writeable(tmp_dir);
|
|
|
|
assert_int_equal(rc, 1);
|
|
|
|
|
|
|
|
/* Create a file */
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/a", tmp_dir);
|
|
|
|
|
|
|
|
file = fopen(buffer, "w");
|
|
|
|
assert_non_null(file);
|
|
|
|
|
|
|
|
fprintf(file, "Hello world!\n");
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
/* Negative test for checking a normal file */
|
|
|
|
rc = ssh_dir_writeable(buffer);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
/* Negative test for non existent file */
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/b", tmp_dir);
|
|
|
|
rc = ssh_dir_writeable(buffer);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
/* Negative test for directory without write permission */
|
|
|
|
rc = ssh_mkdir(buffer, 0400);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
|
|
|
|
rc = ssh_dir_writeable(buffer);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
torture_rmdirs(tmp_dir);
|
|
|
|
|
|
|
|
SAFE_FREE(tmp_dir);
|
|
|
|
}
|
|
|
|
|
2019-07-31 16:15:22 +03:00
|
|
|
static void torture_ssh_mkdirs(UNUSED_PARAM(void **state))
|
|
|
|
{
|
|
|
|
char *tmp_dir = NULL;
|
|
|
|
char *cwd = NULL;
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
ssize_t count = 0;
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Get current working directory */
|
|
|
|
cwd = torture_get_current_working_dir();
|
|
|
|
assert_non_null(cwd);
|
|
|
|
|
|
|
|
/* Create a base disposable directory */
|
|
|
|
tmp_dir = torture_make_temp_dir(template);
|
|
|
|
assert_non_null(tmp_dir);
|
|
|
|
|
|
|
|
/* Create a single directory */
|
|
|
|
count = snprintf(buffer, sizeof(buffer), "%s/a", tmp_dir);
|
|
|
|
assert_return_code(count, errno);
|
|
|
|
|
|
|
|
rc = ssh_mkdirs(buffer, 0700);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
|
|
|
|
rc = ssh_dir_writeable(buffer);
|
|
|
|
assert_int_equal(rc, 1);
|
|
|
|
|
|
|
|
/* Create directories recursively */
|
|
|
|
count = snprintf(buffer, sizeof(buffer), "%s/b/c/d", tmp_dir);
|
|
|
|
assert_return_code(count, errno);
|
|
|
|
|
|
|
|
rc = ssh_mkdirs(buffer, 0700);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
|
|
|
|
rc = ssh_dir_writeable(buffer);
|
|
|
|
assert_int_equal(rc, 1);
|
|
|
|
|
|
|
|
/* Change directory */
|
|
|
|
rc = torture_change_dir(tmp_dir);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
|
|
|
|
/* Create single local directory */
|
|
|
|
rc = ssh_mkdirs("e", 0700);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
|
|
|
|
rc = ssh_dir_writeable("e");
|
|
|
|
assert_int_equal(rc, 1);
|
|
|
|
|
|
|
|
/* Create local directories recursively */
|
|
|
|
rc = ssh_mkdirs("f/g/h", 0700);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
|
|
|
|
rc = ssh_dir_writeable("f/g/h");
|
|
|
|
assert_int_equal(rc, 1);
|
|
|
|
|
|
|
|
/* Negative test for creating "." directory */
|
|
|
|
rc = ssh_mkdirs(".", 0700);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EINVAL);
|
|
|
|
|
|
|
|
/* Negative test for creating "/" directory */
|
|
|
|
rc = ssh_mkdirs("/", 0700);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EINVAL);
|
|
|
|
|
|
|
|
/* Negative test for creating "" directory */
|
|
|
|
rc = ssh_mkdirs("", 0700);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EINVAL);
|
|
|
|
|
|
|
|
/* Negative test for creating NULL directory */
|
|
|
|
rc = ssh_mkdirs(NULL, 0700);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EINVAL);
|
|
|
|
|
|
|
|
/* Negative test for creating existing directory */
|
|
|
|
rc = ssh_mkdirs("a", 0700);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EEXIST);
|
|
|
|
|
|
|
|
/* Return to original directory */
|
|
|
|
rc = torture_change_dir(cwd);
|
|
|
|
assert_return_code(rc, errno);
|
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
torture_rmdirs(tmp_dir);
|
|
|
|
|
|
|
|
SAFE_FREE(tmp_dir);
|
|
|
|
SAFE_FREE(cwd);
|
|
|
|
}
|
|
|
|
|
2019-10-22 20:45:13 +03:00
|
|
|
static void torture_ssh_quote_file_name(UNUSED_PARAM(void **state))
|
|
|
|
{
|
|
|
|
char buffer[2048];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Only ordinary chars */
|
|
|
|
rc = ssh_quote_file_name("a b", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 5);
|
|
|
|
assert_string_equal(buffer, "'a b'");
|
|
|
|
|
|
|
|
/* Single quote in file name */
|
|
|
|
rc = ssh_quote_file_name("a'b", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 9);
|
|
|
|
assert_string_equal(buffer, "'a'\"'\"'b'");
|
|
|
|
|
|
|
|
/* Exclamation in file name */
|
|
|
|
rc = ssh_quote_file_name("a!b", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 8);
|
|
|
|
assert_string_equal(buffer, "'a'\\!'b'");
|
|
|
|
|
|
|
|
/* All together */
|
|
|
|
rc = ssh_quote_file_name("'a!b'", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 14);
|
|
|
|
assert_string_equal(buffer, "\"'\"'a'\\!'b'\"'\"");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("a'!b", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 11);
|
|
|
|
assert_string_equal(buffer, "'a'\"'\"\\!'b'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("a'$b", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 10);
|
|
|
|
assert_string_equal(buffer, "'a'\"'\"'$b'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("a'`b", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 10);
|
|
|
|
assert_string_equal(buffer, "'a'\"'\"'`b'");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name(" ", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 3);
|
|
|
|
assert_string_equal(buffer, "' '");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name(" ", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 4);
|
|
|
|
assert_string_equal(buffer, "' '");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\r", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 3);
|
|
|
|
assert_string_equal(buffer, "'\r'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\n", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 3);
|
|
|
|
assert_string_equal(buffer, "'\n'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\r\n", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 4);
|
|
|
|
assert_string_equal(buffer, "'\r\n'");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\\r", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 4);
|
|
|
|
assert_string_equal(buffer, "'\\r'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\\n", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 4);
|
|
|
|
assert_string_equal(buffer, "'\\n'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\\r\\n", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 6);
|
|
|
|
assert_string_equal(buffer, "'\\r\\n'");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\t", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 3);
|
|
|
|
assert_string_equal(buffer, "'\t'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\v", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 3);
|
|
|
|
assert_string_equal(buffer, "'\v'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\t\v", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 4);
|
|
|
|
assert_string_equal(buffer, "'\t\v'");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("'", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 3);
|
|
|
|
assert_string_equal(buffer, "\"'\"");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("''", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 4);
|
|
|
|
assert_string_equal(buffer, "\"''\"");
|
|
|
|
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\"", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 3);
|
|
|
|
assert_string_equal(buffer, "'\"'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\"\"", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 4);
|
|
|
|
assert_string_equal(buffer, "'\"\"'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("'\"", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 6);
|
|
|
|
assert_string_equal(buffer, "\"'\"'\"'");
|
|
|
|
|
|
|
|
rc = ssh_quote_file_name("\"'", buffer, 2048);
|
|
|
|
assert_int_equal(rc, 6);
|
|
|
|
assert_string_equal(buffer, "'\"'\"'\"");
|
|
|
|
|
|
|
|
|
|
|
|
/* Worst case */
|
|
|
|
rc = ssh_quote_file_name("a'b'", buffer, 3 * 4 + 1);
|
|
|
|
assert_int_equal(rc, 12);
|
|
|
|
assert_string_equal(buffer, "'a'\"'\"'b'\"'\"");
|
|
|
|
|
|
|
|
/* Negative tests */
|
|
|
|
|
|
|
|
/* NULL params */
|
|
|
|
rc = ssh_quote_file_name(NULL, buffer, 3 * 4 + 1);
|
|
|
|
assert_int_equal(rc, SSH_ERROR);
|
|
|
|
|
|
|
|
/* NULL params */
|
|
|
|
rc = ssh_quote_file_name("a b", NULL, 3 * 4 + 1);
|
|
|
|
assert_int_equal(rc, SSH_ERROR);
|
|
|
|
|
|
|
|
/* Small buffer size */
|
|
|
|
rc = ssh_quote_file_name("a b", buffer, 0);
|
|
|
|
assert_int_equal(rc, SSH_ERROR);
|
|
|
|
|
|
|
|
/* Worst case and small buffer size */
|
|
|
|
rc = ssh_quote_file_name("a'b'", buffer, 3 * 4);
|
|
|
|
assert_int_equal(rc, SSH_ERROR);
|
|
|
|
}
|
|
|
|
|
2019-11-04 17:35:15 +03:00
|
|
|
static void torture_ssh_newline_vis(UNUSED_PARAM(void **state))
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
rc = ssh_newline_vis("\n", buffer, 1024);
|
|
|
|
assert_int_equal(rc, 2);
|
|
|
|
assert_string_equal(buffer, "\\n");
|
|
|
|
|
|
|
|
rc = ssh_newline_vis("\n\n\n\n", buffer, 1024);
|
|
|
|
assert_int_equal(rc, 8);
|
|
|
|
assert_string_equal(buffer, "\\n\\n\\n\\n");
|
|
|
|
|
|
|
|
rc = ssh_newline_vis("a\nb\n", buffer, 1024);
|
|
|
|
assert_int_equal(rc, 6);
|
|
|
|
assert_string_equal(buffer, "a\\nb\\n");
|
|
|
|
}
|
|
|
|
|
2019-12-20 03:44:00 +03:00
|
|
|
static void torture_ssh_strreplace(void **state)
|
|
|
|
{
|
|
|
|
char test_string1[] = "this;is;a;test";
|
|
|
|
char test_string2[] = "test;is;a;this";
|
|
|
|
char test_string3[] = "this;test;is;a";
|
|
|
|
char *replaced_string = NULL;
|
|
|
|
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
/* pattern and replacement are of the same size */
|
|
|
|
replaced_string = ssh_strreplace(test_string1, "test", "kiwi");
|
|
|
|
assert_string_equal(replaced_string, "this;is;a;kiwi");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
replaced_string = ssh_strreplace(test_string2, "test", "kiwi");
|
|
|
|
assert_string_equal(replaced_string, "kiwi;is;a;this");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
replaced_string = ssh_strreplace(test_string3, "test", "kiwi");
|
|
|
|
assert_string_equal(replaced_string, "this;kiwi;is;a");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
/* replacement is greater than pattern */
|
|
|
|
replaced_string = ssh_strreplace(test_string1, "test", "an;apple");
|
|
|
|
assert_string_equal(replaced_string, "this;is;a;an;apple");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
replaced_string = ssh_strreplace(test_string2, "test", "an;apple");
|
|
|
|
assert_string_equal(replaced_string, "an;apple;is;a;this");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
replaced_string = ssh_strreplace(test_string3, "test", "an;apple");
|
|
|
|
assert_string_equal(replaced_string, "this;an;apple;is;a");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
/* replacement is less than pattern */
|
|
|
|
replaced_string = ssh_strreplace(test_string1, "test", "an");
|
|
|
|
assert_string_equal(replaced_string, "this;is;a;an");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
replaced_string = ssh_strreplace(test_string2, "test", "an");
|
|
|
|
assert_string_equal(replaced_string, "an;is;a;this");
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
replaced_string = ssh_strreplace(test_string3, "test", "an");
|
|
|
|
assert_string_equal(replaced_string, "this;an;is;a");
|
|
|
|
free(replaced_string);
|
2020-01-10 19:43:08 +03:00
|
|
|
|
|
|
|
/* pattern not found in teststring */
|
|
|
|
replaced_string = ssh_strreplace(test_string1, "banana", "an");
|
|
|
|
assert_string_equal(replaced_string, test_string1);
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
/* pattern is NULL */
|
|
|
|
replaced_string = ssh_strreplace(test_string1, NULL , "an");
|
|
|
|
assert_string_equal(replaced_string, test_string1);
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
/* replacement is NULL */
|
|
|
|
replaced_string = ssh_strreplace(test_string1, "test", NULL);
|
|
|
|
assert_string_equal(replaced_string, test_string1);
|
|
|
|
free(replaced_string);
|
|
|
|
|
|
|
|
/* src is NULL */
|
|
|
|
replaced_string = ssh_strreplace(NULL, "test", "kiwi");
|
|
|
|
assert_null(replaced_string);
|
2019-12-20 03:44:00 +03:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:03:49 +03:00
|
|
|
static void torture_ssh_strerror(void **state)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
size_t bufflen = sizeof(buf);
|
|
|
|
char *out = NULL;
|
|
|
|
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
out = ssh_strerror(ENOENT, buf, 1); /* too short */
|
|
|
|
assert_string_equal(out, "\0");
|
|
|
|
|
|
|
|
out = ssh_strerror(256, buf, bufflen); /* unknown error code */
|
|
|
|
/* This error is always different:
|
|
|
|
* Freebd: "Unknown error: 256"
|
|
|
|
* MinGW/Win: "Unknown error"
|
|
|
|
* Linux/glibc: "Unknown error 256"
|
|
|
|
* Alpine/musl: "No error information"
|
|
|
|
*/
|
|
|
|
assert_non_null(out);
|
|
|
|
|
|
|
|
out = ssh_strerror(ENOMEM, buf, bufflen);
|
|
|
|
/* This actually differs too for glibc/musl:
|
|
|
|
* musl: "Out of memory"
|
|
|
|
* everything else: "Cannot allocate memory"
|
|
|
|
*/
|
|
|
|
assert_non_null(out);
|
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
int torture_run_tests(void) {
|
2011-01-02 19:25:51 +03:00
|
|
|
int rc;
|
2015-09-07 11:39:51 +03:00
|
|
|
struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test(torture_get_user_home_dir),
|
|
|
|
cmocka_unit_test(torture_basename),
|
|
|
|
cmocka_unit_test(torture_dirname),
|
|
|
|
cmocka_unit_test(torture_ntohll),
|
2011-01-01 21:23:38 +03:00
|
|
|
#ifdef _WIN32
|
2015-09-07 11:39:51 +03:00
|
|
|
cmocka_unit_test(torture_path_expand_tilde_win),
|
2011-01-01 21:23:38 +03:00
|
|
|
#else
|
2015-09-07 11:39:51 +03:00
|
|
|
cmocka_unit_test(torture_path_expand_tilde_unix),
|
2011-01-01 21:23:38 +03:00
|
|
|
#endif
|
2015-09-07 11:39:51 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_path_expand_escape, setup, teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_path_expand_known_hosts, setup, teardown),
|
2019-07-30 17:02:46 +03:00
|
|
|
cmocka_unit_test_setup_teardown(torture_path_expand_percent, setup, teardown),
|
2015-09-07 11:39:51 +03:00
|
|
|
cmocka_unit_test(torture_timeout_elapsed),
|
|
|
|
cmocka_unit_test(torture_timeout_update),
|
2017-07-12 01:46:54 +03:00
|
|
|
cmocka_unit_test(torture_ssh_analyze_banner),
|
2019-08-01 13:45:01 +03:00
|
|
|
cmocka_unit_test(torture_ssh_dir_writeable),
|
2019-11-04 17:35:15 +03:00
|
|
|
cmocka_unit_test(torture_ssh_newline_vis),
|
2019-07-31 16:15:22 +03:00
|
|
|
cmocka_unit_test(torture_ssh_mkdirs),
|
2019-10-22 20:45:13 +03:00
|
|
|
cmocka_unit_test(torture_ssh_quote_file_name),
|
2019-12-20 03:44:00 +03:00
|
|
|
cmocka_unit_test(torture_ssh_strreplace),
|
2022-08-29 15:03:49 +03:00
|
|
|
cmocka_unit_test(torture_ssh_strerror),
|
2010-12-28 16:57:42 +03:00
|
|
|
};
|
|
|
|
|
2011-01-02 19:25:51 +03:00
|
|
|
ssh_init();
|
2014-09-02 11:07:17 +04:00
|
|
|
torture_filter_tests(tests);
|
2015-09-07 11:39:51 +03:00
|
|
|
rc = cmocka_run_group_tests(tests, NULL, NULL);
|
2011-01-02 19:25:51 +03:00
|
|
|
ssh_finalize();
|
|
|
|
return rc;
|
2010-12-28 16:57:42 +03:00
|
|
|
}
|