2011-01-13 20:09:04 +03:00
|
|
|
/*
|
|
|
|
* This file is part of the SSH Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 by Aris Adamantiadis
|
|
|
|
*
|
|
|
|
* The SSH Library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SSH Library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with the SSH Library; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
|
|
* MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2017-10-28 15:31:37 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-01-13 20:09:04 +03:00
|
|
|
#define LIBSSH_STATIC
|
|
|
|
|
|
|
|
#include "torture.h"
|
|
|
|
#include <libssh/libssh.h>
|
2016-11-06 13:52:12 +03:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2011-05-25 01:25:40 +04:00
|
|
|
#include <sys/time.h>
|
2016-11-06 13:52:12 +03:00
|
|
|
#endif /* HAVE_SYS_TIME_H */
|
2014-03-17 16:53:23 +04:00
|
|
|
#include <arpa/inet.h>
|
2015-02-15 00:09:02 +03:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2016-02-02 13:26:53 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
2014-10-13 12:00:25 +04:00
|
|
|
|
2011-05-25 01:25:40 +04:00
|
|
|
/* Should work until Apnic decides to assign it :) */
|
|
|
|
#define BLACKHOLE "1.1.1.1"
|
2011-01-13 20:09:04 +03:00
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
static int sshd_setup(void **state)
|
|
|
|
{
|
|
|
|
torture_setup_sshd_server(state);
|
2011-01-13 20:09:04 +03:00
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2011-01-13 20:09:04 +03:00
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
static int sshd_teardown(void **state) {
|
|
|
|
torture_teardown_sshd_server(state);
|
2015-09-07 11:39:51 +03:00
|
|
|
|
|
|
|
return 0;
|
2011-01-13 20:09:04 +03:00
|
|
|
}
|
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
static int session_setup(void **state)
|
|
|
|
{
|
|
|
|
struct torture_state *s = *state;
|
|
|
|
int verbosity = torture_libssh_verbosity();
|
2016-02-02 13:26:53 +03:00
|
|
|
struct passwd *pwd;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
pwd = getpwnam("bob");
|
|
|
|
assert_non_null(pwd);
|
|
|
|
|
|
|
|
rc = setuid(pwd->pw_uid);
|
|
|
|
assert_return_code(rc, errno);
|
2014-10-13 12:00:25 +04:00
|
|
|
|
|
|
|
s->ssh.session = ssh_new();
|
|
|
|
assert_non_null(s->ssh.session);
|
|
|
|
|
|
|
|
ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
|
2015-09-07 11:39:51 +03:00
|
|
|
|
|
|
|
return 0;
|
2011-01-13 20:09:04 +03:00
|
|
|
}
|
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
static int session_teardown(void **state)
|
|
|
|
{
|
|
|
|
struct torture_state *s = *state;
|
2011-01-13 20:09:04 +03:00
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
ssh_disconnect(s->ssh.session);
|
|
|
|
ssh_free(s->ssh.session);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void torture_connect_nonblocking(void **state) {
|
|
|
|
struct torture_state *s = *state;
|
|
|
|
ssh_session session = s->ssh.session;
|
2011-01-13 20:09:04 +03:00
|
|
|
int rc;
|
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_ssh_return_code(session, rc);
|
2011-01-13 20:09:04 +03:00
|
|
|
ssh_set_blocking(session,0);
|
|
|
|
|
|
|
|
do {
|
2014-03-14 16:13:00 +04:00
|
|
|
rc = ssh_connect(session);
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_ssh_return_code_not_equal(session, rc, SSH_ERROR);
|
2011-01-13 20:09:04 +03:00
|
|
|
} while(rc == SSH_AGAIN);
|
|
|
|
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_ssh_return_code(session, rc);
|
2011-01-13 20:09:04 +03:00
|
|
|
}
|
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
#if 0 /* This does not work with socket_wrapper */
|
2011-05-25 01:25:40 +04:00
|
|
|
static void torture_connect_timeout(void **state) {
|
2014-10-13 12:00:25 +04:00
|
|
|
struct torture_state *s = *state;
|
|
|
|
ssh_session session = s->ssh.session;
|
2011-05-25 01:25:40 +04:00
|
|
|
struct timeval before, after;
|
|
|
|
int rc;
|
|
|
|
long timeout = 2;
|
|
|
|
time_t sec;
|
|
|
|
suseconds_t usec;
|
|
|
|
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HOST, BLACKHOLE);
|
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout);
|
|
|
|
assert_true(rc == SSH_OK);
|
|
|
|
|
|
|
|
rc = gettimeofday(&before, NULL);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
rc = ssh_connect(session);
|
|
|
|
assert_true(rc == SSH_ERROR);
|
|
|
|
rc = gettimeofday(&after, NULL);
|
|
|
|
assert_true(rc == 0);
|
|
|
|
sec = after.tv_sec - before.tv_sec;
|
|
|
|
usec = after.tv_usec - before.tv_usec;
|
|
|
|
/* Borrow a second for the missing usecs, but don't bother calculating */
|
2014-03-14 16:13:00 +04:00
|
|
|
if (usec < 0)
|
2011-05-25 01:25:40 +04:00
|
|
|
sec--;
|
2014-03-14 16:13:00 +04:00
|
|
|
assert_in_range(sec, 1, 3);
|
2011-05-25 01:25:40 +04:00
|
|
|
}
|
2014-10-13 12:00:25 +04:00
|
|
|
#endif
|
2011-05-25 01:25:40 +04:00
|
|
|
|
2011-01-13 20:09:04 +03:00
|
|
|
static void torture_connect_double(void **state) {
|
2014-10-13 12:00:25 +04:00
|
|
|
struct torture_state *s = *state;
|
|
|
|
ssh_session session = s->ssh.session;
|
2011-01-13 20:09:04 +03:00
|
|
|
|
|
|
|
int rc;
|
|
|
|
|
2014-10-13 12:00:25 +04:00
|
|
|
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_ssh_return_code(session, rc);
|
|
|
|
|
2011-01-13 20:09:04 +03:00
|
|
|
rc = ssh_connect(session);
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_ssh_return_code(session, rc);
|
2011-01-13 20:09:04 +03:00
|
|
|
ssh_disconnect(session);
|
|
|
|
|
|
|
|
rc = ssh_connect(session);
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_ssh_return_code(session, rc);
|
2011-01-13 20:09:04 +03:00
|
|
|
}
|
|
|
|
|
2014-03-14 16:13:00 +04:00
|
|
|
static void torture_connect_failure(void **state) {
|
2011-01-16 00:50:34 +03:00
|
|
|
/*
|
|
|
|
* The intent of this test is to check that a fresh
|
|
|
|
* ssh_new/ssh_disconnect/ssh_free sequence doesn't crash/leak
|
|
|
|
* and the behavior of a double ssh_disconnect
|
|
|
|
*/
|
2014-10-13 12:00:25 +04:00
|
|
|
struct torture_state *s = *state;
|
|
|
|
ssh_session session = s->ssh.session;
|
|
|
|
|
2011-01-16 00:50:34 +03:00
|
|
|
ssh_disconnect(session);
|
|
|
|
}
|
2014-03-14 16:13:00 +04:00
|
|
|
|
2014-03-17 16:53:23 +04:00
|
|
|
static void torture_connect_socket(void **state) {
|
2014-10-13 12:00:25 +04:00
|
|
|
struct torture_state *s = *state;
|
|
|
|
ssh_session session = s->ssh.session;
|
2014-03-17 16:53:23 +04:00
|
|
|
|
|
|
|
int rc;
|
|
|
|
int sock_fd = 0;
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
|
|
|
|
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
assert_true(sock_fd > 0);
|
|
|
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
server_addr.sin_port = htons(22);
|
2014-10-13 12:00:25 +04:00
|
|
|
server_addr.sin_addr.s_addr = inet_addr(TORTURE_SSH_SERVER);
|
2014-03-17 16:53:23 +04:00
|
|
|
|
2014-04-09 18:16:44 +04:00
|
|
|
rc = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_return_code(rc, errno);
|
2014-03-17 16:53:23 +04:00
|
|
|
|
|
|
|
ssh_options_set(session, SSH_OPTIONS_FD, &sock_fd);
|
|
|
|
|
|
|
|
rc = ssh_connect(session);
|
2018-06-30 14:49:14 +03:00
|
|
|
assert_ssh_return_code(session, rc);
|
2014-03-17 16:53:23 +04:00
|
|
|
}
|
|
|
|
|
2011-01-13 20:09:04 +03:00
|
|
|
int torture_run_tests(void) {
|
|
|
|
int rc;
|
2015-09-07 11:39:51 +03:00
|
|
|
struct CMUnitTest tests[] = {
|
2014-10-13 12:00:25 +04:00
|
|
|
cmocka_unit_test_setup_teardown(torture_connect_nonblocking, session_setup, session_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_connect_double, session_setup, session_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(torture_connect_failure, session_setup, session_teardown),
|
|
|
|
#if 0
|
|
|
|
cmocka_unit_test_setup_teardown(torture_connect_timeout, session_setup, session_teardown),
|
|
|
|
#endif
|
|
|
|
cmocka_unit_test_setup_teardown(torture_connect_socket, session_setup, session_teardown),
|
2011-01-13 20:09:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
ssh_init();
|
|
|
|
|
2014-09-02 11:07:17 +04:00
|
|
|
torture_filter_tests(tests);
|
2014-10-13 12:00:25 +04:00
|
|
|
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
|
2011-01-13 20:09:04 +03:00
|
|
|
|
|
|
|
ssh_finalize();
|
|
|
|
return rc;
|
|
|
|
}
|