Start work on fancy compiler tricks to expose extra stuff to our tests

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.
This commit is contained in:
Nick Mathewson 2013-06-06 14:56:05 -04:00
parent fab99844fc
commit f7d654b81e
7 changed files with 79 additions and 11 deletions

4
.gitignore vendored
View File

@ -128,10 +128,13 @@
/src/common/Makefile.in /src/common/Makefile.in
/src/common/common_sha1.i /src/common/common_sha1.i
/src/common/libor.a /src/common/libor.a
/src/common/libor-testing.a
/src/common/libor.lib /src/common/libor.lib
/src/common/libor-crypto.a /src/common/libor-crypto.a
/src/common/libor-crypto-testing.a
/src/common/libor-crypto.lib /src/common/libor-crypto.lib
/src/common/libor-event.a /src/common/libor-event.a
/src/common/libor-event-testing.a
/src/common/libor-event.lib /src/common/libor-event.lib
/src/common/libcurve25519_donna.a /src/common/libcurve25519_donna.a
/src/common/libcurve25519_donna.lib /src/common/libcurve25519_donna.lib
@ -150,6 +153,7 @@
/src/or/tor /src/or/tor
/src/or/tor.exe /src/or/tor.exe
/src/or/libtor.a /src/or/libtor.a
/src/or/libtor-testing.a
/src/or/libtor.lib /src/or/libtor.lib
# /src/test # /src/test

9
changes/fancy_testing Normal file
View File

@ -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.

View 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)) 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, AC_ARG_ENABLE(curve25519,
AS_HELP_STRING(--disable-curve25519, Build Tor with no curve25519 elliptic-curve crypto support)) 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 if test "$enable_static_tor" = "yes"; then
enable_static_libevent="yes"; enable_static_libevent="yes";

View 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+= \ EXTRA_DIST+= \
src/common/common_sha1.i \ src/common/common_sha1.i \
@ -38,7 +48,7 @@ if CURVE25519_ENABLED
libcrypto_extra_source=src/common/crypto_curve25519.c libcrypto_extra_source=src/common/crypto_curve25519.c
endif endif
src_common_libor_a_SOURCES = \ LIBOR_A_SOURCES = \
src/common/address.c \ src/common/address.c \
src/common/compat.c \ src/common/compat.c \
src/common/container.c \ src/common/container.c \
@ -51,7 +61,7 @@ src_common_libor_a_SOURCES = \
src/common/util_codedigest.c \ src/common/util_codedigest.c \
$(libor_extra_source) $(libor_extra_source)
src_common_libor_crypto_a_SOURCES = \ LIBOR_CRYPTO_A_SOURCES = \
src/common/aes.c \ src/common/aes.c \
src/common/crypto.c \ src/common/crypto.c \
src/common/crypto_format.c \ src/common/crypto_format.c \
@ -59,7 +69,19 @@ src_common_libor_crypto_a_SOURCES = \
src/common/tortls.c \ src/common/tortls.c \
$(libcrypto_extra_source) $(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 = \ COMMONHEADERS = \
src/common/address.h \ src/common/address.h \
@ -74,6 +96,7 @@ COMMONHEADERS = \
src/common/memarea.h \ src/common/memarea.h \
src/common/mempool.h \ src/common/mempool.h \
src/common/procmon.h \ src/common/procmon.h \
src/common/testsupport.h \
src/common/torgzip.h \ src/common/torgzip.h \
src/common/torint.h \ src/common/torint.h \
src/common/torlog.h \ src/common/torlog.h \

14
src/common/testsupport.h Normal file
View File

@ -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

View File

@ -1,5 +1,10 @@
bin_PROGRAMS+= src/or/tor 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 if BUILD_NT_SERVICES
tor_platform_source=src/or/ntmain.c tor_platform_source=src/or/ntmain.c
@ -21,7 +26,7 @@ else
onion_ntor_source= onion_ntor_source=
endif endif
src_or_libtor_a_SOURCES = \ LIBTOR_A_SOURCES = \
src/or/addressmap.c \ src/or/addressmap.c \
src/or/buffers.c \ src/or/buffers.c \
src/or/channel.c \ src/or/channel.c \
@ -77,6 +82,9 @@ src_or_libtor_a_SOURCES = \
$(onion_ntor_source) \ $(onion_ntor_source) \
src/or/config_codedigest.c 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 \ #libtor_a_LIBADD = ../common/libor.a ../common/libor-crypto.a \
# ../common/libor-event.a # ../common/libor-event.a
@ -90,6 +98,8 @@ AM_CPPFLAGS += -DSHARE_DATADIR="\"$(datadir)\"" \
-DLOCALSTATEDIR="\"$(localstatedir)\"" \ -DLOCALSTATEDIR="\"$(localstatedir)\"" \
-DBINDIR="\"$(bindir)\"" -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. # -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 # 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. # matters a lot there, and is quite hard to debug if you forget to do it.

View File

@ -1,11 +1,15 @@
TESTS+= src/test/test 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)\"" \ src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
-DLOCALSTATEDIR="\"$(localstatedir)\"" \ -DLOCALSTATEDIR="\"$(localstatedir)\"" \
-DBINDIR="\"$(bindir)\"" \ -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. # -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 # 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@ \ src_test_test_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@ @TOR_LDFLAGS_libevent@
src_test_test_LDADD = src/or/libtor.a src/common/libor.a \ src_test_test_LDADD = src/or/libtor-testing.a src/common/libor-testing.a \
src/common/libor-crypto.a $(LIBDONNA) \ src/common/libor-crypto-testing.a $(LIBDONNA) \
src/common/libor-event.a \ src/common/libor-event-testing.a \
@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \ @TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
@TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@ @TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @CURVE25519_LIBS@