]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Start work on fancy compiler tricks to expose extra stuff to our tests
authorNick Mathewson <nickm@torproject.org>
Thu, 6 Jun 2013 18:56:05 +0000 (14:56 -0400)
committerNick Mathewson <nickm@torproject.org>
Wed, 10 Jul 2013 19:20:09 +0000 (15:20 -0400)
This is mainly a matter of automake trickery: we build each static
library in two versions now: one with the TOR_UNIT_TESTS macro
defined, and one without.  When TOR_UNIT_TESTS is defined, we can
enable mocking and expose more functions. When it's not defined, we
can lock the binary down more.

The alternatives would be to have alternate build modes: a "testing
configuration" for building the libraries with test support, and a
"production configuration" for building them without.  I don't favor
that approach, since I think it would mean more people runnning
binaries build for testing, or more people not running unit tests.

.gitignore
changes/fancy_testing [new file with mode: 0644]
configure.ac
src/common/include.am
src/common/testsupport.h [new file with mode: 0644]
src/or/include.am
src/test/include.am

index e48918d32c3721e5df33b6383a3aff50f2858cbd..bb3817ba57d9980397c21bf2e3eca3fd1dc7bcd5 100644 (file)
 /src/common/Makefile.in
 /src/common/common_sha1.i
 /src/common/libor.a
+/src/common/libor-testing.a
 /src/common/libor.lib
 /src/common/libor-crypto.a
+/src/common/libor-crypto-testing.a
 /src/common/libor-crypto.lib
 /src/common/libor-event.a
+/src/common/libor-event-testing.a
 /src/common/libor-event.lib
 /src/common/libcurve25519_donna.a
 /src/common/libcurve25519_donna.lib
 /src/or/tor
 /src/or/tor.exe
 /src/or/libtor.a
+/src/or/libtor-testing.a
 /src/or/libtor.lib
 
 # /src/test
diff --git a/changes/fancy_testing b/changes/fancy_testing
new file mode 100644 (file)
index 0000000..4d97194
--- /dev/null
@@ -0,0 +1,9 @@
+  o Build features:
+
+    - Tor now builds each source file in two modes: a mode that avoids
+      exposing identifiers needlessly, and another mode that exposes
+      more identifiers for testing. This lets the compiler do better at
+      optimizing the production code, while enabling us to take more
+      radical measures to let the unit tests test things.
+
+
index 235f19b4383908bf2ef51cf55435c0a0f372f453..34ed524dd85533ee2b608403d82507b17a7f3557 100644 (file)
@@ -39,6 +39,10 @@ AC_ARG_ENABLE(static-tor,
    AS_HELP_STRING(--enable-static-tor, Create an entirely static Tor binary. Requires --with-openssl-dir and --with-libevent-dir and --with-zlib-dir))
 AC_ARG_ENABLE(curve25519,
    AS_HELP_STRING(--disable-curve25519, Build Tor with no curve25519 elliptic-curve crypto support))
+AC_ARG_ENABLE(unittests,
+   AS_HELP_STRING(--disable-unittests, [Don't build unit tests for Tor. Risky!]))
+
+AM_CONDITIONAL(UNITTESTS_ENABLED, test x$unittests != xno)
 
 if test "$enable_static_tor" = "yes"; then
   enable_static_libevent="yes";
index 68275cbcf7132033b210d10477e4183645b65139..3d8cc8e9ebdd2c8b6b2bbd81c37b79c2f3363116 100644 (file)
@@ -1,5 +1,15 @@
 
-noinst_LIBRARIES+= src/common/libor.a src/common/libor-crypto.a src/common/libor-event.a
+noinst_LIBRARIES += \
+       src/common/libor.a \
+       src/common/libor-crypto.a \
+       src/common/libor-event.a
+
+if UNITTESTS_ENABLED
+noinst_LIBRARIES += \
+       src/common/libor-testing.a \
+       src/common/libor-crypto-testing.a \
+       src/common/libor-event-testing.a
+endif
 
 EXTRA_DIST+= \
   src/common/common_sha1.i     \
@@ -38,7 +48,7 @@ if CURVE25519_ENABLED
 libcrypto_extra_source=src/common/crypto_curve25519.c
 endif
 
-src_common_libor_a_SOURCES = \
+LIBOR_A_SOURCES = \
   src/common/address.c                                 \
   src/common/compat.c                                  \
   src/common/container.c                               \
@@ -51,7 +61,7 @@ src_common_libor_a_SOURCES = \
   src/common/util_codedigest.c                         \
   $(libor_extra_source)
 
-src_common_libor_crypto_a_SOURCES = \
+LIBOR_CRYPTO_A_SOURCES = \
   src/common/aes.c             \
   src/common/crypto.c          \
   src/common/crypto_format.c   \
@@ -59,7 +69,19 @@ src_common_libor_crypto_a_SOURCES = \
   src/common/tortls.c          \
   $(libcrypto_extra_source)
 
-src_common_libor_event_a_SOURCES = src/common/compat_libevent.c
+LIBOR_EVENT_A_SOURCES = src/common/compat_libevent.c
+
+src_common_libor_a_SOURCES = $(LIBOR_A_SOURCES)
+src_common_libor_crypto_a_SOURCES = $(LIBOR_CRYPTO_A_SOURCES)
+src_common_libor_event_a_SOURCES = $(LIBOR_EVENT_A_SOURCES)
+
+src_common_libor_testing_a_SOURCES = $(LIBOR_A_SOURCES)
+src_common_libor_crypto_testing_a_SOURCES = $(LIBOR_CRYPTO_A_SOURCES)
+src_common_libor_event_testing_a_SOURCES = $(LIBOR_EVENT_A_SOURCES)
+
+src_common_libor_testing_a_CPPFLAGS = -DTOR_UNIT_TESTS $(AM_CPPFLAGS)
+src_common_libor_crypto_testing_a_CPPFLAGS = -DTOR_UNIT_TESTS $(AM_CPPFLAGS)
+src_common_libor_event_testing_a_CPPFLAGS = -DTOR_UNIT_TESTS $(AM_CPPFLAGS)
 
 COMMONHEADERS = \
   src/common/address.h                         \
@@ -74,6 +96,7 @@ COMMONHEADERS = \
   src/common/memarea.h                         \
   src/common/mempool.h                         \
   src/common/procmon.h                         \
+  src/common/testsupport.h                     \
   src/common/torgzip.h                         \
   src/common/torint.h                          \
   src/common/torlog.h                          \
diff --git a/src/common/testsupport.h b/src/common/testsupport.h
new file mode 100644 (file)
index 0000000..621fd8c
--- /dev/null
@@ -0,0 +1,14 @@
+/* Copyright (c) 2013, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_TESTSUPPORT_H
+#define TOR_TESTSUPPORT_H
+
+#ifdef TOR_UNIT_TESTS
+#define STATIC_UNLESS_TESTING
+#else
+#define STATIC_UNLESS_TESTING static
+#endif
+
+#endif
+
index 65dbeff53e8956074424212e66f16e40ec0d6472..91b9bfce728e2e9b161f9ceaabbb181dba814699 100644 (file)
@@ -1,5 +1,10 @@
 bin_PROGRAMS+= src/or/tor
-noinst_LIBRARIES+= src/or/libtor.a
+noinst_LIBRARIES += \
+       src/or/libtor.a
+if UNITTESTS_ENABLED
+noinst_LIBRARIES += \
+       src/or/libtor-testing.a
+endif
 
 if BUILD_NT_SERVICES
 tor_platform_source=src/or/ntmain.c
@@ -21,7 +26,7 @@ else
 onion_ntor_source=
 endif
 
-src_or_libtor_a_SOURCES = \
+LIBTOR_A_SOURCES = \
        src/or/addressmap.c                             \
        src/or/buffers.c                                \
        src/or/channel.c                                \
@@ -77,6 +82,9 @@ src_or_libtor_a_SOURCES = \
        $(onion_ntor_source)                            \
        src/or/config_codedigest.c
 
+src_or_libtor_a_SOURCES = $(LIBTOR_A_SOURCES)
+src_or_libtor_testing_a_SOURCES = $(LIBTOR_A_SOURCES)
+
 #libtor_a_LIBADD = ../common/libor.a ../common/libor-crypto.a \
 #      ../common/libor-event.a
 
@@ -90,6 +98,8 @@ AM_CPPFLAGS += -DSHARE_DATADIR="\"$(datadir)\"" \
         -DLOCALSTATEDIR="\"$(localstatedir)\"" \
         -DBINDIR="\"$(bindir)\""
 
+src_or_libtor_testing_a_CPPFLAGS = -DTOR_UNIT_TESTS $(AM_CPPFLAGS)
+
 # -L flags need to go in LDFLAGS. -l flags need to go in LDADD.
 # This seems to matter nowhere but on windows, but I assure you that it
 # matters a lot there, and is quite hard to debug if you forget to do it.
index 112d1a79d85148184bca53d5e264eea5eef5a135..08eb7fba6715fcc1da69e06e4db76d94c99569fb 100644 (file)
@@ -1,11 +1,15 @@
 TESTS+= src/test/test
 
-noinst_PROGRAMS+= src/test/test src/test/test-child src/test/bench
+noinst_PROGRAMS+= src/test/bench
+if UNITTESTS_ENABLED
+noinst_PROGRAMS+= src/test/test src/test/test-child
+endif
 
 src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
         -DLOCALSTATEDIR="\"$(localstatedir)\"" \
         -DBINDIR="\"$(bindir)\""              \
-       -I"$(top_srcdir)/src/or" -I"$(top_srcdir)/src/ext"
+       -I"$(top_srcdir)/src/or" -I"$(top_srcdir)/src/ext" \
+       -DTOR_UNIT_TESTS
 
 # -L flags need to go in LDFLAGS. -l flags need to go in LDADD.
 # This seems to matter nowhere but on Windows, but I assure you that it
@@ -36,9 +40,9 @@ src_test_bench_CPPFLAGS= $(src_test_AM_CPPFLAGS)
 
 src_test_test_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
         @TOR_LDFLAGS_libevent@
-src_test_test_LDADD = src/or/libtor.a src/common/libor.a \
-       src/common/libor-crypto.a $(LIBDONNA) \
-       src/common/libor-event.a \
+src_test_test_LDADD = src/or/libtor-testing.a src/common/libor-testing.a \
+       src/common/libor-crypto-testing.a $(LIBDONNA) \
+       src/common/libor-event-testing.a \
        @TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
        @TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@