| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2012 by Aris Adamantiadis |
| 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/priv.h" |
| 29 |
#include "libssh/session.h" |
| 30 |
#include "libssh/callbacks.h" |
| 31 |
|
| 32 |
#include <sys/types.h> |
| 33 |
#include <pwd.h> |
| 34 |
#include <errno.h> |
| 35 |
|
| 36 |
#define STATE_SUCCESS (1) |
| 37 |
#define STATE_FAILURE (2) |
| 38 |
|
| 39 |
struct callback_state |
| 40 |
{ |
| 41 |
int open_response; |
| 42 |
int request_response; |
| 43 |
ssh_session expected_session; |
| 44 |
ssh_channel expected_channel; |
| 45 |
struct ssh_channel_callbacks_struct *callback; |
| 46 |
}; |
| 47 |
|
| 48 |
static void on_open_response(ssh_session session, ssh_channel channel, bool is_success, void *userdata) |
| 49 |
{ |
| 50 |
struct callback_state *state = (struct callback_state*)userdata; |
| 51 |
assert_ptr_equal(state->expected_session, session); |
| 52 |
assert_ptr_equal(state->expected_channel, channel); |
| 53 |
state->open_response = is_success ? STATE_SUCCESS : STATE_FAILURE; |
| 54 |
} |
| 55 |
|
| 56 |
static void on_request_response(ssh_session session, ssh_channel channel, void *userdata) |
| 57 |
{ |
| 58 |
struct callback_state *state = (struct callback_state*)userdata; |
| 59 |
assert_ptr_equal(state->expected_session, session); |
| 60 |
assert_ptr_equal(state->expected_channel, channel); |
| 61 |
state->request_response = STATE_SUCCESS; |
| 62 |
} |
| 63 |
|
| 64 |
static struct callback_state *set_callbacks(ssh_session session, ssh_channel channel) |
| 65 |
{ |
| 66 |
int rc; |
| 67 |
struct ssh_channel_callbacks_struct *cb; |
| 68 |
struct callback_state *cb_state = NULL; |
| 69 |
|
| 70 |
cb_state = (struct callback_state *)calloc(1, |
| 71 |
sizeof(struct callback_state)); |
| 72 |
assert_non_null(cb_state); |
| 73 |
cb_state->expected_session = session; |
| 74 |
cb_state->expected_channel = channel; |
| 75 |
|
| 76 |
cb = (struct ssh_channel_callbacks_struct *)calloc(1, |
| 77 |
sizeof(struct ssh_channel_callbacks_struct)); |
| 78 |
assert_non_null(cb); |
| 79 |
ssh_callbacks_init(cb); |
| 80 |
cb->userdata = cb_state; |
| 81 |
cb->channel_open_response_function = on_open_response; |
| 82 |
cb->channel_request_response_function = on_request_response; |
| 83 |
rc = ssh_set_channel_callbacks(channel, cb); |
| 84 |
assert_ssh_return_code(session, rc); |
| 85 |
|
| 86 |
/* Keep the reference so it can be cleaned up later */ |
| 87 |
cb_state->callback = cb; |
| 88 |
return cb_state; |
| 89 |
} |
| 90 |
|
| 91 |
static int sshd_setup(void **state) |
| 92 |
{ |
| 93 |
torture_setup_sshd_server(state, false); |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
static int sshd_teardown(void **state) |
| 99 |
{ |
| 100 |
torture_teardown_sshd_server(state); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static int session_setup(void **state) |
| 106 |
{ |
| 107 |
struct torture_state *s = *state; |
| 108 |
struct passwd *pwd; |
| 109 |
int rc; |
| 110 |
|
| 111 |
pwd = getpwnam("bob"); |
| 112 |
assert_non_null(pwd); |
| 113 |
|
| 114 |
rc = setuid(pwd->pw_uid); |
| 115 |
assert_return_code(rc, errno); |
| 116 |
|
| 117 |
s->ssh.session = torture_ssh_session(s, |
| 118 |
TORTURE_SSH_SERVER, |
| 119 |
NULL, |
| 120 |
TORTURE_SSH_USER_ALICE, |
| 121 |
NULL); |
| 122 |
assert_non_null(s->ssh.session); |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
static int session_teardown(void **state) |
| 128 |
{ |
| 129 |
struct torture_state *s = *state; |
| 130 |
|
| 131 |
ssh_disconnect(s->ssh.session); |
| 132 |
ssh_free(s->ssh.session); |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static void torture_open_success(void **state) |
| 138 |
{ |
| 139 |
struct torture_state *s = *state; |
| 140 |
ssh_session session = s->ssh.session; |
| 141 |
ssh_channel channel; |
| 142 |
int rc; |
| 143 |
struct callback_state *cb_state = NULL; |
| 144 |
|
| 145 |
channel = ssh_channel_new(session); |
| 146 |
assert_non_null(channel); |
| 147 |
|
| 148 |
cb_state = set_callbacks(session, channel); |
| 149 |
|
| 150 |
rc = ssh_channel_open_session(channel); |
| 151 |
assert_ssh_return_code(session, rc); |
| 152 |
|
| 153 |
assert_int_equal(STATE_SUCCESS, cb_state->open_response); |
| 154 |
|
| 155 |
ssh_channel_free(channel); |
| 156 |
free(cb_state->callback); |
| 157 |
free(cb_state); |
| 158 |
} |
| 159 |
|
| 160 |
static void torture_open_failure(void **state) |
| 161 |
{ |
| 162 |
struct torture_state *s = *state; |
| 163 |
ssh_session session = s->ssh.session; |
| 164 |
ssh_channel channel; |
| 165 |
int rc; |
| 166 |
struct callback_state *cb_state = NULL; |
| 167 |
|
| 168 |
channel = ssh_channel_new(session); |
| 169 |
assert_non_null(channel); |
| 170 |
|
| 171 |
cb_state = set_callbacks(session, channel); |
| 172 |
|
| 173 |
rc = ssh_channel_open_forward(channel, "0.0.0.0", 0, "0.0.0.0", 0); |
| 174 |
assert_ssh_return_code_equal(session, rc, SSH_ERROR); |
| 175 |
|
| 176 |
assert_int_equal(STATE_FAILURE, cb_state->open_response); |
| 177 |
|
| 178 |
ssh_channel_free(channel); |
| 179 |
free(cb_state->callback); |
| 180 |
free(cb_state); |
| 181 |
} |
| 182 |
|
| 183 |
static void torture_request_success(void **state) |
| 184 |
{ |
| 185 |
struct torture_state *s = *state; |
| 186 |
ssh_session session = s->ssh.session; |
| 187 |
ssh_channel channel; |
| 188 |
int rc; |
| 189 |
struct callback_state *cb_state = NULL; |
| 190 |
|
| 191 |
channel = ssh_channel_new(session); |
| 192 |
assert_non_null(channel); |
| 193 |
|
| 194 |
cb_state = set_callbacks(session, channel); |
| 195 |
|
| 196 |
rc = ssh_channel_open_session(channel); |
| 197 |
assert_ssh_return_code(session, rc); |
| 198 |
|
| 199 |
rc = ssh_channel_request_exec(channel, "echo -n ABCD"); |
| 200 |
assert_ssh_return_code(session, rc); |
| 201 |
|
| 202 |
assert_int_equal(STATE_SUCCESS, cb_state->request_response); |
| 203 |
|
| 204 |
ssh_channel_free(channel); |
| 205 |
free(cb_state->callback); |
| 206 |
free(cb_state); |
| 207 |
} |
| 208 |
|
| 209 |
static void torture_request_failure(void **state) |
| 210 |
{ |
| 211 |
struct torture_state *s = *state; |
| 212 |
ssh_session session = s->ssh.session; |
| 213 |
ssh_channel channel; |
| 214 |
int rc; |
| 215 |
struct callback_state *cb_state = NULL; |
| 216 |
|
| 217 |
channel = ssh_channel_new(session); |
| 218 |
assert_non_null(channel); |
| 219 |
|
| 220 |
cb_state = set_callbacks(session, channel); |
| 221 |
|
| 222 |
rc = ssh_channel_open_session(channel); |
| 223 |
assert_ssh_return_code(session, rc); |
| 224 |
|
| 225 |
rc = ssh_channel_request_env(channel, "NOT_ACCEPTED", "VALUE"); |
| 226 |
assert_ssh_return_code_equal(session, rc, SSH_ERROR); |
| 227 |
|
| 228 |
assert_int_equal(STATE_SUCCESS, cb_state->request_response); |
| 229 |
|
| 230 |
ssh_channel_free(channel); |
| 231 |
free(cb_state->callback); |
| 232 |
free(cb_state); |
| 233 |
} |
| 234 |
|
| 235 |
int torture_run_tests(void) |
| 236 |
{ |
| 237 |
int rc; |
| 238 |
struct CMUnitTest tests[] = { |
| 239 |
cmocka_unit_test_setup_teardown(torture_open_success, |
| 240 |
session_setup, |
| 241 |
session_teardown), |
| 242 |
cmocka_unit_test_setup_teardown(torture_open_failure, |
| 243 |
session_setup, |
| 244 |
session_teardown), |
| 245 |
cmocka_unit_test_setup_teardown(torture_request_success, |
| 246 |
session_setup, |
| 247 |
session_teardown), |
| 248 |
cmocka_unit_test_setup_teardown(torture_request_failure, |
| 249 |
session_setup, |
| 250 |
session_teardown), |
| 251 |
}; |
| 252 |
|
| 253 |
ssh_init(); |
| 254 |
|
| 255 |
torture_filter_tests(tests); |
| 256 |
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); |
| 257 |
|
| 258 |
ssh_finalize(); |
| 259 |
|
| 260 |
return rc; |
| 261 |
} |