Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | #include <setjmp.h> |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 5 | #include <sys/socket.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 6 | #include <sys/types.h> |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 7 | |
| 8 | #include <cmocka.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 9 | #include <config.h> |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 10 | #include <libyang/libyang.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 11 | #include <log.h> |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 12 | #include <session_client.h> |
Fred Gan | 3a736e0 | 2021-01-04 17:59:38 +0800 | [diff] [blame] | 13 | #include <session_client_ch.h> |
| 14 | #include <session_p.h> |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 15 | #include "tests/config.h" |
| 16 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 17 | #include <libssh/callbacks.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 18 | #include <libssh/libssh.h> |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 19 | #include <libssh/server.h> |
| 20 | |
| 21 | static int |
| 22 | ssh_hostkey_check_clb(const char *hostname, ssh_session session, void *priv) |
| 23 | { |
| 24 | (void)hostname; |
| 25 | (void)session; |
| 26 | (void)priv; |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static int |
| 32 | setup_f(void **state) |
| 33 | { |
| 34 | (void)state; |
| 35 | int ret; |
| 36 | |
| 37 | nc_verbosity(NC_VERB_VERBOSE); |
| 38 | |
| 39 | ret = nc_client_ssh_set_username("username"); |
| 40 | assert_int_equal(ret, 0); |
Fred Gan | 3a736e0 | 2021-01-04 17:59:38 +0800 | [diff] [blame] | 41 | ret = nc_client_ssh_ch_set_username("ch_username"); |
| 42 | assert_int_equal(ret, 0); |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 43 | nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb, NULL); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int |
| 49 | teardown_f(void **state) |
| 50 | { |
| 51 | (void)state; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | MOCK int |
| 56 | __wrap_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) |
| 57 | { |
| 58 | (void)sockfd; |
| 59 | (void)addr; |
| 60 | (void)addrlen; |
| 61 | |
| 62 | return (int)mock(); |
| 63 | } |
| 64 | |
| 65 | MOCK int |
| 66 | __wrap_ssh_connect(ssh_session session) |
| 67 | { |
| 68 | (void)session; |
| 69 | |
| 70 | /* set support of all authentication methods by fake server */ |
| 71 | ssh_set_auth_methods(session, SSH_AUTH_METHOD_PUBLICKEY | SSH_AUTH_METHOD_PASSWORD | SSH_AUTH_METHOD_INTERACTIVE); |
| 72 | return (int)mock(); |
| 73 | } |
| 74 | |
| 75 | MOCK int |
| 76 | __wrap_ssh_userauth_none(ssh_session session, const char *username) |
| 77 | { |
| 78 | (void)session; |
| 79 | (void)username; |
| 80 | |
| 81 | return (int)mock(); |
| 82 | } |
| 83 | |
| 84 | MOCK int |
| 85 | __wrap_ssh_userauth_kbdint(ssh_session session, const char *user, const char *submethods) |
| 86 | { |
| 87 | (void)session; |
| 88 | (void)user; |
| 89 | (void)submethods; |
| 90 | |
| 91 | return (int)mock(); |
| 92 | } |
| 93 | |
| 94 | MOCK int |
| 95 | __wrap_ssh_is_connected(ssh_session session) |
| 96 | { |
| 97 | (void)session; |
| 98 | |
| 99 | return (int)mock(); |
| 100 | } |
| 101 | |
| 102 | MOCK int |
| 103 | __wrap_ssh_channel_open_session(ssh_channel channel) |
| 104 | { |
| 105 | (void)channel; |
| 106 | |
| 107 | return (int)mock(); |
| 108 | } |
| 109 | |
| 110 | MOCK int |
| 111 | __wrap_ssh_channel_request_subsystem(ssh_channel channel, const char *subsystem) |
| 112 | { |
| 113 | (void)channel; |
| 114 | (void)subsystem; |
| 115 | |
| 116 | return (int)mock(); |
| 117 | } |
| 118 | |
| 119 | MOCK int |
| 120 | __wrap_ssh_channel_is_closed(ssh_channel channel) |
| 121 | { |
| 122 | (void)channel; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | MOCK int |
| 128 | __wrap_ssh_channel_write(ssh_channel channel, const void *data, uint32_t len) |
| 129 | { |
| 130 | (void)channel; |
| 131 | (void)data; |
| 132 | |
| 133 | return len; |
| 134 | } |
| 135 | |
| 136 | MOCK int |
| 137 | __wrap_ssh_channel_poll_timeout(ssh_channel channel, int timeout, int is_stderr) |
| 138 | { |
| 139 | (void)channel; |
| 140 | (void)timeout; |
| 141 | (void)is_stderr; |
| 142 | |
| 143 | return (int)mock(); |
| 144 | } |
| 145 | |
| 146 | MOCK int |
| 147 | __wrap_ssh_userauth_password(ssh_session session, const char *username, const char *password) |
| 148 | { |
| 149 | (void)session; |
| 150 | check_expected(password); |
| 151 | check_expected(username); |
| 152 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 153 | return (int)mock(); |
| 154 | } |
| 155 | |
| 156 | MOCK int |
| 157 | __wrap_nc_handshake_io(struct nc_session *session) |
| 158 | { |
| 159 | (void)session; |
| 160 | |
| 161 | return (int)mock(); |
| 162 | } |
| 163 | |
| 164 | MOCK int |
| 165 | __wrap_nc_ctx_check_and_fill(struct nc_session *session) |
| 166 | { |
| 167 | (void)session; |
| 168 | |
| 169 | return (int)mock(); |
| 170 | } |
| 171 | |
David Sedlák | 77acc20 | 2018-10-08 21:44:14 +0200 | [diff] [blame] | 172 | MOCK int |
| 173 | __wrap_ssh_userauth_try_publickey(ssh_session session, const char *username, const ssh_key pubkey) |
| 174 | { |
| 175 | (void)session; |
| 176 | (void)username; |
| 177 | (void)pubkey; |
| 178 | |
| 179 | return (int)mock(); |
| 180 | } |
| 181 | |
| 182 | MOCK int |
| 183 | __wrap_ssh_userauth_publickey(ssh_session session, const char *username, const ssh_key privkey) |
| 184 | { |
| 185 | (void)session; |
| 186 | (void)username; |
| 187 | (void)privkey; |
| 188 | |
| 189 | return (int)mock(); |
| 190 | } |
| 191 | |
Fred Gan | 3a736e0 | 2021-01-04 17:59:38 +0800 | [diff] [blame] | 192 | MOCK int |
| 193 | __wrap_nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka) |
| 194 | { |
| 195 | (void)address; |
| 196 | (void)port; |
| 197 | (void)ka; |
| 198 | |
| 199 | return (int)mock(); |
| 200 | } |
| 201 | |
| 202 | MOCK int |
| 203 | __wrap_nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx) |
| 204 | { |
| 205 | (void)binds; |
| 206 | (void)bind_count; |
| 207 | (void)timeout; |
| 208 | (void)host; |
| 209 | (void)port; |
| 210 | |
| 211 | *idx = 0; |
| 212 | return (int)mock(); |
| 213 | } |
| 214 | |
| 215 | MOCK struct nc_session * |
| 216 | __wrap_nc_accept_callhome_ssh_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout) |
| 217 | { |
| 218 | (void)sock; |
| 219 | (void)host; |
| 220 | (void)port; |
| 221 | (void)ctx; |
| 222 | (void)timeout; |
| 223 | |
| 224 | return mock_ptr_type(struct nc_session *); |
| 225 | } |
| 226 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 227 | static int |
| 228 | test_hostkey_clb(const char *hostname, ssh_session session, void *priv) |
| 229 | { |
| 230 | (void)hostname; |
| 231 | (void)session; |
| 232 | (void)priv; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static void |
| 238 | test_nc_client_ssh_setting_auth_hostkey_check_clb(void **state) |
| 239 | { |
| 240 | (void)state; |
| 241 | int (*ret_f)(const char *hostname, ssh_session session, void *priv); |
| 242 | char *priv_data_ret; |
| 243 | |
| 244 | /* ssh_hostkey_check_clb is set in setup_f */ |
| 245 | nc_client_ssh_get_auth_hostkey_check_clb(&ret_f, (void **)&priv_data_ret); |
| 246 | assert_ptr_equal(ret_f, ssh_hostkey_check_clb); |
| 247 | assert_null(priv_data_ret); |
| 248 | |
| 249 | /* set different callback and private data */ |
| 250 | nc_client_ssh_set_auth_hostkey_check_clb(test_hostkey_clb, "DATA"); |
| 251 | nc_client_ssh_get_auth_hostkey_check_clb(&ret_f, (void **)&priv_data_ret); |
| 252 | assert_ptr_equal(ret_f, test_hostkey_clb); |
| 253 | assert_string_equal(priv_data_ret, "DATA"); |
| 254 | } |
| 255 | |
| 256 | char * |
| 257 | test_pwd_clb1(const char *username, const char *hostname, void *priv) |
| 258 | { |
| 259 | char *pass, *pass_to_return; |
| 260 | |
| 261 | check_expected(username); |
| 262 | check_expected(hostname); |
| 263 | check_expected(priv); |
| 264 | |
| 265 | pass = (char *)mock(); |
| 266 | pass_to_return = malloc(sizeof *pass * (strlen(pass) + 1)); |
| 267 | strcpy(pass_to_return, pass); |
| 268 | |
| 269 | return pass_to_return; |
| 270 | } |
| 271 | |
| 272 | char * |
| 273 | test_pwd_clb2(const char *username, const char *hostname, void *priv) |
| 274 | { |
| 275 | (void)username; |
| 276 | (void)hostname; |
| 277 | (void)priv; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static void |
| 283 | test_nc_client_ssh_setting_auth_password_clb(void **state) |
| 284 | { |
| 285 | (void)state; |
| 286 | char *(*ret_f)(const char *username, const char *hostname, void *priv); |
| 287 | char *priv_data_ret; |
| 288 | |
| 289 | /* set callback */ |
| 290 | nc_client_ssh_set_auth_password_clb(test_pwd_clb1, "DATA"); |
| 291 | nc_client_ssh_get_auth_password_clb(&ret_f, (void **)&priv_data_ret); |
| 292 | assert_ptr_equal(test_pwd_clb1, ret_f); |
| 293 | assert_string_equal("DATA", priv_data_ret); |
| 294 | |
| 295 | /* set different callback */ |
| 296 | nc_client_ssh_set_auth_password_clb(test_pwd_clb2, "NEW DATA"); |
| 297 | nc_client_ssh_get_auth_password_clb(&ret_f, (void **)&priv_data_ret); |
| 298 | assert_ptr_equal(test_pwd_clb2, ret_f); |
| 299 | assert_string_equal("NEW DATA", priv_data_ret); |
| 300 | } |
| 301 | |
| 302 | char * |
| 303 | test_inter_clb1(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv) |
| 304 | { |
| 305 | (void)auth_name; |
| 306 | (void)instruction; |
| 307 | (void)prompt; |
| 308 | (void)echo; |
| 309 | (void)priv; |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | char * |
| 315 | test_inter_clb2(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv) |
| 316 | { |
| 317 | (void)auth_name; |
| 318 | (void)instruction; |
| 319 | (void)prompt; |
| 320 | (void)echo; |
| 321 | (void)priv; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static void |
| 327 | test_nc_client_ssh_setting_auth_interactive_clb(void **state) |
| 328 | { |
| 329 | (void)state; |
| 330 | char *(*ret_f)(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv); |
| 331 | char *priv_data_ret; |
| 332 | |
| 333 | /* set callback */ |
| 334 | nc_client_ssh_set_auth_interactive_clb(test_inter_clb1, "DATA"); |
| 335 | nc_client_ssh_get_auth_interactive_clb(&ret_f, (void **)&priv_data_ret); |
| 336 | assert_ptr_equal(test_inter_clb1, ret_f); |
| 337 | assert_string_equal("DATA", priv_data_ret); |
| 338 | |
| 339 | /* set diferent callback */ |
| 340 | nc_client_ssh_set_auth_interactive_clb(test_inter_clb2, "NEW DATA"); |
| 341 | nc_client_ssh_get_auth_interactive_clb(&ret_f, (void **)&priv_data_ret); |
| 342 | assert_ptr_equal(test_inter_clb2, ret_f); |
| 343 | assert_string_equal("NEW DATA", priv_data_ret); |
| 344 | } |
| 345 | |
| 346 | char * |
| 347 | test_passphrase_clb1(const char *privkey_path, void *priv) |
| 348 | { |
| 349 | (void)privkey_path; |
| 350 | (void)priv; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | char * |
| 356 | test_passphrase_clb2(const char *privkey_path, void *priv) |
| 357 | { |
| 358 | (void)privkey_path; |
| 359 | (void)priv; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static void |
| 365 | test_nc_client_ssh_setting_auth_privkey_passphrase_clb(void **state) |
| 366 | { |
| 367 | (void)state; |
| 368 | char *(*ret_f)(const char *privkey_path, void *priv); |
| 369 | char *priv_data_ret; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 370 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 371 | /* set first callback */ |
| 372 | nc_client_ssh_set_auth_privkey_passphrase_clb(test_passphrase_clb1, "DATA"); |
| 373 | nc_client_ssh_get_auth_privkey_passphrase_clb(&ret_f, (void **)&priv_data_ret); |
| 374 | assert_ptr_equal(ret_f, test_passphrase_clb1); |
| 375 | assert_string_equal("DATA", priv_data_ret); |
| 376 | |
| 377 | /* set different callback */ |
| 378 | nc_client_ssh_set_auth_privkey_passphrase_clb(test_passphrase_clb2, "NEW DATA"); |
| 379 | nc_client_ssh_get_auth_privkey_passphrase_clb(&ret_f, (void **)&priv_data_ret); |
| 380 | assert_ptr_equal(ret_f, test_passphrase_clb2); |
| 381 | assert_string_equal("NEW DATA", priv_data_ret); |
| 382 | } |
| 383 | |
| 384 | static void |
| 385 | test_nc_client_ssh_adding_keypair(void **state) |
| 386 | { |
| 387 | (void)state; |
| 388 | int ret; |
| 389 | const char *pubkey1, *pubkey2; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 390 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 391 | /* at the beginning keypair count should be 0 */ |
| 392 | ret = nc_client_ssh_get_keypair_count(); |
| 393 | assert_int_equal(ret, 0); |
| 394 | |
| 395 | /* add first key pair */ |
Michal Vasko | 7a4213f | 2022-09-08 11:02:34 +0200 | [diff] [blame^] | 396 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_ecdsa.pub", TESTS_DIR "/data/key_ecdsa"); |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 397 | assert_int_equal(ret, 0); |
| 398 | ret = nc_client_ssh_get_keypair_count(); |
| 399 | assert_int_equal(ret, 1); |
| 400 | |
| 401 | /* add second keypair */ |
| 402 | ret = nc_client_ssh_add_keypair("key_pub", "key_priv"); |
| 403 | assert_int_equal(ret, 0); |
| 404 | ret = nc_client_ssh_get_keypair_count(); |
| 405 | assert_int_equal(ret, 2); |
| 406 | ret = nc_client_ssh_get_keypair(1, &pubkey1, &pubkey2); |
| 407 | assert_int_equal(ret, 0); |
| 408 | assert_string_equal(pubkey1, "key_pub"); |
| 409 | assert_string_equal(pubkey2, "key_priv"); |
| 410 | |
| 411 | /* delete first keypair */ |
| 412 | ret = nc_client_ssh_del_keypair(0); |
| 413 | assert_int_equal(ret, 0); |
| 414 | ret = nc_client_ssh_get_keypair_count(); |
| 415 | assert_int_equal(ret, 1); |
| 416 | /* try to get deleted keypair */ |
| 417 | ret = nc_client_ssh_get_keypair(5, &pubkey1, &pubkey2); |
| 418 | assert_int_equal(ret, -1); |
| 419 | |
| 420 | /* try to add keypair that is already set */ |
| 421 | ret = nc_client_ssh_add_keypair("key_pub", "key_priv"); |
| 422 | assert_int_equal(ret, -1); |
| 423 | ret = nc_client_ssh_get_keypair_count(); |
| 424 | assert_int_equal(ret, 1); |
| 425 | |
| 426 | /* try to delete keypair with id that is not used */ |
| 427 | ret = nc_client_ssh_del_keypair(42); |
| 428 | assert_int_equal(ret, -1); |
| 429 | ret = nc_client_ssh_get_keypair_count(); |
| 430 | assert_int_equal(ret, 1); |
| 431 | |
| 432 | /* remove remaining keypairs */ |
| 433 | ret = nc_client_ssh_del_keypair(0); |
| 434 | assert_int_equal(ret, 0); |
| 435 | ret = nc_client_ssh_get_keypair_count(); |
| 436 | assert_int_equal(ret, 0); |
| 437 | } |
| 438 | |
| 439 | static void |
| 440 | test_nc_client_ssh_setting_auth_pref(void **state) |
| 441 | { |
| 442 | (void)state; |
| 443 | int ret; |
| 444 | |
David Sedlák | aae4df3 | 2018-10-08 22:27:22 +0200 | [diff] [blame] | 445 | /* initiate client, must be called in first test */ |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 446 | nc_client_init(); |
| 447 | |
| 448 | /* check default prefference settings according to documentation */ |
| 449 | ret = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_INTERACTIVE); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 450 | assert_int_equal(ret, 1); |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 451 | ret = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_PASSWORD); |
| 452 | assert_int_equal(ret, 2); |
| 453 | ret = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_PUBLICKEY); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 454 | assert_int_equal(ret, 3); |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 455 | |
| 456 | /* try to set prefetence of non existing method */ |
| 457 | nc_client_ssh_set_auth_pref(42, 22); |
| 458 | |
| 459 | /* try to get preference of non existing method */ |
| 460 | ret = nc_client_ssh_get_auth_pref(42); |
| 461 | assert_int_equal(ret, 0); |
| 462 | |
| 463 | /* change values of all methods and check if they actually changed */ |
| 464 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, 9); |
| 465 | ret = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_INTERACTIVE); |
| 466 | assert_int_equal(ret, 9); |
| 467 | |
| 468 | /* negative value should be set as -1 */ |
| 469 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -5); |
| 470 | ret = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_PASSWORD); |
| 471 | assert_int_equal(ret, -1); |
| 472 | |
| 473 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 11); |
| 474 | ret = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_PUBLICKEY); |
| 475 | assert_int_equal(ret, 11); |
| 476 | } |
| 477 | |
| 478 | static void |
| 479 | test_nc_client_ssh_setting_username(void **state) |
| 480 | { |
| 481 | (void)state; |
| 482 | int ret; |
| 483 | const char *username_ret; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 484 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 485 | username_ret = nc_client_ssh_get_username(); |
| 486 | /* username is set to "username" in setup_f */ |
| 487 | assert_string_equal(username_ret, "username"); |
| 488 | |
| 489 | /* set new username and check if it changes */ |
| 490 | ret = nc_client_ssh_set_username("new_username"); |
| 491 | assert_int_equal(ret, 0); |
| 492 | username_ret = nc_client_ssh_get_username(); |
| 493 | assert_string_equal(username_ret, "new_username"); |
| 494 | } |
| 495 | |
| 496 | static void |
| 497 | test_nc_connect_ssh_interactive_succesfull(void **state) |
| 498 | { |
| 499 | (void)state; |
| 500 | struct nc_session *session; |
| 501 | |
| 502 | /* set authentication method to use interactive authentication */ |
| 503 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, 1); |
| 504 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| 505 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1); |
| 506 | |
| 507 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, 20); |
| 508 | |
| 509 | /* prepare return values for functions used by nc_connect_ssh */ |
| 510 | will_return(__wrap_connect, 0); |
| 511 | will_return(__wrap_ssh_connect, 0); |
| 512 | will_return(__wrap_ssh_userauth_none, 1); |
| 513 | |
| 514 | will_return(__wrap_ssh_userauth_kbdint, 0); |
| 515 | will_return(__wrap_ssh_is_connected, 1); |
| 516 | will_return(__wrap_ssh_is_connected, 1); |
| 517 | |
| 518 | will_return(__wrap_ssh_channel_open_session, 0); |
| 519 | will_return(__wrap_ssh_channel_request_subsystem, 0); |
| 520 | |
| 521 | will_return(__wrap_nc_handshake_io, 3); |
| 522 | will_return(__wrap_nc_ctx_check_and_fill, 0); |
| 523 | |
| 524 | session = nc_connect_ssh("127.0.0.1", 8080, NULL); |
| 525 | assert_non_null(session); |
| 526 | |
Michal Vasko | 0ab3a66 | 2021-07-26 12:17:41 +0200 | [diff] [blame] | 527 | will_return(__wrap_ssh_channel_poll_timeout, 0); |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 528 | nc_session_free(session, NULL); |
| 529 | } |
| 530 | |
| 531 | static void |
| 532 | test_nc_connect_ssh_password_succesfull(void **state) |
| 533 | { |
| 534 | (void)state; |
| 535 | struct nc_session *session; |
| 536 | |
| 537 | /* set authentication method to use password authentication */ |
| 538 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, 1); |
| 539 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1); |
| 540 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| 541 | |
| 542 | /* set authentication callback */ |
| 543 | nc_client_ssh_set_auth_password_clb(test_pwd_clb1, "private_data"); |
| 544 | will_return(test_pwd_clb1, "secret password"); |
| 545 | /* set values that are expected as parameters for authentication callback */ |
| 546 | expect_string(test_pwd_clb1, username, "username"); |
| 547 | expect_string(test_pwd_clb1, hostname, "127.0.0.1"); |
| 548 | expect_string(test_pwd_clb1, priv, "private_data"); |
| 549 | |
| 550 | /* fake succesfull connection */ |
| 551 | will_return(__wrap_connect, 0); |
| 552 | will_return(__wrap_ssh_connect, 0); |
| 553 | /* do not authenticate using no authentication method */ |
| 554 | will_return(__wrap_ssh_userauth_none, 1); |
| 555 | |
| 556 | /* succesfully authenticate via password authentication */ |
| 557 | expect_string(__wrap_ssh_userauth_password, password, "secret password"); |
| 558 | expect_string(__wrap_ssh_userauth_password, username, "username"); |
| 559 | will_return(__wrap_ssh_userauth_password, 0); |
| 560 | |
| 561 | /* fake ssh functions that are used to open netconf channel */ |
| 562 | will_return(__wrap_ssh_channel_open_session, 0); |
| 563 | will_return(__wrap_ssh_channel_request_subsystem, 0); |
| 564 | |
| 565 | /* fake that connection is still alive*/ |
| 566 | will_return(__wrap_ssh_is_connected, 1); |
| 567 | |
| 568 | /* fake ssh function for recieving hello message */ |
| 569 | will_return(__wrap_ssh_is_connected, 1); |
| 570 | |
| 571 | will_return(__wrap_nc_handshake_io, 3); |
| 572 | will_return(__wrap_nc_ctx_check_and_fill, 0); |
| 573 | |
| 574 | session = nc_connect_ssh("127.0.0.1", 8080, NULL); |
| 575 | assert_non_null(session); |
| 576 | |
| 577 | /* disconnect */ |
Michal Vasko | 0ab3a66 | 2021-07-26 12:17:41 +0200 | [diff] [blame] | 578 | will_return(__wrap_ssh_channel_poll_timeout, 0); |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 579 | nc_session_free(session, NULL); |
| 580 | } |
| 581 | |
| 582 | static void |
David Sedlák | 77acc20 | 2018-10-08 21:44:14 +0200 | [diff] [blame] | 583 | test_nc_connect_ssh_pubkey_succesfull(void **state) |
| 584 | { |
| 585 | (void)state; |
| 586 | struct nc_session *session; |
| 587 | int ret = 0; |
| 588 | |
| 589 | /* set authentication method to use password authentication */ |
| 590 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| 591 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1); |
| 592 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| 593 | |
| 594 | /* add keypair for authentication */ |
Michal Vasko | 7a4213f | 2022-09-08 11:02:34 +0200 | [diff] [blame^] | 595 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_ecdsa.pub", TESTS_DIR "/data/key_ecdsa"); |
David Sedlák | 77acc20 | 2018-10-08 21:44:14 +0200 | [diff] [blame] | 596 | assert_int_equal(ret, 0); |
| 597 | |
| 598 | /* fake succesfull connection */ |
| 599 | will_return(__wrap_connect, 0); |
| 600 | will_return(__wrap_ssh_connect, 0); |
| 601 | /* do not authenticate using no authentication method */ |
| 602 | will_return(__wrap_ssh_userauth_none, 1); |
| 603 | will_return(__wrap_ssh_userauth_try_publickey, 0); |
| 604 | will_return(__wrap_ssh_userauth_publickey, 0); |
| 605 | will_return(__wrap_ssh_is_connected, 1); |
| 606 | will_return(__wrap_ssh_channel_open_session, 0); |
| 607 | will_return(__wrap_ssh_channel_request_subsystem, 0); |
| 608 | |
| 609 | /* fake ssh function for recieving hello message */ |
| 610 | will_return(__wrap_ssh_is_connected, 1); |
| 611 | |
| 612 | will_return(__wrap_nc_handshake_io, 3); |
| 613 | will_return(__wrap_nc_ctx_check_and_fill, 0); |
| 614 | session = nc_connect_ssh("127.0.0.1", 8080, NULL); |
| 615 | assert_non_null(session); |
| 616 | |
| 617 | /* disconnect */ |
Michal Vasko | 0ab3a66 | 2021-07-26 12:17:41 +0200 | [diff] [blame] | 618 | will_return(__wrap_ssh_channel_poll_timeout, 0); |
David Sedlák | 77acc20 | 2018-10-08 21:44:14 +0200 | [diff] [blame] | 619 | nc_session_free(session, NULL); |
| 620 | } |
| 621 | |
| 622 | static void |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 623 | test_nc_connect_connection_failed(void **state) |
| 624 | { |
| 625 | (void)state; |
| 626 | struct nc_session *session; |
| 627 | |
| 628 | errno = ECONNREFUSED; |
| 629 | will_return(__wrap_connect, -1); |
| 630 | will_return(__wrap_ssh_is_connected, 0); |
| 631 | |
| 632 | session = nc_connect_ssh("127.0.0.1", 8080, NULL); |
| 633 | assert_null(session); |
| 634 | } |
| 635 | |
| 636 | static void |
| 637 | test_nc_connect_ssh_bad_hello(void **state) |
| 638 | { |
| 639 | (void)state; |
| 640 | struct nc_session *session; |
| 641 | |
| 642 | /* set authentication method to use interactive authentication */ |
| 643 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, 1); |
| 644 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| 645 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1); |
| 646 | |
| 647 | nc_client_ssh_set_auth_password_clb(test_pwd_clb2, NULL); |
| 648 | |
| 649 | will_return(__wrap_connect, 0); |
| 650 | will_return(__wrap_ssh_connect, 0); |
| 651 | will_return(__wrap_ssh_userauth_none, 1); |
| 652 | |
| 653 | will_return(__wrap_ssh_userauth_kbdint, 0); |
| 654 | will_return(__wrap_ssh_is_connected, 1); |
| 655 | will_return(__wrap_ssh_is_connected, 1); |
| 656 | |
| 657 | will_return(__wrap_ssh_channel_open_session, 0); |
| 658 | will_return(__wrap_ssh_channel_request_subsystem, 0); |
| 659 | will_return(__wrap_nc_handshake_io, 4); |
| 660 | |
| 661 | session = nc_connect_ssh("127.0.0.1", 8080, NULL); |
| 662 | assert_null(session); |
| 663 | |
David Sedlák | aae4df3 | 2018-10-08 22:27:22 +0200 | [diff] [blame] | 664 | /* destroy client, must be called in last test */ |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 665 | nc_client_destroy(); |
| 666 | } |
| 667 | |
Fred Gan | 3a736e0 | 2021-01-04 17:59:38 +0800 | [diff] [blame] | 668 | static void |
| 669 | test_nc_client_ssh_ch_setting_username(void **state) |
| 670 | { |
| 671 | (void)state; |
| 672 | const char *username_ret; |
| 673 | int ret; |
| 674 | |
| 675 | /* username is set to "ch_username" in setup_f */ |
| 676 | username_ret = nc_client_ssh_ch_get_username(); |
| 677 | assert_string_equal(username_ret, "ch_username"); |
| 678 | /* set new username and check if it changes */ |
| 679 | ret = nc_client_ssh_ch_set_username("new_ch_username"); |
| 680 | assert_int_equal(ret, 0); |
| 681 | username_ret = nc_client_ssh_ch_get_username(); |
| 682 | assert_string_equal(username_ret, "new_ch_username"); |
| 683 | } |
| 684 | |
| 685 | static void |
| 686 | test_nc_client_ssh_ch_add_bind_listen(void **state) |
| 687 | { |
| 688 | (void)state; |
| 689 | int ret; |
| 690 | |
| 691 | /* invalid parameters, address NULL or port 0 */ |
| 692 | ret = nc_client_ssh_ch_add_bind_listen(NULL, 4334); |
| 693 | assert_int_equal(ret, -1); |
| 694 | ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 0); |
| 695 | assert_int_equal(ret, -1); |
| 696 | |
| 697 | /* failed to create an ssh listening socket */ |
| 698 | will_return(__wrap_nc_sock_listen_inet, -1); |
| 699 | ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 4334); |
| 700 | assert_int_equal(ret, -1); |
| 701 | |
| 702 | /* fake a successful CH ssh listening socket */ |
| 703 | will_return(__wrap_nc_sock_listen_inet, 1); |
| 704 | ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 4334); |
| 705 | assert_int_equal(ret, 0); |
| 706 | |
| 707 | /* remove ssh listening client binds */ |
| 708 | ret = nc_client_ssh_ch_del_bind("127.0.0.1", 4334); |
| 709 | assert_int_equal(ret, 0); |
| 710 | } |
| 711 | |
| 712 | static void |
| 713 | test_nc_accept_callhome(void **state) |
| 714 | { |
| 715 | (void)state; |
| 716 | struct nc_session *session = NULL; |
| 717 | int timeout = 10; |
| 718 | int ret; |
| 719 | |
| 720 | /* invalid parameter session */ |
| 721 | ret = nc_accept_callhome(timeout, NULL, NULL); |
| 722 | assert_int_equal(ret, -1); |
| 723 | |
| 724 | /* no client bind */ |
| 725 | ret = nc_accept_callhome(timeout, NULL, &session); |
| 726 | assert_int_equal(ret, -1); |
| 727 | |
| 728 | /* successfully add a client Call Home bind */ |
| 729 | will_return(__wrap_nc_sock_listen_inet, 1); |
| 730 | ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 4334); |
| 731 | assert_int_equal(ret, 0); |
| 732 | |
| 733 | /* failed to accept a client bind */ |
| 734 | will_return(__wrap_nc_sock_accept_binds, -1); |
| 735 | ret = nc_accept_callhome(timeout, NULL, &session); |
| 736 | assert_int_equal(ret, -1); |
| 737 | |
| 738 | /* failed to accept a server Call Home connection */ |
| 739 | will_return(__wrap_nc_accept_callhome_ssh_sock, NULL); |
| 740 | will_return(__wrap_nc_sock_accept_binds, 2); |
| 741 | ret = nc_accept_callhome(timeout, NULL, &session); |
| 742 | assert_int_equal(ret, -1); |
| 743 | |
| 744 | /* create session structure to fake a successful server call home connection */ |
| 745 | session = nc_new_session(NC_CLIENT, 0); |
| 746 | assert_non_null(session); |
| 747 | will_return(__wrap_nc_sock_accept_binds, 2); |
| 748 | will_return(__wrap_nc_accept_callhome_ssh_sock, session); |
| 749 | ret = nc_accept_callhome(timeout, NULL, &session); |
| 750 | assert_int_equal(ret, 1); |
| 751 | |
| 752 | /* remove ssh listening client binds */ |
| 753 | ret = nc_client_ssh_ch_del_bind("127.0.0.1", 4334); |
| 754 | assert_int_equal(ret, 0); |
| 755 | |
| 756 | /* free session */ |
| 757 | nc_session_free(session, NULL); |
| 758 | } |
| 759 | |
| 760 | static void |
| 761 | test_nc_client_ssh_callhome_successful(void **state) |
| 762 | { |
| 763 | (void)state; |
| 764 | struct nc_session *session = NULL; |
| 765 | int timeout = 10; |
| 766 | int ret; |
| 767 | |
| 768 | /* create session structure */ |
| 769 | session = nc_new_session(NC_CLIENT, 0); |
| 770 | assert_non_null(session); |
| 771 | |
| 772 | /* prepare to fake return values for functions used by nc_accept_callhome */ |
| 773 | will_return(__wrap_nc_sock_listen_inet, 1); |
| 774 | will_return(__wrap_nc_sock_accept_binds, 2); |
| 775 | will_return(__wrap_nc_accept_callhome_ssh_sock, session); |
| 776 | |
| 777 | ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 4334); |
| 778 | assert_int_equal(ret, 0); |
| 779 | ret = nc_accept_callhome(timeout, NULL, &session); |
| 780 | assert_int_equal(ret, 1); |
| 781 | |
| 782 | /* remove ssh listening client binds */ |
| 783 | ret = nc_client_ssh_ch_del_bind("127.0.0.1", 4334); |
| 784 | assert_int_equal(ret, 0); |
| 785 | |
| 786 | /* free session */ |
| 787 | nc_session_free(session, NULL); |
| 788 | } |
| 789 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 790 | int |
| 791 | main(void) |
| 792 | { |
| 793 | const struct CMUnitTest tests[] = { |
| 794 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_setting_auth_pref, setup_f, teardown_f), |
| 795 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_setting_auth_hostkey_check_clb, setup_f, teardown_f), |
| 796 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_setting_auth_password_clb, setup_f, teardown_f), |
| 797 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_setting_auth_interactive_clb, setup_f, teardown_f), |
| 798 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_setting_auth_privkey_passphrase_clb, setup_f, teardown_f), |
| 799 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_adding_keypair, setup_f, teardown_f), |
| 800 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_setting_username, setup_f, teardown_f), |
| 801 | cmocka_unit_test_setup_teardown(test_nc_connect_ssh_interactive_succesfull, setup_f, teardown_f), |
| 802 | cmocka_unit_test_setup_teardown(test_nc_connect_ssh_password_succesfull, setup_f, teardown_f), |
David Sedlák | 77acc20 | 2018-10-08 21:44:14 +0200 | [diff] [blame] | 803 | cmocka_unit_test_setup_teardown(test_nc_connect_ssh_pubkey_succesfull, setup_f, teardown_f), |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 804 | cmocka_unit_test_setup_teardown(test_nc_connect_connection_failed, setup_f, teardown_f), |
| 805 | cmocka_unit_test_setup_teardown(test_nc_connect_ssh_bad_hello, setup_f, teardown_f), |
Fred Gan | 3a736e0 | 2021-01-04 17:59:38 +0800 | [diff] [blame] | 806 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_ch_setting_username, setup_f, teardown_f), |
| 807 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_ch_add_bind_listen, setup_f, teardown_f), |
| 808 | cmocka_unit_test_setup_teardown(test_nc_accept_callhome, setup_f, teardown_f), |
| 809 | cmocka_unit_test_setup_teardown(test_nc_client_ssh_callhome_successful, setup_f, teardown_f), |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 810 | }; |
| 811 | |
| 812 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 813 | } |