2011-01-11 19:48:48 +03:00
|
|
|
|
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 "torture.h"
|
|
|
|
#include "misc.c"
|
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
|
|
|
|
2010-05-11 03:12:08 +04:00
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void setup(void **state) {
|
|
|
|
ssh_session session = ssh_new();
|
|
|
|
*state = session;
|
2010-05-11 03:12:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
static void teardown(void **state) {
|
|
|
|
ssh_free(*state);
|
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();
|
2010-12-29 22:35:55 +03:00
|
|
|
assert_false(user == NULL);
|
|
|
|
#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");
|
|
|
|
assert_true(path != NULL);
|
|
|
|
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/");
|
|
|
|
assert_true(path != NULL);
|
|
|
|
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");
|
|
|
|
assert_true(path != NULL);
|
|
|
|
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/");
|
|
|
|
assert_true(path != NULL);
|
|
|
|
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){
|
2010-05-09 00:22:06 +04:00
|
|
|
/* we're in little endian */
|
|
|
|
check = 0xefcdab8967452301;
|
|
|
|
} else {
|
|
|
|
/* big endian */
|
|
|
|
check = value;
|
|
|
|
}
|
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");
|
|
|
|
assert_false(d == NULL);
|
|
|
|
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;
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
(void) state;
|
|
|
|
|
2010-05-11 00:47:21 +04:00
|
|
|
snprintf(h, 256 - 1, "%s/.ssh", getenv("HOME"));
|
|
|
|
|
|
|
|
d = ssh_path_expand_tilde("~/.ssh");
|
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");
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(d, "/guru/meditation");
|
2010-05-11 00:47:21 +04:00
|
|
|
free(d);
|
|
|
|
|
|
|
|
snprintf(h, 256 - 1, "~%s/.ssh", getenv("USER"));
|
|
|
|
d = ssh_path_expand_tilde(h);
|
|
|
|
|
|
|
|
snprintf(h, 256 - 1, "%s/.ssh", getenv("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;
|
2010-05-11 03:12:08 +04:00
|
|
|
const char *s = "%d/%h/by/%r";
|
|
|
|
char *e;
|
|
|
|
|
2010-06-03 14:24:53 +04:00
|
|
|
session->sshdir = strdup("guru");
|
|
|
|
session->host = strdup("meditation");
|
|
|
|
session->username = strdup("root");
|
2010-05-11 03:12:08 +04:00
|
|
|
|
|
|
|
e = ssh_path_expand_escape(session, s);
|
2010-12-28 16:57:42 +03:00
|
|
|
assert_string_equal(e, "guru/meditation/by/root");
|
2010-05-11 03:12:08 +04:00
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2010-06-03 14:24:53 +04:00
|
|
|
session->sshdir = strdup("/home/guru/.ssh");
|
2010-05-31 11:17:54 +04:00
|
|
|
|
|
|
|
tmp = ssh_path_expand_escape(session, "%d/known_hosts");
|
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
|
|
|
|
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);
|
|
|
|
usleep(50000);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-12-28 16:57:42 +03:00
|
|
|
int torture_run_tests(void) {
|
2011-01-02 19:25:51 +03:00
|
|
|
int rc;
|
2010-12-28 16:57:42 +03:00
|
|
|
const UnitTest tests[] = {
|
|
|
|
unit_test(torture_get_user_home_dir),
|
|
|
|
unit_test(torture_basename),
|
|
|
|
unit_test(torture_dirname),
|
|
|
|
unit_test(torture_ntohll),
|
2011-01-01 21:23:38 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
unit_test(torture_path_expand_tilde_win),
|
|
|
|
#else
|
|
|
|
unit_test(torture_path_expand_tilde_unix),
|
|
|
|
#endif
|
2010-12-28 16:57:42 +03:00
|
|
|
unit_test_setup_teardown(torture_path_expand_escape, setup, teardown),
|
|
|
|
unit_test_setup_teardown(torture_path_expand_known_hosts, setup, teardown),
|
2011-05-25 01:26:18 +04:00
|
|
|
unit_test(torture_timeout_elapsed),
|
|
|
|
unit_test(torture_timeout_update),
|
2010-12-28 16:57:42 +03:00
|
|
|
};
|
|
|
|
|
2011-01-02 19:25:51 +03:00
|
|
|
ssh_init();
|
|
|
|
rc=run_tests(tests);
|
|
|
|
ssh_finalize();
|
|
|
|
return rc;
|
2010-12-28 16:57:42 +03:00
|
|
|
}
|