aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorAnderson Toshiyuki Sasaki <ansasaki@redhat.com>2018-07-04 13:51:39 +0200
committerAndreas Schneider <asn@cryptomilk.org>2018-08-03 16:43:03 +0200
commitedcdef94eeb3d43ab61017ce3527976b012d2a13 (patch)
treeeb9e3ce43b32243b5ad3552cddfdbdfccbe1dfac /tests/unittests
parent04cc7b769a359ca2afdcfc0b70b5e8a17849b556 (diff)
downloadlibssh-edcdef94eeb3d43ab61017ce3527976b012d2a13.tar.gz
libssh-edcdef94eeb3d43ab61017ce3527976b012d2a13.tar.xz
libssh-edcdef94eeb3d43ab61017ce3527976b012d2a13.zip
tests: Add test for init running on threads
The test runs ssh_init()/ssh_finalize() on multiple threads. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/CMakeLists.txt2
-rw-r--r--tests/unittests/torture_threads_init.c95
2 files changed, 97 insertions, 0 deletions
diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt
index e833481a..aa67b033 100644
--- a/tests/unittests/CMakeLists.txt
+++ b/tests/unittests/CMakeLists.txt
@@ -35,6 +35,8 @@ if (UNIX AND NOT WIN32)
if (CMAKE_USE_PTHREADS_INIT)
add_cmocka_test(torture_rand torture_rand.c ${TORTURE_LIBRARY})
target_link_libraries(torture_rand Threads::Threads)
+ add_cmocka_test(torture_threads_init torture_threads_init.c ${TORTURE_LIBRARY})
+ target_link_libraries(torture_threads_init Threads::Threads)
# Not working correctly
#if (WITH_SERVER)
# add_cmocka_test(torture_server_x11 torture_server_x11.c ${TORTURE_LIBRARY})
diff --git a/tests/unittests/torture_threads_init.c b/tests/unittests/torture_threads_init.c
new file mode 100644
index 00000000..1b0781e5
--- /dev/null
+++ b/tests/unittests/torture_threads_init.c
@@ -0,0 +1,95 @@
+/*
+ * This file is part of the SSH Library
+ *
+ * Copyright (c) 2018 by Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
+ *
+ * The SSH Library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * The SSH Library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the SSH Library; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#define LIBSSH_STATIC
+
+#include "torture.h"
+#include "libssh/libssh.h"
+
+#include <pthread.h>
+
+#define NUM_THREADS 20
+
+static int run_on_threads(void *(*func)(void *))
+{
+ pthread_t threads[NUM_THREADS];
+ int rc;
+ int i;
+
+ for (i = 0; i < NUM_THREADS; ++i) {
+ rc = pthread_create(&threads[i], NULL, func, NULL);
+ assert_int_equal(rc, 0);
+ }
+
+ for (i = 0; i < NUM_THREADS; ++i) {
+ void *p = NULL;
+ uint64_t *result;
+
+ rc = pthread_join(threads[i], &p);
+ assert_int_equal(rc, 0);
+
+ result = (uint64_t *)p;
+ assert_null(result);
+ }
+
+ return rc;
+}
+
+static void *thread_ssh_init(void *threadid)
+{
+ int rc;
+
+ (void) threadid;
+
+ rc = ssh_init();
+ assert_int_equal(rc, SSH_OK);
+
+ rc = ssh_finalize();
+ assert_int_equal(rc, SSH_OK);
+
+ pthread_exit(NULL);
+}
+
+static void torture_ssh_init(void **state)
+{
+ int rc;
+
+ /* Unused */
+ (void) state;
+
+ rc = run_on_threads(thread_ssh_init);
+ assert_int_equal(rc, 0);
+}
+
+int torture_run_tests(void)
+{
+ int rc;
+ struct CMUnitTest tests[] = {
+ cmocka_unit_test(torture_ssh_init),
+ };
+
+ torture_filter_tests(tests);
+ rc = cmocka_run_group_tests(tests, NULL, NULL);
+
+ return rc;
+}