diff options
author | Anderson Toshiyuki Sasaki <ansasaki@redhat.com> | 2020-04-08 15:08:34 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2020-05-05 14:31:59 +0200 |
commit | 3025eeace35511b76657e8419281edbb03b78cd2 (patch) | |
tree | 1cd4fb3e0718cd8c27dca66d620748c4e9b39950 | |
parent | ffb000776819eedc1ef0fd1c279b529e12a26b75 (diff) | |
download | libssh-3025eeace35511b76657e8419281edbb03b78cd2.tar.gz libssh-3025eeace35511b76657e8419281edbb03b78cd2.tar.xz libssh-3025eeace35511b76657e8419281edbb03b78cd2.zip |
client: Check if the library is initialized in ssh_connect()
If the library is not initialized, SSH_ERROR is returned and the error
message is set properly.
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit e3e3a2786362f89fc72cd8a8a67fd52a435a1597)
-rw-r--r-- | src/client.c | 7 | ||||
-rw-r--r-- | tests/client/torture_connect.c | 33 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/client.c b/src/client.c index d26b99a2..36d3ea2c 100644 --- a/src/client.c +++ b/src/client.c @@ -509,6 +509,13 @@ int ssh_connect(ssh_session session) { int ret; + if (!is_ssh_initialized()) { + ssh_set_error(session, SSH_FATAL, + "Library not initialized."); + + return SSH_ERROR; + } + if (session == NULL) { return SSH_ERROR; } diff --git a/tests/client/torture_connect.c b/tests/client/torture_connect.c index 0ad8ef55..f31f521f 100644 --- a/tests/client/torture_connect.c +++ b/tests/client/torture_connect.c @@ -183,6 +183,38 @@ static void torture_connect_socket(void **state) { assert_ssh_return_code(session, rc); } +static void torture_connect_uninitialized(UNUSED_PARAM(void **state)) +{ + int rc; + ssh_session session; + struct passwd *pwd; + + /* Make sure the library is unitialized */ + while (is_ssh_initialized()) { + rc = ssh_finalize(); + assert_return_code(rc, errno); + } + + pwd = getpwnam("bob"); + assert_non_null(pwd); + + rc = setuid(pwd->pw_uid); + assert_return_code(rc, errno); + + session = ssh_new(); + assert_non_null(session); + + rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); + assert_ssh_return_code(session, rc); + + /* Expect error from ssh_connect */ + rc = ssh_connect(session); + assert_false(rc == SSH_OK); + assert_string_equal(ssh_get_error(session), "Library not initialized."); + + ssh_free(session); +} + int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { @@ -193,6 +225,7 @@ int torture_run_tests(void) { cmocka_unit_test_setup_teardown(torture_connect_timeout, session_setup, session_teardown), #endif cmocka_unit_test_setup_teardown(torture_connect_socket, session_setup, session_teardown), + cmocka_unit_test(torture_connect_uninitialized), }; ssh_init(); |