diff options
author | Jakub Jelen <jjelen@redhat.com> | 2021-03-16 18:07:12 +0100 |
---|---|---|
committer | Jakub Jelen <jjelen@redhat.com> | 2021-08-17 15:46:54 +0200 |
commit | d8fea02d2bbdf5053ea8a6dc88241dfc69a4f91a (patch) | |
tree | bbbbd3d0f9286bcfa3616d1acbbc072439d53d9b | |
parent | 265b826f78a5db8b6336c3a19195182ad3df6556 (diff) | |
download | libssh-d8fea02d2bbdf5053ea8a6dc88241dfc69a4f91a.tar.gz libssh-d8fea02d2bbdf5053ea8a6dc88241dfc69a4f91a.tar.xz libssh-d8fea02d2bbdf5053ea8a6dc88241dfc69a4f91a.zip |
tests: Cover sftp_new_channel function
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Sahana Prasad <sahana@redhat.com>
(cherry picked from commit 25f9ca83a4fcfe305c557cc05d10c7b03e77b4e8)
-rw-r--r-- | tests/client/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/client/torture_sftp_init.c | 106 | ||||
-rw-r--r-- | tests/torture.c | 31 | ||||
-rw-r--r-- | tests/torture.h | 1 |
4 files changed, 135 insertions, 4 deletions
diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt index 70b5de3e..14f24c26 100644 --- a/tests/client/CMakeLists.txt +++ b/tests/client/CMakeLists.txt @@ -37,6 +37,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 <sys/types.h> +#include <pwd.h> +#include <errno.h> + +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 9d8a88f5..b3ea36b4 100644 --- a/tests/torture.c +++ b/tests/torture.c @@ -421,7 +421,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; @@ -437,9 +438,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); @@ -470,6 +488,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 a1dc6208..63ddc3cd 100644 --- a/tests/torture.h +++ b/tests/torture.h @@ -111,6 +111,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); |