Debian Codegraph @

tests/client/torture_request_pty_modes.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2013 by Andreas Schneider <[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
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <pwd.h>
32 #include <errno.h>
33 #include <termios.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <pty.h>
37
38 static int sshd_setup(void **state)
39 {
40 torture_setup_sshd_server(state, false);
41
42 return 0;
43 }
44
45 static int sshd_teardown(void **state) {
46 torture_teardown_sshd_server(state);
47
48 return 0;
49 }
50
51 static int session_setup(void **state)
52 {
53 struct torture_state *s = *state;
54 struct passwd *pwd;
55 int rc;
56
57 pwd = getpwnam("bob");
58 assert_non_null(pwd);
59
60 rc = setuid(pwd->pw_uid);
61 assert_return_code(rc, errno);
62
63 s->ssh.session = torture_ssh_session(s,
64 TORTURE_SSH_SERVER,
65 NULL,
66 TORTURE_SSH_USER_ALICE,
67 NULL);
68 assert_non_null(s->ssh.session);
69
70 return 0;
71 }
72
73 static int session_teardown(void **state)
74 {
75 struct torture_state *s = *state;
76
77 ssh_disconnect(s->ssh.session);
78 ssh_free(s->ssh.session);
79
80 return 0;
81 }
82
83 /* reads from the channel, expecting the given output */
84 static int check_channel_output(ssh_channel c, const char *expected)
85 {
86 char buffer[4096] = {0};
87 int nbytes, offset = 0;
88
89 nbytes = ssh_channel_read(c, buffer, sizeof(buffer) - 1, 0);
90 while (nbytes > 0) {
91 buffer[offset + nbytes] = '\0';
92 ssh_log_hexdump("Read bytes:",
93 (unsigned char *)buffer,
94 offset + nbytes);
95 if (strstr(buffer, expected) != NULL)
96 {
97 return 1;
98 }
99 /* read on */
100 offset = nbytes;
101 nbytes = ssh_channel_read(c,
102 buffer + offset,
103 sizeof(buffer) - offset - 1,
104 0);
105 }
106 return 0;
107 }
108
109 /* set explicit TTY modes and validate that the server uses them */
110 static void torture_request_pty_modes_translate_ocrnl(void **state)
111 {
112 const unsigned char modes[] = {
113 /* enable OCRNL */
114 73, 0, 0, 0, 1,
115 /* disable all other CR/NL handling */
116 34, 0, 0, 0, 0,
117 35, 0, 0, 0, 0,
118 36, 0, 0, 0, 0,
119 72, 0, 0, 0, 0,
120 74, 0, 0, 0, 0,
121 75, 0, 0, 0, 0,
122 0, /* TTY_OP_END */
123 };
124 struct torture_state *s = *state;
125 ssh_session session = s->ssh.session;
126 ssh_channel c;
127 int rc;
128 int string_found = 0;
129
130 c = ssh_channel_new(session);
131 assert_non_null(c);
132
133 rc = ssh_channel_open_session(c);
134 assert_ssh_return_code(session, rc);
135
136 rc = ssh_channel_request_pty_size_modes(c, "xterm", 80, 25, modes, sizeof(modes));
137 assert_ssh_return_code(session, rc);
138
139 rc = ssh_channel_request_exec(c, "/bin/echo -e '>TEST\\r\\n<'");
140 assert_ssh_return_code(session, rc);
141
142 /* expect 2 newline characters */
143 string_found = check_channel_output(c, ">TEST\n\n<");
144 assert_int_equal(string_found, 1);
145
146 ssh_channel_close(c);
147 }
148
149 /* if stdin is a TTY, its modes are passed to the server */
150 static void torture_request_pty_modes_use_stdin_modes(void **state)
151 {
152 struct torture_state *s = *state;
153 ssh_session session = s->ssh.session;
154 ssh_channel c;
155 int rc;
156 int string_found = 0;
157 struct termios modes;
158 int stdin_backup_fd = -1;
159 int master_fd, slave_fd;
160
161 c = ssh_channel_new(session);
162 assert_non_null(c);
163
164 rc = ssh_channel_open_session(c);
165 assert_ssh_return_code(session, rc);
166
167 /* stdin must be a TTY, so open one and replace the FD */
168 stdin_backup_fd = dup(STDIN_FILENO);
169 rc = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
170 assert_int_equal(rc, 0);
171 dup2(master_fd, STDIN_FILENO);
172 assert_true(isatty(STDIN_FILENO));
173 /* translate NL to CRNL on output to see a noticeable effect */
174 memset(&modes, 0, sizeof(modes));
175 tcgetattr(STDIN_FILENO, &modes);
176 modes.c_oflag |= ONLCR;
177 modes.c_iflag &= ~(ICRNL | INLCR | IGNCR);
178 tcsetattr(STDIN_FILENO, TCSANOW, &modes);
179
180 rc = ssh_channel_request_pty_size(c, "xterm", 80, 25);
181
182 /* revert the changes to STDIN first! */
183 dup2(stdin_backup_fd, STDIN_FILENO);
184 close(stdin_backup_fd);
185 close(master_fd);
186 close(slave_fd);
187
188 assert_ssh_return_code(session, rc);
189
190 rc = ssh_channel_request_exec(c, "/bin/echo -e '>TEST\\r\\n<'");
191 assert_ssh_return_code(session, rc);
192
193 /* expect 2 carriage return characters + newline */
194 string_found = check_channel_output(c, ">TEST\r\r\n<");
195 assert_int_equal(string_found, 1);
196
197 ssh_channel_close(c);
198 }
199
200 /* if stdin is NOT a TTY, default modes are passed to the server */
201 static void torture_request_pty_modes_use_default_modes(void **state)
202 {
203 struct torture_state *s = *state;
204 ssh_session session = s->ssh.session;
205 ssh_channel c;
206 int rc;
207 int string_found = 0;
208 int stdin_backup_fd = -1;
209
210 c = ssh_channel_new(session);
211 assert_non_null(c);
212
213 rc = ssh_channel_open_session(c);
214 assert_ssh_return_code(session, rc);
215
216 /* stdin must not a TTY - change the FD to something else */
217 stdin_backup_fd = dup(STDIN_FILENO);
218 close(STDIN_FILENO);
219 rc = open("/dev/null", O_RDONLY); // reuses FD 0 now
220 assert_int_equal(rc, STDIN_FILENO);
221 assert_false(isatty(STDIN_FILENO));
222
223 rc = ssh_channel_request_pty_size(c, "xterm", 80, 25);
224
225 /* revert the changes to STDIN first! */
226 dup2(stdin_backup_fd, STDIN_FILENO);
227 close(stdin_backup_fd);
228
229 assert_ssh_return_code(session, rc);
230
231 rc = ssh_channel_request_exec(c, "/bin/echo -e '>TEST\\r\\n<'");
232 assert_ssh_return_code(session, rc);
233
234 /* expect the CRLF translated to newline */
235 string_found = check_channel_output(c, ">TEST\r\r\n<");
236 assert_int_equal(string_found, 1);
237
238 ssh_channel_close(c);
239 }
240
241 int torture_run_tests(void) {
242 int rc;
243
244 struct CMUnitTest tests[] = {
245 cmocka_unit_test_setup_teardown(torture_request_pty_modes_translate_ocrnl,
246 session_setup,
247 session_teardown),
248 cmocka_unit_test_setup_teardown(torture_request_pty_modes_use_stdin_modes,
249 session_setup,
250 session_teardown),
251 cmocka_unit_test_setup_teardown(torture_request_pty_modes_use_default_modes,
252 session_setup,
253 session_teardown),
254 };
255
256 ssh_init();
257
258 torture_filter_tests(tests);
259 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
260
261 ssh_finalize();
262 return rc;
263 }
264