Debian Codegraph @

tests/client/torture_forward.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 <errno.h>
30 #include <sys/types.h>
31 #include <pwd.h>
32
33 static int sshd_setup(void **state)
34 {
35 torture_setup_sshd_server(state, false);
36
37 return 0;
38 }
39
40 static int sshd_teardown(void **state) {
41 torture_teardown_sshd_server(state);
42
43 return 0;
44 }
45
46 static int session_setup(void **state)
47 {
48 struct torture_state *s = *state;
49 struct passwd *pwd;
50 int rc;
51
52 pwd = getpwnam("bob");
53 assert_non_null(pwd);
54
55 rc = setuid(pwd->pw_uid);
56 assert_return_code(rc, errno);
57
58 s->ssh.session = torture_ssh_session(s,
59 TORTURE_SSH_SERVER,
60 NULL,
61 TORTURE_SSH_USER_ALICE,
62 NULL);
63 assert_non_null(s->ssh.session);
64
65 return 0;
66 }
67
68 static int session_teardown(void **state)
69 {
70 struct torture_state *s = *state;
71
72 ssh_disconnect(s->ssh.session);
73 ssh_free(s->ssh.session);
74
75 return 0;
76 }
77
78 static void torture_ssh_forward(void **state)
79 {
80 struct torture_state *s = *state;
81 ssh_session session = s->ssh.session;
82 ssh_channel c;
83 int dport;
84 int bound_port;
85 char *originator_host = NULL;
86 int originator_port;
87 int rc;
88 int verbosity = SSH_LOG_TRACE;
89
90 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
91
92 rc = ssh_channel_listen_forward(session, "127.0.0.21", 8080, &bound_port);
93 assert_ssh_return_code(session, rc);
94
95 c = ssh_channel_open_forward_port(session, 10, &dport, &originator_host, &originator_port);
96 /* We do not get a listener and run into the timeout here */
97 assert_null(c);
98
99 ssh_channel_send_eof(c);
100 ssh_channel_close(c);
101 }
102
103 int torture_run_tests(void) {
104 int rc;
105
106 struct CMUnitTest tests[] = {
107 cmocka_unit_test_setup_teardown(torture_ssh_forward,
108 session_setup,
109 session_teardown),
110 };
111
112 ssh_init();
113
114 torture_filter_tests(tests);
115 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
116
117 ssh_finalize();
118 return rc;
119 }