Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / crypto_init.cc
1 #include <errno.h>
2 #include <time.h>
3 #include <pthread.h>
4 #include <unistd.h>
5 #include <vector>
6
7 #include "include/types.h"
8 #include "common/code_environment.h"
9 #include "global/global_context.h"
10 #include "global/global_init.h"
11 #include "include/msgr.h"
12 #include "gtest/gtest.h"
13 #include "auth/Crypto.h"
14 #include "common/ceph_crypto.h"
15
16 #ifdef USE_NSS
17 void *init_crypto(void *p) {
18   ceph::crypto::init(g_ceph_context);
19   return NULL;
20 }
21
22 // Tests for a race condition in libnss when calling crypto_init
23 // multiple times simultaneously from different threads.
24 TEST(CRYPTO_INIT, NSS_RACE) {
25   std::vector<const char*> args;
26   auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
27                          CODE_ENVIRONMENT_UTILITY,
28                          CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
29   // Most reliably reproduced with more threads than cores.
30   long n_thread = sysconf(_SC_NPROCESSORS_ONLN) * 2;
31   pthread_t *ts = (pthread_t*)malloc(n_thread * sizeof(pthread_t));
32   int i;
33   for (i = 0; i < n_thread; i++) {
34     pthread_create(&ts[i], NULL, init_crypto, NULL);
35   }
36   for (i = 0; i < n_thread; i++) {
37     int k;
38     void *p = (void*)&k;
39     pthread_join(ts[i], &p);
40   }
41   free(ts);
42 }
43
44 #endif
45
46 int main(int argc, char **argv) {
47   ::testing::InitGoogleTest(&argc, argv);
48   return RUN_ALL_TESTS();
49 }