Debian Codegraph @

tests/client/torture_sftp_canonicalize_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 torture_teardown_sshd_server(state);
21
22 return 0;
23 }
24
25 static int session_setup(void **state)
26 {
27 struct torture_state *s = *state;
28 struct passwd *pwd;
29 int rc;
30
31 pwd = getpwnam("bob");
32 assert_non_null(pwd);
33
34 rc = setuid(pwd->pw_uid);
35 assert_return_code(rc, errno);
36
37 s->ssh.session = torture_ssh_session(s,
38 TORTURE_SSH_SERVER,
39 NULL,
40 TORTURE_SSH_USER_ALICE,
41 NULL);
42 assert_non_null(s->ssh.session);
43
44 s->ssh.tsftp = torture_sftp_session(s->ssh.session);
45 assert_non_null(s->ssh.tsftp);
46
47 return 0;
48 }
49
50 static int session_teardown(void **state)
51 {
52 struct torture_state *s = *state;
53
54 torture_rmdirs(s->ssh.tsftp->testdir);
55 torture_sftp_close(s->ssh.tsftp);
56 ssh_disconnect(s->ssh.session);
57 ssh_free(s->ssh.session);
58
59 return 0;
60 }
61
62 static void torture_sftp_canonicalize_path(void **state)
63 {
64 struct torture_state *s = *state;
65 struct torture_sftp *t = s->ssh.tsftp;
66 struct passwd *pwd = NULL;
67 char *canonicalized_path = NULL;
68
69 pwd = getpwnam(TORTURE_SSH_USER_ALICE);
70 assert_non_null(pwd);
71
72 canonicalized_path = sftp_canonicalize_path(t->sftp, ".");
73 assert_non_null(canonicalized_path);
74
75 assert_string_equal(canonicalized_path, pwd->pw_dir);
76
77 SSH_STRING_FREE_CHAR(canonicalized_path);
78 }
79
80 int torture_run_tests(void) {
81 int rc;
82 struct CMUnitTest tests[] = {
83 cmocka_unit_test_setup_teardown(torture_sftp_canonicalize_path,
84 session_setup,
85 session_teardown)
86 };
87
88 ssh_init();
89
90 torture_filter_tests(tests);
91 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
92
93 ssh_finalize();
94
95 return rc;
96 }
97