Debian Codegraph @

tests/unittests/torture_rand.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4 #include <libssh/priv.h>
5 #include <libssh/callbacks.h>
6 #include <pthread.h>
7 #include <errno.h>
8 #include "torture.h"
9
10 #ifdef HAVE_LIBGCRYPT
11 #define NUM_LOOPS 1000
12 #else
13 /* openssl is much faster */
14 #define NUM_LOOPS 20000
15 #endif
16 #define NUM_THREADS 100
17
18 static int setup(void **state) {
19 int rc;
20
21 (void) state;
22
23 ssh_threads_set_callbacks(ssh_threads_get_pthread());
24 rc = ssh_init();
25 if (rc != SSH_OK) {
26 return -1;
27 }
28
29 return 0;
30 }
31
32 static int teardown(void **state) {
33 (void) state;
34
35 ssh_finalize();
36
37 return 0;
38 }
39
40 static void *torture_rand_thread(void *threadid) {
41 char buffer[12];
42 int i;
43 int ok;
44
45 (void) threadid;
46
47 buffer[0] = buffer[1] = buffer[10] = buffer[11] = 'X';
48 for(i = 0; i < NUM_LOOPS; ++i) {
49 ok = ssh_get_random(&buffer[2], i % 8 + 1, 0);
50 assert_true(ok);
51 }
52
53 pthread_exit(NULL);
54 }
55
56 static void torture_rand_threading(void **state) {
57 pthread_t threads[NUM_THREADS];
58 int i;
59 int err;
60
61 (void) state;
62
63 for(i = 0; i < NUM_THREADS; ++i) {
64 err = pthread_create(&threads[i], NULL, torture_rand_thread, NULL);
65 assert_int_equal(err, 0);
66 }
67 for(i = 0; i < NUM_THREADS; ++i) {
68 err=pthread_join(threads[i], NULL);
69 assert_int_equal(err, 0);
70 }
71 }
72
73 int torture_run_tests(void) {
74 int rc;
75 struct CMUnitTest tests[] = {
76 cmocka_unit_test_setup_teardown(torture_rand_threading, setup, teardown),
77 };
78
79 torture_filter_tests(tests);
80 rc = cmocka_run_group_tests(tests, NULL, NULL);
81
82 return rc;
83 }