Debian Codegraph @

tests/unittests/torture_threads_init.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 by Anderson Toshiyuki Sasaki <[email protected]>
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "torture.h"
27 #include "libssh/libssh.h"
28
29 #include <pthread.h>
30
31 #define NUM_THREADS 20
32
33 static int run_on_threads(void *(*func)(void *))
34 {
35 pthread_t threads[NUM_THREADS];
36 int rc;
37 int i;
38
39 for (i = 0; i < NUM_THREADS; ++i) {
40 rc = pthread_create(&threads[i], NULL, func, NULL);
41 assert_int_equal(rc, 0);
42 }
43
44 for (i = 0; i < NUM_THREADS; ++i) {
45 void *p = NULL;
46 uint64_t *result;
47
48 rc = pthread_join(threads[i], &p);
49 assert_int_equal(rc, 0);
50
51 result = (uint64_t *)p;
52 assert_null(result);
53 }
54
55 return rc;
56 }
57
58 static void *thread_ssh_init(UNUSED_PARAM(void *threadid))
59 {
60 int rc;
61
62 (void) threadid;
63
64 rc = ssh_init();
65 assert_int_equal(rc, SSH_OK);
66
67 rc = ssh_finalize();
68 assert_int_equal(rc, SSH_OK);
69
70 pthread_exit(NULL);
71 }
72
73 static void torture_ssh_init(UNUSED_PARAM(void **state))
74 {
75 int rc;
76
77 rc = run_on_threads(thread_ssh_init);
78 assert_int_equal(rc, 0);
79 }
80
81 int torture_run_tests(void)
82 {
83 int rc;
84 struct CMUnitTest tests[] = {
85 cmocka_unit_test(torture_ssh_init),
86 };
87
88 /*
89 * If the library is statically linked, ssh_init() is not called
90 * automatically
91 */
92 ssh_init();
93 torture_filter_tests(tests);
94 rc = cmocka_run_group_tests(tests, NULL, NULL);
95 ssh_finalize();
96
97 return rc;
98 }