From 73c3d8965d2549440884a3c224dc9ee641da8a25 Mon Sep 17 00:00:00 2001 From: Eshan Kelkar Date: Thu, 6 Apr 2023 22:31:59 +0530 Subject: [PATCH] Add tests for sftp_hardlink For testing sftp_hardlink, torture_sftp_hardlink has been introduced in tests/client. Signed-off-by: Eshan Kelkar Reviewed-by: Jakub Jelen --- tests/client/CMakeLists.txt | 1 + tests/client/torture_sftp_hardlink.c | 114 +++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/client/torture_sftp_hardlink.c diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt index 4772efc9..bb9de65e 100644 --- a/tests/client/CMakeLists.txt +++ b/tests/client/CMakeLists.txt @@ -50,6 +50,7 @@ if (WITH_SFTP) torture_sftp_dir torture_sftp_read torture_sftp_fsync + torture_sftp_hardlink ${SFTP_BENCHMARK_TESTS}) endif (WITH_SFTP) diff --git a/tests/client/torture_sftp_hardlink.c b/tests/client/torture_sftp_hardlink.c new file mode 100644 index 00000000..37963636 --- /dev/null +++ b/tests/client/torture_sftp_hardlink.c @@ -0,0 +1,114 @@ +#define LIBSSH_STATIC + +#include "config.h" + +#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 int session_setup(void **state) +{ + struct torture_state *s = *state; + struct passwd *pwd = 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); + + 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_hardlink(void **state) +{ + struct torture_state *s = *state; + struct torture_sftp *t = s->ssh.tsftp; + + char link_1[128] = {0}; + char link_2[128] = {0}; + int fd; + int rc; + + snprintf(link_1, sizeof(link_1), + "%s/libssh_sftp_hardlink_test_1", t->testdir); + snprintf(link_2, sizeof(link_2), + "%s/libssh_sftp_hardlink_test_2", t->testdir); + + fd = open(link_1, O_CREAT, S_IRWXU); + assert_return_code(fd, errno); + close(fd); + + rc = sftp_hardlink(t->sftp, link_1, link_2); + assert_int_equal(rc, SSH_OK); + + /* check whether the file got associated with link_2 */ + rc = access(link_2, F_OK); + assert_int_equal(rc, 0); + + unlink(link_1); + unlink(link_2); + + /* + * try to create a hardlink for a file that does not + * exist, this should fail + */ + rc = sftp_hardlink(t->sftp, link_1, link_2); + assert_int_not_equal(rc, 0); +} + +int torture_run_tests(void) +{ + int rc; + struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(torture_sftp_hardlink, + session_setup, + session_teardown) + }; + + ssh_init(); + + torture_filter_tests(tests); + rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); + ssh_finalize(); + + return rc; +}