Debian Codegraph @

tests/client/torture_sftp_recv_response_msg.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2024 Eshan Kelkar <[email protected]>
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "torture.h"
27 #include <libssh/libssh.h>
28 #include <libssh/sftp.h>
29 #include <libssh/sftp_priv.h>
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <sys/types.h>
35
36 /* For the ability to access the members of the sftp_aio_struct in the test */
37 #include "sftp_aio.c"
38
39 static int sshd_setup(void **state)
40 {
41 torture_setup_sshd_server(state, false);
42
43 return 0;
44 }
45
46 static int sshd_teardown(void **state)
47 {
48 torture_teardown_sshd_server(state);
49
50 return 0;
51 }
52
53 static int session_setup(void **state)
54 {
55 struct torture_state *s = *state;
56 struct passwd *pwd = NULL;
57 int rc;
58
59 pwd = getpwnam("bob");
60 assert_non_null(pwd);
61
62 rc = setuid(pwd->pw_uid);
63 assert_return_code(rc, errno);
64
65 s->ssh.session = torture_ssh_session(s,
66 TORTURE_SSH_SERVER,
67 NULL,
68 TORTURE_SSH_USER_ALICE,
69 NULL);
70 assert_non_null(s->ssh.session);
71
72 s->ssh.tsftp = torture_sftp_session(s->ssh.session);
73 assert_non_null(s->ssh.tsftp);
74
75 return 0;
76 }
77
78 static int session_teardown(void **state)
79 {
80 struct torture_state *s = *state;
81
82 torture_rmdirs(s->ssh.tsftp->testdir);
83 torture_sftp_close(s->ssh.tsftp);
84 ssh_disconnect(s->ssh.session);
85 ssh_free(s->ssh.session);
86
87 return 0;
88 }
89
90 /* Test that sftp_recv_response_msg() works properly in blocking mode */
91 static void torture_sftp_recv_response_msg_blocking(void **state)
92 {
93 struct torture_state *s = *state;
94 struct torture_sftp *t = s->ssh.tsftp;
95 sftp_session sftp = t->sftp;
96 sftp_file file = NULL;
97 sftp_aio aio = NULL;
98 sftp_message msg = NULL;
99 ssize_t bytes_requested;
100 int rc;
101
102 /*
103 * For sending an sftp request and obtaining its request id, this test uses
104 * the sftp aio API
105 */
106 file = sftp_open(sftp, SSH_EXECUTABLE, O_RDONLY, 0);
107 assert_non_null(file);
108
109 /* Send an sftp read request */
110 bytes_requested = sftp_aio_begin_read(file, 16, &aio);
111 assert_int_equal(bytes_requested, 16);
112 assert_non_null(aio);
113
114 /* Wait for the response (blocking mode) */
115 rc = sftp_recv_response_msg(sftp, aio->id, true, &msg);
116 assert_int_equal(rc, SSH_OK);
117
118 sftp_message_free(msg);
119 sftp_aio_free(aio);
120 sftp_close(file);
121 }
122
123 /* Test that sftp_recv_response_msg() works properly in non blocking mode */
124 static void torture_sftp_recv_response_msg_non_blocking(void **state)
125 {
126 struct torture_state *s = *state;
127 struct torture_sftp *t = s->ssh.tsftp;
128 sftp_session sftp = t->sftp;
129 sftp_message msg = NULL;
130 sftp_file file = NULL;
131 sftp_aio aio = NULL;
132 ssize_t bytes_requested;
133 int rc;
134
135 /*
136 * At this point, the sftp channel shouldn't contain any outstanding
137 * responses.
138 *
139 * Hence, sftp_recv_response_msg() should return SSH_AGAIN immediately when
140 * we try to receive a response for any request ID in non-blocking mode.
141 */
142 rc = sftp_recv_response_msg(sftp, 1984, false, &msg);
143 assert_int_equal(rc, SSH_AGAIN);
144
145 /*
146 * Validate that after a response arrives in the sftp channel, trying to
147 * receive the response in non-blocking mode works properly.
148 *
149 * For sending an sftp request and obtaining its request id, this test uses
150 * the sftp aio API
151 */
152 file = sftp_open(sftp, SSH_EXECUTABLE, O_RDONLY, 0);
153 assert_non_null(file);
154
155 bytes_requested = sftp_aio_begin_read(file, 16, &aio);
156 assert_int_equal(bytes_requested, 16);
157 assert_non_null(aio);
158
159 /* Poll the sftp channel for the response */
160 rc = ssh_channel_poll_timeout(sftp->channel, 60000, 0);
161 assert_int_not_equal(rc, SSH_ERROR);
162 assert_int_not_equal(rc, SSH_EOF);
163 assert_int_not_equal(rc, 0);
164
165 /*
166 * The response has arrived, trying to obtain it in non blocking mode
167 * should work
168 */
169 rc = sftp_recv_response_msg(sftp, aio->id, false, &msg);
170 assert_int_equal(rc, SSH_OK);
171
172 sftp_message_free(msg);
173 sftp_aio_free(aio);
174 sftp_close(file);
175 }
176
177 int torture_run_tests(void)
178 {
179 int rc;
180 struct CMUnitTest tests[] = {
181 cmocka_unit_test_setup_teardown(torture_sftp_recv_response_msg_blocking,
182 session_setup,
183 session_teardown),
184 cmocka_unit_test_setup_teardown(
185 torture_sftp_recv_response_msg_non_blocking,
186 session_setup,
187 session_teardown),
188 };
189
190 ssh_init();
191
192 torture_filter_tests(tests);
193 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
194 ssh_finalize();
195
196 return rc;
197 }