Debian Codegraph @

tests/client/torture_client_global_requests.c

raw
1 /*
2 * torture_client_global_requests.c - Tests for client global requests
3 *
4 * This file is part of the SSH Library
5 *
6 * Copyright (c) 2019 by Red Hat, Inc.
7 *
8 * Author: Anderson Toshiyuki Sasaki <[email protected]>
9 *
10 * The SSH Library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
14 *
15 * The SSH Library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the SSH Library; see the file COPYING. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA.
24 */
25
26 #include "config.h"
27
28 #define LIBSSH_STATIC
29
30 #include "torture.h"
31 #include "libssh/libssh.h"
32 #include "libssh/priv.h"
33 #include "libssh/session.h"
34 #include "libssh/channels.h"
35
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <pwd.h>
39
40 static int sshd_setup(void **state)
41 {
42 torture_setup_sshd_server(state, true);
43
44 return 0;
45 }
46
47 static int sshd_teardown(void **state)
48 {
49 torture_teardown_sshd_server(state);
50
51 return 0;
52 }
53
54 static int session_setup(void **state)
55 {
56 struct torture_state *s = *state;
57 int verbosity = torture_libssh_verbosity();
58 struct passwd *pwd;
59 bool b = false;
60 int rc;
61
62 pwd = getpwnam("bob");
63 assert_non_null(pwd);
64
65 rc = setuid(pwd->pw_uid);
66 assert_return_code(rc, errno);
67
68 s->ssh.session = ssh_new();
69 assert_non_null(s->ssh.session);
70
71 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
72 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
73
74 /* Make sure no other configuration options from system will get used */
75 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b);
76 assert_ssh_return_code(s->ssh.session, rc);
77
78 return 0;
79 }
80
81 static int session_teardown(void **state)
82 {
83 struct torture_state *s = *state;
84
85 ssh_disconnect(s->ssh.session);
86 ssh_free(s->ssh.session);
87
88 return 0;
89 }
90
91 static int authenticate(void **state)
92 {
93 struct torture_state *s = *state;
94 ssh_session session = s->ssh.session;
95 int rc;
96
97 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
98 assert_int_equal(rc, SSH_OK);
99
100 rc = ssh_connect(session);
101 assert_int_equal(rc, SSH_OK);
102
103 rc = ssh_userauth_password(session, NULL, TORTURE_SSH_USER_BOB_PASSWORD);
104 assert_int_equal(rc, SSH_AUTH_SUCCESS);
105
106 return rc;
107 }
108
109 static void torture_unknown_request(void **state)
110 {
111 struct torture_state *s = *state;
112 ssh_session session = s->ssh.session;
113 ssh_channel channel;
114 int rc;
115
116 rc = authenticate(state);
117 assert_ssh_return_code(session, rc);
118
119 /* Request asking for reply */
120 rc = ssh_global_request(session, "[email protected]", NULL, 1);
121 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
122
123 /* Request and don't ask for reply */
124 rc = ssh_global_request(session, "[email protected]", NULL, 0);
125 assert_ssh_return_code(session, rc);
126
127 /* Open channel to make sure the session is still working */
128 channel = ssh_channel_new(session);
129 assert_non_null(channel);
130
131 rc = ssh_channel_open_session(channel);
132 assert_ssh_return_code(session, rc);
133
134 ssh_channel_close(channel);
135 }
136
137 int torture_run_tests(void)
138 {
139 int rc;
140 struct CMUnitTest tests[] = {
141 cmocka_unit_test_setup_teardown(torture_unknown_request,
142 session_setup,
143 session_teardown),
144 };
145
146 ssh_init();
147 torture_filter_tests(tests);
148 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
149 ssh_finalize();
150
151 return rc;
152 }