diff options
Diffstat (limited to 'tests/client')
-rw-r--r-- | tests/client/CMakeLists.txt | 6 | ||||
-rw-r--r-- | tests/client/torture_auth.c | 370 |
2 files changed, 171 insertions, 205 deletions
diff --git a/tests/client/CMakeLists.txt b/tests/client/CMakeLists.txt index 7dbeb087..1a1af7e6 100644 --- a/tests/client/CMakeLists.txt +++ b/tests/client/CMakeLists.txt @@ -3,7 +3,6 @@ project(clienttests C) find_package(socket_wrapper) add_cmocka_test(torture_algorithms torture_algorithms.c ${TORTURE_LIBRARY}) -add_cmocka_test(torture_auth torture_auth.c ${TORTURE_LIBRARY}) add_cmocka_test(torture_knownhosts torture_knownhosts.c ${TORTURE_LIBRARY}) add_cmocka_test(torture_proxycommand torture_proxycommand.c ${TORTURE_LIBRARY}) add_cmocka_test(torture_session torture_session.c ${TORTURE_LIBRARY}) @@ -16,7 +15,8 @@ if (WITH_SFTP) endif (WITH_SFTP) set(LIBSSH_CLIENT_TESTS - torture_connect) + torture_connect + torture_auth) foreach(_CLI_TEST ${LIBSSH_CLIENT_TESTS}) add_cmocka_test(${_CLI_TEST} ${_CLI_TEST}.c ${TORTURE_LIBRARY}) @@ -32,6 +32,6 @@ foreach(_CLI_TEST ${LIBSSH_CLIENT_TESTS}) TEST ${_CLI_TEST} PROPERTY - ENVIRONMENT LD_PRELOAD=${SOCKET_WRAPPER_LIBRARY}) + ENVIRONMENT ${TORTURE_ENVIRONMENT}) endif() endforeach() diff --git a/tests/client/torture_auth.c b/tests/client/torture_auth.c index d686b4c5..6342ec14 100644 --- a/tests/client/torture_auth.c +++ b/tests/client/torture_auth.c @@ -27,41 +27,111 @@ #include "libssh/session.h" #include "agent.c" -static int setup(void **state) { +static int sshd_setup(void **state) +{ + torture_setup_sshd_server(state); + + return 0; +} + +static int sshd_teardown(void **state) { + torture_teardown_sshd_server(state); + + return 0; +} + +static int session_setup(void **state) +{ + struct torture_state *s = *state; int verbosity = torture_libssh_verbosity(); - ssh_session session = ssh_new(); - ssh_options_set(session, SSH_OPTIONS_HOST, "localhost"); - ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); + s->ssh.session = ssh_new(); + assert_non_null(s->ssh.session); - *state = session; + ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); + ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); return 0; } -static int teardown(void **state) { - ssh_disconnect(*state); - ssh_free(*state); +static int session_teardown(void **state) +{ + struct torture_state *s = *state; + + ssh_disconnect(s->ssh.session); + ssh_free(s->ssh.session); return 0; } -static void torture_auth_autopubkey(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); +static int pubkey_setup(void **state) +{ int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; + rc = session_setup(state); + if (rc != 0) { + return rc; + } + + return 0; +} + +static void torture_auth_none(void **state) { + struct torture_state *s = *state; + ssh_session session = s->ssh.session; + int rc; + + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER1); + assert_int_equal(rc, SSH_OK); + + rc = ssh_connect(session); + assert_int_equal(rc, SSH_OK); + + rc = ssh_userauth_none(session,NULL); + assert_int_equal(rc, SSH_AUTH_DENIED); + + /* This request should return a SSH_REQUEST_DENIED error */ + if (rc == SSH_ERROR) { + assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED); + } +} + +static void torture_auth_none_nonblocking(void **state) { + struct torture_state *s = *state; + ssh_session session = s->ssh.session; + int rc; + + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER2); + assert_int_equal(rc, SSH_OK); + + rc = ssh_connect(session); + assert_int_equal(rc, SSH_OK); + + /* This request should return a SSH_REQUEST_DENIED error */ + if (rc == SSH_ERROR) { + assert_true(ssh_get_error_code(session) == SSH_REQUEST_DENIED); } - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + ssh_set_blocking(session,0); + + do { + rc = ssh_userauth_none(session,NULL); + } while (rc == SSH_AUTH_AGAIN); + assert_int_equal(rc, SSH_AUTH_DENIED); + assert_true(ssh_get_error_code(session) == SSH_REQUEST_DENIED); + +} + +static void torture_auth_autopubkey(void **state) { + struct torture_state *s = *state; + ssh_session session = s->ssh.session; + int rc; + + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER1); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); rc = ssh_userauth_none(session,NULL); /* This request should return a SSH_REQUEST_DENIED error */ @@ -72,25 +142,19 @@ static void torture_auth_autopubkey(void **state) { assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY); rc = ssh_userauth_publickey_auto(session, NULL, NULL); - assert_true(rc == SSH_AUTH_SUCCESS); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } static void torture_auth_autopubkey_nonblocking(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); + struct torture_state *s = *state; + ssh_session session = s->ssh.session; int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } - - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER1); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); ssh_set_blocking(session,0); do { @@ -108,32 +172,19 @@ static void torture_auth_autopubkey_nonblocking(void **state) { do { rc = ssh_userauth_publickey_auto(session, NULL, NULL); } while (rc == SSH_AUTH_AGAIN); - assert_true(rc == SSH_AUTH_SUCCESS); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } static void torture_auth_kbdint(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); - char *password = getenv("TORTURE_PASSWORD"); + struct torture_state *s = *state; + ssh_session session = s->ssh.session; int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } - - if (password == NULL) { - print_message("*** Please set the environment variable " - "TORTURE_PASSWORD to enable this test!!\n"); - return; - } - - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER1); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); rc = ssh_userauth_none(session,NULL); /* This request should return a SSH_REQUEST_DENIED error */ @@ -144,10 +195,10 @@ static void torture_auth_kbdint(void **state) { assert_true(rc & SSH_AUTH_METHOD_INTERACTIVE); rc = ssh_userauth_kbdint(session, NULL, NULL); - assert_true(rc == SSH_AUTH_INFO); + assert_int_equal(rc, SSH_AUTH_INFO); assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 1); - rc = ssh_userauth_kbdint_setanswer(session, 0, password); + rc = ssh_userauth_kbdint_setanswer(session, 0, TORTURE_SSH_USER1_PASSWORD); assert_false(rc < 0); rc = ssh_userauth_kbdint(session, NULL, NULL); @@ -156,32 +207,19 @@ static void torture_auth_kbdint(void **state) { assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 0); rc = ssh_userauth_kbdint(session, NULL, NULL); } - assert_true(rc == SSH_AUTH_SUCCESS); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } static void torture_auth_kbdint_nonblocking(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); - char *password = getenv("TORTURE_PASSWORD"); + struct torture_state *s = *state; + ssh_session session = s->ssh.session; int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } - - if (password == NULL) { - print_message("*** Please set the environment variable " - "TORTURE_PASSWORD to enable this test!!\n"); - return; - } - - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER2); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); ssh_set_blocking(session,0); do { @@ -198,10 +236,10 @@ static void torture_auth_kbdint_nonblocking(void **state) { do { rc = ssh_userauth_kbdint(session, NULL, NULL); } while (rc == SSH_AUTH_AGAIN); - assert_true(rc == SSH_AUTH_INFO); + assert_int_equal(rc, SSH_AUTH_INFO); assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 1); do { - rc = ssh_userauth_kbdint_setanswer(session, 0, password); + rc = ssh_userauth_kbdint_setanswer(session, 0, TORTURE_SSH_USER2_PASSWORD); } while (rc == SSH_AUTH_AGAIN); assert_false(rc < 0); @@ -215,32 +253,19 @@ static void torture_auth_kbdint_nonblocking(void **state) { rc = ssh_userauth_kbdint(session, NULL, NULL); } while (rc == SSH_AUTH_AGAIN); } - assert_true(rc == SSH_AUTH_SUCCESS); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } static void torture_auth_password(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); - char *password = getenv("TORTURE_PASSWORD"); + struct torture_state *s = *state; + ssh_session session = s->ssh.session; int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } - - if (password == NULL) { - print_message("*** Please set the environment variable " - "TORTURE_PASSWORD to enable this test!!\n"); - return; - } - - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER1); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); rc = ssh_userauth_none(session, NULL); /* This request should return a SSH_REQUEST_DENIED error */ @@ -250,33 +275,20 @@ static void torture_auth_password(void **state) { rc = ssh_userauth_list(session, NULL); assert_true(rc & SSH_AUTH_METHOD_PASSWORD); - rc = ssh_userauth_password(session, NULL, password); - assert_true(rc == SSH_AUTH_SUCCESS); + rc = ssh_userauth_password(session, NULL, TORTURE_SSH_USER1_PASSWORD); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } static void torture_auth_password_nonblocking(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); - char *password = getenv("TORTURE_PASSWORD"); + struct torture_state *s = *state; + ssh_session session = s->ssh.session; int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } - - if (password == NULL) { - print_message("*** Please set the environment variable " - "TORTURE_PASSWORD to enable this test!!\n"); - return; - } - - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER1); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); ssh_set_blocking(session,0); do { @@ -292,31 +304,26 @@ static void torture_auth_password_nonblocking(void **state) { assert_true(rc & SSH_AUTH_METHOD_PASSWORD); do { - rc = ssh_userauth_password(session, NULL, password); + rc = ssh_userauth_password(session, NULL, TORTURE_SSH_USER1_PASSWORD); } while(rc==SSH_AUTH_AGAIN); - assert_true(rc == SSH_AUTH_SUCCESS); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } static void torture_auth_agent(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); + struct torture_state *s = *state; + ssh_session session = s->ssh.session; int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } if (!agent_is_running(session)){ print_message("*** Agent not running. Test ignored\n"); return; } - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER1); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); rc = ssh_userauth_none(session,NULL); /* This request should return a SSH_REQUEST_DENIED error */ @@ -327,28 +334,23 @@ static void torture_auth_agent(void **state) { assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY); rc = ssh_userauth_agent(session, NULL); - assert_true(rc == SSH_AUTH_SUCCESS); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } static void torture_auth_agent_nonblocking(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); + struct torture_state *s = *state; + ssh_session session = s->ssh.session; int rc; - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } if (!agent_is_running(session)){ print_message("*** Agent not running. Test ignored\n"); return; } - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); + rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER2); + assert_int_equal(rc, SSH_OK); rc = ssh_connect(session); - assert_true(rc == SSH_OK); + assert_int_equal(rc, SSH_OK); rc = ssh_userauth_none(session,NULL); /* This request should return a SSH_REQUEST_DENIED error */ @@ -363,84 +365,48 @@ static void torture_auth_agent_nonblocking(void **state) { do { rc = ssh_userauth_agent(session, NULL); } while (rc == SSH_AUTH_AGAIN); - assert_true(rc == SSH_AUTH_SUCCESS); + assert_int_equal(rc, SSH_AUTH_SUCCESS); } -static void torture_auth_none(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); - int rc; - - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); - - rc = ssh_connect(session); - assert_true(rc == SSH_OK); - - rc = ssh_userauth_none(session,NULL); - - assert_true(rc == SSH_AUTH_DENIED); - /* This request should return a SSH_REQUEST_DENIED error */ - if (rc == SSH_ERROR) { - assert_true(ssh_get_error_code(session) == SSH_REQUEST_DENIED); - } -} - -static void torture_auth_none_nonblocking(void **state) { - ssh_session session = *state; - char *user = getenv("TORTURE_USER"); - int rc; - - if (user == NULL) { - print_message("*** Please set the environment variable TORTURE_USER" - " to enable this test!!\n"); - return; - } - rc = ssh_options_set(session, SSH_OPTIONS_USER, user); - assert_true(rc == SSH_OK); - - rc = ssh_connect(session); - assert_true(rc == SSH_OK); - - /* This request should return a SSH_REQUEST_DENIED error */ - if (rc == SSH_ERROR) { - assert_true(ssh_get_error_code(session) == SSH_REQUEST_DENIED); - } - - ssh_set_blocking(session,0); - - do { - rc = ssh_userauth_none(session,NULL); - } while (rc == SSH_AUTH_AGAIN); - assert_true(rc == SSH_AUTH_DENIED); - assert_true(ssh_get_error_code(session) == SSH_REQUEST_DENIED); - -} - int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(torture_auth_kbdint, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_kbdint_nonblocking, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_password, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_password_nonblocking, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_autopubkey, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_autopubkey_nonblocking, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_agent, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_agent_nonblocking, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_none, setup, teardown), - cmocka_unit_test_setup_teardown(torture_auth_none_nonblocking, setup, teardown), + cmocka_unit_test_setup_teardown(torture_auth_none, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_none_nonblocking, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_password, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_password_nonblocking, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_kbdint, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_kbdint_nonblocking, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_autopubkey, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_autopubkey_nonblocking, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_agent, + session_setup, + session_teardown), + cmocka_unit_test_setup_teardown(torture_auth_agent_nonblocking, + session_setup, + session_teardown), }; ssh_init(); torture_filter_tests(tests); - rc = cmocka_run_group_tests(tests, NULL, NULL); + rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); ssh_finalize(); return rc; |