]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Add unit test for race condition in libnss 1433/head
authorSharif Olorin <sio@tesser.org>
Thu, 13 Mar 2014 07:36:00 +0000 (18:36 +1100)
committerSharif Olorin <sio@tesser.org>
Thu, 13 Mar 2014 14:22:07 +0000 (01:22 +1100)
This isn't in test/crypto.cc because common_init_finish is called prior
to running any tests. Will not build the test function if Ceph hasn't
been configured with NSS.

Signed-off-by: Sharif Olorin <sio@tesser.org>
src/test/Makefile.am
src/test/crypto_init.cc [new file with mode: 0644]

index 1f07a757a76e7a56e3be1775c8b16792863d994a..5e4cd955d0a9f76b2cbb5ae26400caf9b850992d 100644 (file)
@@ -426,6 +426,11 @@ unittest_crypto_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
 unittest_crypto_CXXFLAGS = $(UNITTEST_CXXFLAGS)
 check_PROGRAMS += unittest_crypto
 
+unittest_crypto_init_SOURCES = test/crypto_init.cc
+unittest_crypto_init_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
+unittest_crypto_init_CXXFLAGS = $(UNITTEST_CXXFLAGS)
+check_PROGRAMS += unittest_crypto_init
+
 unittest_perf_counters_SOURCES = test/perf_counters.cc
 unittest_perf_counters_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
 unittest_perf_counters_CXXFLAGS = $(UNITTEST_CXXFLAGS)
diff --git a/src/test/crypto_init.cc b/src/test/crypto_init.cc
new file mode 100644 (file)
index 0000000..d4b2562
--- /dev/null
@@ -0,0 +1,48 @@
+#include <errno.h>
+#include <time.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <vector>
+
+#include "include/types.h"
+#include "common/code_environment.h"
+#include "global/global_context.h"
+#include "global/global_init.h"
+#include "include/msgr.h"
+#include "gtest/gtest.h"
+#include "auth/Crypto.h"
+#include "common/ceph_crypto.h"
+
+#ifdef USE_NSS
+void *init_crypto(void *p) {
+  ceph::crypto::init(g_ceph_context);
+  return NULL;
+}
+
+// Tests for a race condition in libnss when calling crypto_init
+// multiple times simultaneously from different threads.
+TEST(CRYPTO_INIT, NSS_RACE) {
+  std::vector<const char*> args;
+  global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY,
+             CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
+  // Most reliably reproduced with more threads than cores.
+  long n_thread = sysconf(_SC_NPROCESSORS_ONLN) * 2;
+  pthread_t *ts = (pthread_t*)malloc(n_thread * sizeof(pthread_t));
+  int i;
+  for (i = 0; i < n_thread; i++) {
+    pthread_create(&ts[i], NULL, init_crypto, NULL);
+  }
+  for (i = 0; i < n_thread; i++) {
+    int k;
+    void *p = (void*)&k;
+    pthread_join(ts[i], &p);
+  }
+  free(ts);
+}
+
+#endif
+
+int main(int argc, char **argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}