diff options
author | Anderson Toshiyuki Sasaki <ansasaki@redhat.com> | 2020-04-06 12:07:28 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2020-05-05 14:31:53 +0200 |
commit | a99b8a397973baa57ecef0f8da3024893bda30b5 (patch) | |
tree | 091f9a38201ed9e786dfffa06751c755888fcfd5 | |
parent | 0a450f02513d4502c570360fdbb188d647374066 (diff) | |
download | libssh-a99b8a397973baa57ecef0f8da3024893bda30b5.tar.gz libssh-a99b8a397973baa57ecef0f8da3024893bda30b5.tar.xz libssh-a99b8a397973baa57ecef0f8da3024893bda30b5.zip |
init: Introduce internal is_ssh_initialized()
The introduced function returns whether the library is initialized or
not.
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit dba2114ed78796caa06fc9d53268cb9cc302699f)
-rw-r--r-- | include/libssh/priv.h | 3 | ||||
-rw-r--r-- | src/init.c | 19 | ||||
-rw-r--r-- | tests/unittests/torture_init.c | 23 |
3 files changed, 45 insertions, 0 deletions
diff --git a/include/libssh/priv.h b/include/libssh/priv.h index 31405d6b..3e910860 100644 --- a/include/libssh/priv.h +++ b/include/libssh/priv.h @@ -32,6 +32,7 @@ #include <stdint.h> #include <stdlib.h> #include <string.h> +#include <stdbool.h> #if !defined(HAVE_STRTOULL) # if defined(HAVE___STRTOULL) @@ -423,4 +424,6 @@ void explicit_bzero(void *s, size_t n); void ssh_agent_state_free(void *data); +bool is_ssh_initialized(void); + #endif /* _LIBSSH_PRIV_H */ @@ -261,4 +261,23 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, #endif /* _WIN32 */ +/** + * @internal + * @brief Return whether the library is initialized + * + * @returns true if the library is initialized; false otherwise. + * + * @see ssh_init() + */ +bool is_ssh_initialized() { + + bool is_initialized = false; + + ssh_mutex_lock(&ssh_init_mutex); + is_initialized = _ssh_initialized > 0; + ssh_mutex_unlock(&ssh_init_mutex); + + return is_initialized; +} + /** @} */ diff --git a/tests/unittests/torture_init.c b/tests/unittests/torture_init.c index 6a97a182..f719fc99 100644 --- a/tests/unittests/torture_init.c +++ b/tests/unittests/torture_init.c @@ -2,6 +2,7 @@ #define LIBSSH_STATIC +#include <errno.h> #include "torture.h" #include "libssh/libssh.h" @@ -32,11 +33,33 @@ static void torture_ssh_init_after_finalize(void **state) { assert_int_equal(rc, SSH_OK); } +static void torture_is_ssh_initialized(UNUSED_PARAM(void **state)) { + + int rc; + bool initialized = false; + + /* Make sure the library is not initialized */ + while (is_ssh_initialized()) { + rc = ssh_finalize(); + assert_return_code(rc, errno); + } + + rc = ssh_init(); + assert_return_code(rc, errno); + initialized = is_ssh_initialized(); + assert_true(initialized); + rc = ssh_finalize(); + assert_return_code(rc, errno); + initialized = is_ssh_initialized(); + assert_false(initialized); +} + int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { cmocka_unit_test(torture_ssh_init), cmocka_unit_test(torture_ssh_init_after_finalize), + cmocka_unit_test(torture_is_ssh_initialized), }; torture_filter_tests(tests); |