Debian Codegraph @

tests/client/torture_sftp_rename.c

raw
1 #define LIBSSH_STATIC
2
3 #include "config.h"
4
5 #include "torture.h"
6 #include "sftp.c"
7
8 #include <sys/types.h>
9 #include <pwd.h>
10 #include <errno.h>
11
12 static int sshd_setup(void **state)
13 {
14 torture_setup_sshd_server(state, false);
15 return 0;
16 }
17
18 static int sshd_teardown(void **state)
19 {
20 torture_teardown_sshd_server(state);
21 return 0;
22 }
23
24 static int session_setup(void **state)
25 {
26 struct torture_state *s = *state;
27 struct passwd *pwd = NULL;
28 int rc;
29
30 pwd = getpwnam("bob");
31 assert_non_null(pwd);
32
33 rc = setuid(pwd->pw_uid);
34 assert_return_code(rc, errno);
35
36 s->ssh.session = torture_ssh_session(s,
37 TORTURE_SSH_SERVER,
38 NULL,
39 TORTURE_SSH_USER_ALICE,
40 NULL);
41 assert_non_null(s->ssh.session);
42
43 s->ssh.tsftp = torture_sftp_session(s->ssh.session);
44 assert_non_null(s->ssh.tsftp);
45
46 return 0;
47 }
48
49 static int session_teardown(void **state)
50 {
51 struct torture_state *s = *state;
52
53 torture_rmdirs(s->ssh.tsftp->testdir);
54 torture_sftp_close(s->ssh.tsftp);
55 ssh_disconnect(s->ssh.session);
56 ssh_free(s->ssh.session);
57
58 return 0;
59 }
60
61 static void torture_sftp_rename(void **state)
62 {
63 struct torture_state *s = *state;
64 struct torture_sftp *t = s->ssh.tsftp;
65
66 char name_1[128] = {0};
67 char name_2[128] = {0};
68
69 int fd;
70 int rc;
71
72 snprintf(name_1, sizeof(name_1),
73 "%s/libssh_sftp_rename_test_1", t->testdir);
74 snprintf(name_2, sizeof(name_2),
75 "%s/libssh_sftp_rename_test_2", t->testdir);
76
77 fd = open(name_1, O_CREAT, S_IRWXU);
78 assert_return_code(fd, errno);
79 close(fd);
80
81 /* try to rename an existing file */
82 rc = sftp_rename(t->sftp, name_1, name_2);
83 assert_int_equal(rc, SSH_OK);
84
85 /* check whether any file with name_1 exists, it shouldn't */
86 rc = access(name_1, F_OK);
87 assert_int_not_equal(rc, 0);
88
89 /* check whether file with name_2 exists, it should */
90 rc = access(name_2, F_OK);
91 assert_int_equal(rc, 0);
92
93 unlink(name_2);
94
95 /*
96 * try to rename a file that does not exist,
97 * this should fail (-ve test case)
98 */
99 rc = sftp_rename(t->sftp, name_1, name_2);
100 assert_int_not_equal(rc, 0);
101 }
102
103
104 int torture_run_tests(void)
105 {
106 int rc;
107 struct CMUnitTest tests[] = {
108 cmocka_unit_test_setup_teardown(torture_sftp_rename,
109 session_setup,
110 session_teardown)
111 };
112
113 ssh_init();
114
115 torture_filter_tests(tests);
116 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
117 ssh_finalize();
118
119 return rc;
120 }