diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt index e6adfe36..71e5182e 100644 --- a/tests/client/CMakeLists.txt +++ b/tests/client/CMakeLists.txt @@ -43,6 +43,7 @@ if (WITH_SFTP) endif() set(LIBSSH_CLIENT_TESTS ${LIBSSH_CLIENT_TESTS} + torture_sftp_init torture_sftp_ext torture_sftp_canonicalize_path torture_sftp_dir diff --git a/tests/client/torture_sftp_init.c b/tests/client/torture_sftp_init.c new file mode 100644 index 00000000..a17f01fe --- /dev/null +++ b/tests/client/torture_sftp_init.c @@ -0,0 +1,106 @@ +#include "config.h" + +#define LIBSSH_STATIC + +#include "torture.h" +#include "sftp.c" + +#include +#include +#include + +static int sshd_setup(void **state) +{ + torture_setup_sshd_server(state, false); + + return 0; +} + +static int sshd_teardown(void **state) { + torture_teardown_sshd_server(state); + + return 0; +} + +static void session_setup(void **state) +{ + struct torture_state *s = *state; + struct passwd *pwd; + int rc; + + pwd = getpwnam("bob"); + assert_non_null(pwd); + + rc = setuid(pwd->pw_uid); + assert_return_code(rc, errno); + + s->ssh.session = torture_ssh_session(s, + TORTURE_SSH_SERVER, + NULL, + TORTURE_SSH_USER_ALICE, + NULL); + assert_non_null(s->ssh.session); + + s->ssh.tsftp = torture_sftp_session(s->ssh.session); + assert_non_null(s->ssh.tsftp); +} + +static void session_setup_channel(void **state) +{ + struct torture_state *s = *state; + struct passwd *pwd = NULL; + ssh_channel c = NULL; + int rc; + + pwd = getpwnam("bob"); + assert_non_null(pwd); + + rc = setuid(pwd->pw_uid); + assert_return_code(rc, errno); + + s->ssh.session = torture_ssh_session(s, + TORTURE_SSH_SERVER, + NULL, + TORTURE_SSH_USER_ALICE, + NULL); + assert_non_null(s->ssh.session); + + c = ssh_channel_new(s->ssh.session); + assert_non_null(c); + + s->ssh.tsftp = torture_sftp_session_channel(s->ssh.session, c); + assert_non_null(s->ssh.tsftp); +} + +static int session_teardown(void **state) +{ + struct torture_state *s = *state; + + torture_rmdirs(s->ssh.tsftp->testdir); + torture_sftp_close(s->ssh.tsftp); + ssh_disconnect(s->ssh.session); + ssh_free(s->ssh.session); + + return 0; +} + +int torture_run_tests(void) { + int rc; + struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(session_setup, + NULL, + session_teardown), + cmocka_unit_test_setup_teardown(session_setup_channel, + NULL, + session_teardown) + }; + + ssh_init(); + + torture_filter_tests(tests); + rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); + + ssh_finalize(); + + return rc; +} diff --git a/tests/torture.c b/tests/torture.c index 4ac1ec66..61209185 100644 --- a/tests/torture.c +++ b/tests/torture.c @@ -426,7 +426,8 @@ ssh_bind torture_ssh_bind(const char *addr, #ifdef WITH_SFTP -struct torture_sftp *torture_sftp_session(ssh_session session) { +struct torture_sftp *torture_sftp_session_channel(ssh_session session, ssh_channel channel) +{ struct torture_sftp *t; char template[] = "/tmp/ssh_torture_XXXXXX"; char *p; @@ -442,9 +443,26 @@ struct torture_sftp *torture_sftp_session(ssh_session session) { } t->ssh = session; - t->sftp = sftp_new(session); - if (t->sftp == NULL) { - goto failed; + if (channel == NULL) { + t->sftp = sftp_new(session); + if (t->sftp == NULL) { + goto failed; + } + } else { + t->sftp = sftp_new_channel(session, channel); + if (t->sftp == NULL) { + goto failed; + } + + rc = ssh_channel_open_session(channel); + if (rc != SSH_OK) { + goto failed; + } + + rc = ssh_channel_request_sftp(channel); + if (rc != SSH_OK) { + goto failed; + } } rc = sftp_init(t->sftp); @@ -475,6 +493,11 @@ failed: return NULL; } +struct torture_sftp *torture_sftp_session(ssh_session session) +{ + return torture_sftp_session_channel(session, NULL); +} + void torture_sftp_close(struct torture_sftp *t) { if (t == NULL) { return; diff --git a/tests/torture.h b/tests/torture.h index b9b87b6a..599b53ea 100644 --- a/tests/torture.h +++ b/tests/torture.h @@ -113,6 +113,7 @@ ssh_bind torture_ssh_bind(const char *addr, const char *private_key_file); struct torture_sftp *torture_sftp_session(ssh_session session); +struct torture_sftp *torture_sftp_session_channel(ssh_session session, ssh_channel channel); void torture_sftp_close(struct torture_sftp *t); void torture_write_file(const char *filename, const char *data);