Debian Codegraph @

tests/client/torture_sftp_packet_read.c

raw
1 /*
2 * This is a regression test to make sure that sftp_read_packet times out
3 * properly in blocking mode
4 */
5
6 #define LIBSSH_STATIC
7
8 #include "config.h"
9
10 #include "sftp.c"
11 #include "torture.h"
12
13 #include <errno.h>
14 #include <libssh/socket.h>
15 #include <pwd.h>
16 #include <sys/types.h>
17
18 static int
19 sshd_setup(void **state)
20 {
21 torture_setup_sshd_server(state, false);
22
23 return 0;
24 }
25
26 static int
27 sshd_teardown(void **state)
28 {
29 torture_teardown_sshd_server(state);
30
31 return 0;
32 }
33
34 static int
35 session_setup(void **state)
36 {
37 struct torture_state *s = *state;
38 struct passwd *pwd = NULL;
39 int rc;
40
41 pwd = getpwnam("bob");
42 assert_non_null(pwd);
43
44 rc = setuid(pwd->pw_uid);
45 assert_return_code(rc, errno);
46
47 s->ssh.session = torture_ssh_session(s,
48 TORTURE_SSH_SERVER,
49 NULL,
50 TORTURE_SSH_USER_ALICE,
51 NULL);
52 assert_non_null(s->ssh.session);
53
54 s->ssh.tsftp = torture_sftp_session(s->ssh.session);
55 assert_non_null(s->ssh.tsftp);
56
57 return 0;
58 }
59
60 static int
61 session_teardown(void **state)
62 {
63 struct torture_state *s = *state;
64
65 torture_rmdirs(s->ssh.tsftp->testdir);
66 torture_sftp_close(s->ssh.tsftp);
67 ssh_disconnect(s->ssh.session);
68 ssh_free(s->ssh.session);
69
70 return 0;
71 }
72
73 static void
74 torture_sftp_packet_read(void **state)
75 {
76 struct torture_state *s = *state;
77 struct torture_sftp *t = s->ssh.tsftp;
78 sftp_packet packet = NULL;
79
80 int fds[2];
81 int rc;
82
83 /* creating blocking fd is the default pipe behaviour */
84 rc = pipe(fds);
85 assert_return_code(rc, errno);
86
87 t->ssh->opts.timeout = 1;
88 ssh_socket_set_fd(t->ssh->socket, fds[0]);
89
90 /*
91 * Making sure that the sftp_packet_read function times out and returns
92 * NULL.
93 */
94 packet = sftp_packet_read(t->sftp);
95 assert_null(packet);
96
97 close(fds[0]);
98 close(fds[1]);
99 }
100
101 int
102 torture_run_tests(void)
103 {
104 int rc;
105 struct CMUnitTest tests[] = {
106 cmocka_unit_test_setup_teardown(torture_sftp_packet_read,
107 session_setup,
108 session_teardown),
109 };
110
111 ssh_init();
112
113 torture_filter_tests(tests);
114 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
115 ssh_finalize();
116
117 return rc;
118 }