Skip to content

Commit 3a736e0

Browse files
authored
tests FEATURE callhome testcases (#280)
1 parent 5eda9cb commit 3a736e0

2 files changed

Lines changed: 166 additions & 1 deletion

File tree

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(test test_client_ssh)
77
set(${test}_mock_funcs connect ssh_connect ssh_userauth_none ssh_userauth_kbdint ssh_is_connected
88
ssh_channel_open_session ssh_channel_request_subsystem ssh_channel_is_close ssh_channel_write
99
ssh_channel_poll_timeout ssh_userauth_password nc_handshake_io nc_ctx_check_and_fill
10-
ssh_userauth_try_publickey ssh_userauth_publickey)
10+
ssh_userauth_try_publickey ssh_userauth_publickey nc_sock_listen_inet nc_sock_accept_binds nc_accept_callhome_ssh_sock)
1111
set(${test}_wrap_link_flags "-Wl")
1212
foreach(mock_func IN LISTS ${test}_mock_funcs)
1313
set(${test}_wrap_link_flags "${${test}_wrap_link_flags},--wrap=${mock_func}")

tests/client/test_client_ssh.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <cmocka.h>
99
#include <libyang/libyang.h>
1010
#include <session_client.h>
11+
#include <session_client_ch.h>
12+
#include <session_p.h>
1113
#include <log.h>
1214
#include <config.h>
1315
#include "tests/config.h"
@@ -36,6 +38,8 @@ setup_f(void **state)
3638

3739
ret = nc_client_ssh_set_username("username");
3840
assert_int_equal(ret, 0);
41+
ret = nc_client_ssh_ch_set_username("ch_username");
42+
assert_int_equal(ret, 0);
3943
nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb, NULL);
4044

4145
return 0;
@@ -186,6 +190,41 @@ __wrap_ssh_userauth_publickey(ssh_session session, const char *username, const s
186190
return (int)mock();
187191
}
188192

193+
MOCK int
194+
__wrap_nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka)
195+
{
196+
(void)address;
197+
(void)port;
198+
(void)ka;
199+
200+
return (int)mock();
201+
}
202+
203+
MOCK int
204+
__wrap_nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx)
205+
{
206+
(void)binds;
207+
(void)bind_count;
208+
(void)timeout;
209+
(void)host;
210+
(void)port;
211+
212+
*idx = 0;
213+
return (int)mock();
214+
}
215+
216+
MOCK struct nc_session *
217+
__wrap_nc_accept_callhome_ssh_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout)
218+
{
219+
(void)sock;
220+
(void)host;
221+
(void)port;
222+
(void)ctx;
223+
(void)timeout;
224+
225+
return mock_ptr_type(struct nc_session *);
226+
}
227+
189228
static int
190229
test_hostkey_clb(const char *hostname, ssh_session session, void *priv)
191230
{
@@ -621,6 +660,128 @@ test_nc_connect_ssh_bad_hello(void **state)
621660
nc_client_destroy();
622661
}
623662

663+
static void
664+
test_nc_client_ssh_ch_setting_username(void **state)
665+
{
666+
(void)state;
667+
const char *username_ret;
668+
int ret;
669+
670+
/* username is set to "ch_username" in setup_f */
671+
username_ret = nc_client_ssh_ch_get_username();
672+
assert_string_equal(username_ret, "ch_username");
673+
/* set new username and check if it changes */
674+
ret = nc_client_ssh_ch_set_username("new_ch_username");
675+
assert_int_equal(ret, 0);
676+
username_ret = nc_client_ssh_ch_get_username();
677+
assert_string_equal(username_ret, "new_ch_username");
678+
}
679+
680+
static void
681+
test_nc_client_ssh_ch_add_bind_listen(void **state)
682+
{
683+
(void)state;
684+
int ret;
685+
686+
/* invalid parameters, address NULL or port 0 */
687+
ret = nc_client_ssh_ch_add_bind_listen(NULL, 4334);
688+
assert_int_equal(ret, -1);
689+
ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 0);
690+
assert_int_equal(ret, -1);
691+
692+
/* failed to create an ssh listening socket */
693+
will_return(__wrap_nc_sock_listen_inet, -1);
694+
ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 4334);
695+
assert_int_equal(ret, -1);
696+
697+
/* fake a successful CH 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, 0);
701+
702+
/* remove ssh listening client binds */
703+
ret = nc_client_ssh_ch_del_bind("127.0.0.1", 4334);
704+
assert_int_equal(ret, 0);
705+
}
706+
707+
static void
708+
test_nc_accept_callhome(void **state)
709+
{
710+
(void)state;
711+
struct nc_session *session = NULL;
712+
int timeout = 10;
713+
int ret;
714+
715+
/* invalid parameter session */
716+
ret = nc_accept_callhome(timeout, NULL, NULL);
717+
assert_int_equal(ret, -1);
718+
719+
/* no client bind */
720+
ret = nc_accept_callhome(timeout, NULL, &session);
721+
assert_int_equal(ret, -1);
722+
723+
/* successfully add a client Call Home bind */
724+
will_return(__wrap_nc_sock_listen_inet, 1);
725+
ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 4334);
726+
assert_int_equal(ret, 0);
727+
728+
/* failed to accept a client bind */
729+
will_return(__wrap_nc_sock_accept_binds, -1);
730+
ret = nc_accept_callhome(timeout, NULL, &session);
731+
assert_int_equal(ret, -1);
732+
733+
/* failed to accept a server Call Home connection */
734+
will_return(__wrap_nc_accept_callhome_ssh_sock, NULL);
735+
will_return(__wrap_nc_sock_accept_binds, 2);
736+
ret = nc_accept_callhome(timeout, NULL, &session);
737+
assert_int_equal(ret, -1);
738+
739+
/* create session structure to fake a successful server call home connection */
740+
session = nc_new_session(NC_CLIENT, 0);
741+
assert_non_null(session);
742+
will_return(__wrap_nc_sock_accept_binds, 2);
743+
will_return(__wrap_nc_accept_callhome_ssh_sock, session);
744+
ret = nc_accept_callhome(timeout, NULL, &session);
745+
assert_int_equal(ret, 1);
746+
747+
/* remove ssh listening client binds */
748+
ret = nc_client_ssh_ch_del_bind("127.0.0.1", 4334);
749+
assert_int_equal(ret, 0);
750+
751+
/* free session */
752+
nc_session_free(session, NULL);
753+
}
754+
755+
static void
756+
test_nc_client_ssh_callhome_successful(void **state)
757+
{
758+
(void)state;
759+
struct nc_session *session = NULL;
760+
int timeout = 10;
761+
int ret;
762+
763+
/* create session structure */
764+
session = nc_new_session(NC_CLIENT, 0);
765+
assert_non_null(session);
766+
767+
/* prepare to fake return values for functions used by nc_accept_callhome */
768+
will_return(__wrap_nc_sock_listen_inet, 1);
769+
will_return(__wrap_nc_sock_accept_binds, 2);
770+
will_return(__wrap_nc_accept_callhome_ssh_sock, session);
771+
772+
ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", 4334);
773+
assert_int_equal(ret, 0);
774+
ret = nc_accept_callhome(timeout, NULL, &session);
775+
assert_int_equal(ret, 1);
776+
777+
/* remove ssh listening client binds */
778+
ret = nc_client_ssh_ch_del_bind("127.0.0.1", 4334);
779+
assert_int_equal(ret, 0);
780+
781+
/* free session */
782+
nc_session_free(session, NULL);
783+
}
784+
624785
int
625786
main(void)
626787
{
@@ -637,6 +798,10 @@ main(void)
637798
cmocka_unit_test_setup_teardown(test_nc_connect_ssh_pubkey_succesfull, setup_f, teardown_f),
638799
cmocka_unit_test_setup_teardown(test_nc_connect_connection_failed, setup_f, teardown_f),
639800
cmocka_unit_test_setup_teardown(test_nc_connect_ssh_bad_hello, setup_f, teardown_f),
801+
cmocka_unit_test_setup_teardown(test_nc_client_ssh_ch_setting_username, setup_f, teardown_f),
802+
cmocka_unit_test_setup_teardown(test_nc_client_ssh_ch_add_bind_listen, setup_f, teardown_f),
803+
cmocka_unit_test_setup_teardown(test_nc_accept_callhome, setup_f, teardown_f),
804+
cmocka_unit_test_setup_teardown(test_nc_client_ssh_callhome_successful, setup_f, teardown_f),
640805
};
641806

642807
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)