diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt index d99dd63f..cce25d20 100644 --- a/tests/client/CMakeLists.txt +++ b/tests/client/CMakeLists.txt @@ -18,6 +18,7 @@ if (WITH_SFTP) set(LIBSSH_CLIENT_TESTS ${LIBSSH_CLIENT_TESTS} torture_sftp_ext + torture_sftp_canonicalize_path torture_sftp_dir torture_sftp_read torture_sftp_fsync) diff --git a/tests/client/torture_sftp_canonicalize_path.c b/tests/client/torture_sftp_canonicalize_path.c new file mode 100644 index 00000000..e9f85606 --- /dev/null +++ b/tests/client/torture_sftp_canonicalize_path.c @@ -0,0 +1,96 @@ +#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); + + return 0; +} + +static int sshd_teardown(void **state) { + torture_teardown_sshd_server(state); + + return 0; +} + +static int 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(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); + + return 0; +} + +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; +} + +static void torture_sftp_canonicalize_path(void **state) +{ + struct torture_state *s = *state; + struct torture_sftp *t = s->ssh.tsftp; + struct passwd *pwd = NULL; + char *canonicalized_path = NULL; + + pwd = getpwnam(TORTURE_SSH_USER_ALICE); + assert_non_null(pwd); + + canonicalized_path = sftp_canonicalize_path(t->sftp, "."); + assert_non_null(canonicalized_path); + + assert_string_equal(canonicalized_path, pwd->pw_dir); + + SSH_STRING_FREE_CHAR(canonicalized_path); +} + +int torture_run_tests(void) { + int rc; + struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(torture_sftp_canonicalize_path, + session_setup, + session_teardown) + }; + + ssh_init(); + + torture_filter_tests(tests); + rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); + + ssh_finalize(); + + return rc; +} +