Debian Codegraph @

tests/client/torture_sftp_expand_path.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
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
16 return 0;
17 }
18
19 static int sshd_teardown(void **state)
20 {
21 torture_teardown_sshd_server(state);
22
23 return 0;
24 }
25
26 static int session_setup(void **state)
27 {
28 struct torture_state *s = *state;
29 struct passwd *pwd = NULL;
30 int rc;
31
32 pwd = getpwnam("bob");
33 assert_non_null(pwd);
34
35 rc = setuid(pwd->pw_uid);
36 assert_return_code(rc, errno);
37
38 s->ssh.session = torture_ssh_session(s,
39 TORTURE_SSH_SERVER,
40 NULL,
41 TORTURE_SSH_USER_ALICE,
42 NULL);
43 assert_non_null(s->ssh.session);
44
45 s->ssh.tsftp = torture_sftp_session(s->ssh.session);
46 assert_non_null(s->ssh.tsftp);
47
48 return 0;
49 }
50
51 static int session_teardown(void **state)
52 {
53 struct torture_state *s = *state;
54
55 torture_rmdirs(s->ssh.tsftp->testdir);
56 torture_sftp_close(s->ssh.tsftp);
57 ssh_disconnect(s->ssh.session);
58 ssh_free(s->ssh.session);
59
60 return 0;
61 }
62
63 static void torture_sftp_expand_path(void **state)
64 {
65 struct torture_state *s = *state;
66 struct torture_sftp *t = s->ssh.tsftp;
67 struct passwd *pwd = NULL;
68 char *expanded_path = NULL;
69 int rc;
70
71 rc = sftp_extension_supported(t->sftp, "[email protected]", "1");
72 if (rc == 0) {
73 skip();
74 }
75
76 pwd = getpwnam(TORTURE_SSH_USER_ALICE);
77 assert_non_null(pwd);
78
79 /* testing for a absolute path */
80 expanded_path = sftp_expand_path(t->sftp, "~/.");
81 assert_non_null(expanded_path);
82
83 assert_string_equal(expanded_path, pwd->pw_dir);
84
85 SSH_STRING_FREE_CHAR(expanded_path);
86
87 /* testing for a relative path */
88 expanded_path = sftp_expand_path(t->sftp, ".");
89 assert_non_null(expanded_path);
90
91 assert_string_equal(expanded_path, pwd->pw_dir);
92
93 SSH_STRING_FREE_CHAR(expanded_path);
94
95 /* passing a NULL sftp session */
96 expanded_path = sftp_expand_path(NULL, "~/.");
97 assert_null(expanded_path);
98
99 /* passing an invalid path */
100 expanded_path = sftp_expand_path(t->sftp, "/...//");
101 assert_null(expanded_path);
102
103 /* passing null path */
104 expanded_path = sftp_expand_path(t->sftp, NULL);
105 assert_null(expanded_path);
106 }
107
108 int torture_run_tests(void)
109 {
110 int rc;
111 struct CMUnitTest tests[] = {
112 cmocka_unit_test_setup_teardown(torture_sftp_expand_path,
113 session_setup,
114 session_teardown)
115 };
116
117 ssh_init();
118
119 torture_filter_tests(tests);
120 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
121
122 ssh_finalize();
123
124 return rc;
125 }