]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Update prefork and postfork NSS code for unit tests.
authorNick Mathewson <nickm@torproject.org>
Wed, 29 Aug 2018 12:39:19 +0000 (08:39 -0400)
committerNick Mathewson <nickm@torproject.org>
Tue, 4 Sep 2018 18:52:35 +0000 (14:52 -0400)
src/ext/tinytest.c
src/lib/crypt_ops/crypto_init.c
src/lib/crypt_ops/crypto_init.h
src/lib/crypt_ops/crypto_nss_mgt.c
src/lib/crypt_ops/crypto_nss_mgt.h
src/test/testing_common.c

index 2a475bd917fd1dceccb9d7e6a2ece5782902d2ca..052fb6483fb6f92a9ee71fe524d091b8dca14a39 100644 (file)
@@ -120,8 +120,10 @@ testcase_run_bare_(const struct testcase_t *testcase)
 #ifndef NO_FORKING
 
 #ifdef TINYTEST_POSTFORK
+void tinytest_prefork(void);
 void tinytest_postfork(void);
 #else
+static void tinytest_prefork(void) { }
 static void tinytest_postfork(void) { }
 #endif
 
@@ -185,16 +187,17 @@ testcase_run_forked_(const struct testgroup_t *group,
 
        if (opt_verbosity>0)
                printf("[forking] ");
+       tinytest_prefork();
        pid = fork();
 #ifdef FORK_BREAKS_GCOV
        vproc_transaction_begin(0);
 #endif
+       tinytest_postfork();
        if (!pid) {
                /* child. */
                int test_r, write_r;
                char b[1];
                close(outcome_pipe[0]);
-               tinytest_postfork();
                test_r = testcase_run_bare_(testcase);
                assert(0<=(int)test_r && (int)test_r<=2);
                b[0] = "NYS"[test_r];
index f9b077e9e7641610a4ab7cd511d99582fd40db14..6fc8e5d94c523a9cf8049e59d50431a00f3dd979 100644 (file)
@@ -58,7 +58,7 @@ crypto_early_init(void)
     crypto_openssl_early_init();
 #endif
 #ifdef ENABLE_NSS
-    crypto_nss_early_init();
+    crypto_nss_early_init(0);
 #endif
 
     if (crypto_seed_rng() < 0)
@@ -134,6 +134,16 @@ crypto_global_cleanup(void)
   return 0;
 }
 
+/** Run operations that the crypto library requires to be happy again
+ * after forking. */
+void
+crypto_prefork(void)
+{
+#ifdef ENABLE_NSS
+  crypto_nss_prefork();
+#endif
+}
+
 /** Run operations that the crypto library requires to be happy again
  * after forking. */
 void
index 05b281720c92a34f5e2f1bee1d1725df0370e6a8..5b6d65d48cd04fbff0d5245e743affc64d81b8ad 100644 (file)
@@ -24,6 +24,7 @@ int crypto_global_init(int hardwareAccel,
 
 void crypto_thread_cleanup(void);
 int crypto_global_cleanup(void);
+void crypto_prefork(void);
 void crypto_postfork(void);
 
 const char *crypto_get_library_name(void);
index 187f556bd247b97c1417b74864ed05ff1e66d733..a1da74aff57ed48194daa3705b24c38cbd465b0d 100644 (file)
@@ -50,10 +50,12 @@ nss_password_func_always_fail(PK11SlotInfo *slot,
 }
 
 void
-crypto_nss_early_init(void)
+crypto_nss_early_init(int nss_only)
 {
-  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
-  PK11_SetPasswordFunc(nss_password_func_always_fail);
+  if (! nss_only) {
+    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
+    PK11_SetPasswordFunc(nss_password_func_always_fail);
+  }
 
   /* Eventually we should use NSS_Init() instead -- but that wants a
      directory. The documentation says that we can't use this if we want
@@ -111,6 +113,14 @@ crypto_nss_late_init(void)
 
 void
 crypto_nss_global_cleanup(void)
+{
+  NSS_Shutdown();
+  PL_ArenaFinish();
+  PR_Cleanup();
+}
+
+void
+crypto_nss_prefork(void)
 {
   NSS_Shutdown();
 }
@@ -118,6 +128,5 @@ crypto_nss_global_cleanup(void)
 void
 crypto_nss_postfork(void)
 {
-  crypto_nss_global_cleanup();
-  crypto_nss_early_init();
+  crypto_nss_early_init(1);
 }
index c4c94f4d8959e742b76e4db945251e462209e1cd..27793dcc457581ad093b003e96d650f2cbfa15ba 100644 (file)
@@ -22,11 +22,12 @@ const char *crypto_nss_get_header_version_str(void);
 
 void crypto_nss_log_errors(int severity, const char *doing);
 
-void crypto_nss_early_init(void);
+void crypto_nss_early_init(int nss_only);
 int crypto_nss_late_init(void);
 
 void crypto_nss_global_cleanup(void);
 
+void crypto_nss_prefork(void);
 void crypto_nss_postfork(void);
 #endif
 
index 17a0756803a9af55fcc8a73fdc1f232c709d1f4b..42b5190ca066c38b6e85906ddc2347cd6e077216 100644 (file)
@@ -223,11 +223,17 @@ an_assertion_failed(void)
   tinytest_set_test_failed_();
 }
 
+void tinytest_prefork(void);
 void tinytest_postfork(void);
 void
-tinytest_postfork(void)
+tinytest_prefork(void)
 {
   free_pregenerated_keys();
+  crypto_prefork();
+}
+void
+tinytest_postfork(void)
+{
   crypto_postfork();
   init_pregenerated_keys();
 }