From: Michael Altizer (mialtize) Date: Mon, 21 Nov 2016 21:12:32 +0000 (-0500) Subject: Merge pull request #714 in SNORT/snort3 from catch_tests to master X-Git-Tag: 3.0.0-233~184 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d4f4137d9970815e5510f8d67b8872e06827bcd9;p=thirdparty%2Fsnort3.git Merge pull request #714 in SNORT/snort3 from catch_tests to master Squashed commit of the following: commit a7190df88455d404405a9b7a2b92ce0e7e018946 Author: Michael Altizer Date: Mon Nov 21 15:01:27 2016 -0500 catch: Support compiling catch tests in standalone source files commit 6223f5aea12c005c39d3760d449a47f7e72503d9 Author: Michael Altizer Date: Mon Nov 21 14:01:00 2016 -0500 wizard: Make DCE curses static --- diff --git a/src/catch/dev_notes.txt b/src/catch/dev_notes.txt index ed4a8e635..dcedf1bb6 100644 --- a/src/catch/dev_notes.txt +++ b/src/catch/dev_notes.txt @@ -4,3 +4,12 @@ This directory contains the unit-test interface. catch.hpp is from https://github.com/philsquared/Catch. +If a source file contains only Catch unit tests, the linker must be tricked +into not excluding the seemingly unneeded object file. To do this, include +unit_test.h in the source file and add this directive to the beginning of the +source code: + SNORT_CATCH_FORCED_INCLUSION_DEFINITION(source_file_name_less_extension) +Additionally, edit unit_test_main.h to add the external definition with: + SNORT_CATCH_FORCED_INCLUSION_EXTERN(source_file_name_less_extension) +As well as the reference to it in catch_extern_tests[] with: + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(source_file_name_less_extension) diff --git a/src/catch/unit_test.cc b/src/catch/unit_test.cc index dbdbf6239..628a88773 100644 --- a/src/catch/unit_test.cc +++ b/src/catch/unit_test.cc @@ -46,16 +46,12 @@ bool catch_enabled() static bool run_catch() { - Catch::Session session; + Catch::Session session; - // write to session.configData() or session.Config() to customize - //if( s_mode == CK_VERBOSE ) - // session.configData().showSuccessfulTests = true; + if ( test_tags.size() > 0 ) + session.configData().testsOrTags = test_tags; - if( test_tags.size() > 0 ) - session.configData().testsOrTags = test_tags; - - return session.run() == 0; + return session.run() == 0; } int catch_test() diff --git a/src/catch/unit_test.h b/src/catch/unit_test.h index db54ad4c8..a2dad3f49 100644 --- a/src/catch/unit_test.h +++ b/src/catch/unit_test.h @@ -27,5 +27,16 @@ void catch_set_filter(const char* s); bool catch_enabled(); int catch_test(); + +// Macros for supporting standalone Catch test source files +#define SNORT_CATCH_FORCED_INCLUSION_SYMBOL( name ) \ + __catch_include_##name + +#define SNORT_CATCH_FORCED_INCLUSION_DEFINITION( name ) \ + bool SNORT_CATCH_FORCED_INCLUSION_SYMBOL( name ) + +#define SNORT_CATCH_FORCED_INCLUSION_EXTERN( name ) \ + extern SNORT_CATCH_FORCED_INCLUSION_DEFINITION( name ) + #endif diff --git a/src/catch/unit_test_main.h b/src/catch/unit_test_main.h new file mode 100644 index 000000000..b1dce8122 --- /dev/null +++ b/src/catch/unit_test_main.h @@ -0,0 +1,49 @@ +//-------------------------------------------------------------------------- +// Copyright (C) 2014-2016 Cisco and/or its affiliates. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License Version 2 as published +// by the Free Software Foundation. You may not use, modify or distribute +// this program under any other version of the GNU General Public License. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +//-------------------------------------------------------------------------- +// unit_test_main.h author Michael Altizer + +#ifndef UNIT_TEST_MAIN_H +#define UNIT_TEST_MAIN_H + +// Unit test framework to be included *only* by the main source file to handle +// forcing the linking of objects file containing only Catch test cases. + +#include "catch/unit_test.h" + +// Unresolved external symbol declarations and references. +SNORT_CATCH_FORCED_INCLUSION_EXTERN(bitop_test); +SNORT_CATCH_FORCED_INCLUSION_EXTERN(sfdaq_module_test); +SNORT_CATCH_FORCED_INCLUSION_EXTERN(sfip_test); +SNORT_CATCH_FORCED_INCLUSION_EXTERN(sfrf_test); +SNORT_CATCH_FORCED_INCLUSION_EXTERN(sfrt_test); +SNORT_CATCH_FORCED_INCLUSION_EXTERN(sfthd_test); +SNORT_CATCH_FORCED_INCLUSION_EXTERN(stopwatch_test); + +bool catch_extern_tests[] = +{ + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(bitop_test), + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(sfdaq_module_test), + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(sfip_test), + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(sfrf_test), + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(sfrt_test), + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(sfthd_test), + SNORT_CATCH_FORCED_INCLUSION_SYMBOL(stopwatch_test), +}; + +#endif + diff --git a/src/filters/sfrf.cc b/src/filters/sfrf.cc index a63441fc9..df19ec69f 100644 --- a/src/filters/sfrf.cc +++ b/src/filters/sfrf.cc @@ -823,8 +823,3 @@ static tSFRFTrackingNode* _getSFRFTrackingNode( return dynNode; } -#ifdef UNIT_TEST -// FIXIT-L Catch issue; see sfip/sf_ip.cc -#include "sfrf_test.cc" -#endif - diff --git a/src/filters/sfrf_test.cc b/src/filters/sfrf_test.cc index 06e5771ce..2426671e8 100644 --- a/src/filters/sfrf_test.cc +++ b/src/filters/sfrf_test.cc @@ -22,24 +22,18 @@ #include "config.h" #endif -#include -#include - #include "catch/catch.hpp" - -#include "detection/rules.h" -#include "detection/treenodes.h" -#include "filters/rate_filter.h" -#include "filters/sfrf.h" -#include "hash/sfghash.h" -#include "main/snort_types.h" -#include "main/policy.h" +#include "catch/unit_test.h" #include "parser/parse_ip.h" #include "sfip/sf_ip.h" -#include "utils/util.h" + +#include "rate_filter.h" +#include "sfrf.h" //--------------------------------------------------------------- +SNORT_CATCH_FORCED_INCLUSION_DEFINITION(sfrf_test); + #define IP_ANY NULL // used to get "unset" #define IP4_SRC "1.2.3.4" diff --git a/src/filters/sfthd.cc b/src/filters/sfthd.cc index c1e88336d..cb6b4e1ed 100644 --- a/src/filters/sfthd.cc +++ b/src/filters/sfthd.cc @@ -1307,8 +1307,3 @@ int sfthd_show_objects(ThresholdObjects* thd_objs) #endif // THD_DEBUG -#ifdef UNIT_TEST -// FIXIT-L Catch issue; see sfip/sf_ip.cc -#include "sfthd_test.cc" -#endif - diff --git a/src/filters/sfthd_test.cc b/src/filters/sfthd_test.cc index 083501ae9..0d154edb6 100644 --- a/src/filters/sfthd_test.cc +++ b/src/filters/sfthd_test.cc @@ -18,21 +18,17 @@ //-------------------------------------------------------------------------- // sfthd_test.cc author Russ Combs -#include -#include -#include - #include "catch/catch.hpp" - -#include "filters/sfthd.h" -#include "hash/sfxhash.h" -#include "main/policy.h" +#include "catch/unit_test.h" #include "parser/parse_ip.h" #include "sfip/sf_ip.h" -#include "utils/util.h" + +#include "sfthd.h" //--------------------------------------------------------------- +SNORT_CATCH_FORCED_INCLUSION_DEFINITION(sfthd_test); + #define IP_ANY NULL // used to get "unset" #define IP4_SRC "1.2.3.4" diff --git a/src/main.cc b/src/main.cc index f3e20a6d5..a9d5c7757 100644 --- a/src/main.cc +++ b/src/main.cc @@ -68,7 +68,7 @@ #include "utils/safec.h" #ifdef UNIT_TEST -#include "catch/unit_test.h" +#include "catch/unit_test_main.h" #endif #ifdef PIGLET diff --git a/src/packet_io/CMakeLists.txt b/src/packet_io/CMakeLists.txt index 761e2ba09..385ae6407 100644 --- a/src/packet_io/CMakeLists.txt +++ b/src/packet_io/CMakeLists.txt @@ -1,4 +1,10 @@ +if (ENABLE_UNIT_TESTS) + set(TEST_FILES + test/sfdaq_module_test.cc + ) +endif (ENABLE_UNIT_TESTS) + add_library (packet_io STATIC active.cc active.h @@ -12,5 +18,6 @@ add_library (packet_io STATIC sfdaq_module.h trough.cc trough.h + ${TEST_FILES} ) diff --git a/src/packet_io/Makefile.am b/src/packet_io/Makefile.am index f85ee17c8..e1a1541cc 100644 --- a/src/packet_io/Makefile.am +++ b/src/packet_io/Makefile.am @@ -1,3 +1,4 @@ +AUTOMAKE_OPTIONS = subdir-objects noinst_LIBRARIES = libpacket_io.a @@ -15,3 +16,7 @@ sfdaq_module.h \ trough.cc \ trough.h +if ENABLE_UNIT_TESTS +libpacket_io_a_SOURCES += test/sfdaq_module_test.cc +endif + diff --git a/src/packet_io/sfdaq_module.cc b/src/packet_io/sfdaq_module.cc index b1f537fc9..7a0dc5279 100644 --- a/src/packet_io/sfdaq_module.cc +++ b/src/packet_io/sfdaq_module.cc @@ -31,10 +31,6 @@ #include "sfdaq_config.h" -#ifdef UNIT_TEST -#include "catch/catch.hpp" -#endif - #define sfdaq_help "configure packet acquisition interface" static const Parameter string_list_param[] = @@ -178,6 +174,3 @@ PegCount* SFDAQModule::get_counts() const return (PegCount*) &ds; } -#ifdef UNIT_TEST -#include "test/sfdaq_module_test.cc" -#endif diff --git a/src/packet_io/test/sfdaq_module_test.cc b/src/packet_io/test/sfdaq_module_test.cc index 9c798451b..dfa69ebcf 100644 --- a/src/packet_io/test/sfdaq_module_test.cc +++ b/src/packet_io/test/sfdaq_module_test.cc @@ -22,7 +22,13 @@ // unit tests // ----------------------------------------------------------------------------- -#ifdef UNIT_TEST +#include "catch/catch.hpp" +#include "catch/unit_test.h" +#include "main/snort_config.h" +#include "packet_io/sfdaq_config.h" +#include "packet_io/sfdaq_module.h" + +SNORT_CATCH_FORCED_INCLUSION_DEFINITION(sfdaq_module_test); TEST_CASE("Kitchen Sink", "[SFDAQModule]") { @@ -156,5 +162,3 @@ TEST_CASE("Kitchen Sink", "[SFDAQModule]") } } -#endif - diff --git a/src/service_inspectors/wizard/curses.cc b/src/service_inspectors/wizard/curses.cc index 853c1cc71..5fa7dc154 100644 --- a/src/service_inspectors/wizard/curses.cc +++ b/src/service_inspectors/wizard/curses.cc @@ -65,7 +65,7 @@ enum DceRpcProtoMinorVers DCERPC_PROTO_MINOR_VERS__1 = 1 }; -bool dce_udp_curse(const uint8_t* data, unsigned len, CurseTracker*) +static bool dce_udp_curse(const uint8_t* data, unsigned len, CurseTracker*) { const uint8_t dcerpc_cl_hdr_len = 80; const uint8_t cl_len_offset = 74; @@ -100,7 +100,7 @@ bool dce_udp_curse(const uint8_t* data, unsigned len, CurseTracker*) return false; } -bool dce_tcp_curse(const uint8_t* data, unsigned len, CurseTracker* tracker) +static bool dce_tcp_curse(const uint8_t* data, unsigned len, CurseTracker* tracker) { const uint8_t dce_rpc_co_hdr_len = 16; @@ -189,7 +189,7 @@ bool dce_tcp_curse(const uint8_t* data, unsigned len, CurseTracker* tracker) return false; } -bool dce_smb_curse(const uint8_t* data, unsigned len, CurseTracker* tracker) +static bool dce_smb_curse(const uint8_t* data, unsigned len, CurseTracker* tracker) { const uint32_t dce_smb_id = 0xff534d42; /* \xffSMB */ const uint32_t dce_smb2_id = 0xfe534d42; /* \xfeSMB */ diff --git a/src/sfip/sf_ip.cc b/src/sfip/sf_ip.cc index 230e72cbe..1e2f759d8 100644 --- a/src/sfip/sf_ip.cc +++ b/src/sfip/sf_ip.cc @@ -658,10 +658,3 @@ int sfip_ismapped(const sfip_t* ip) return 1; } -#ifdef UNIT_TEST -// FIXIT-L this is a hack to get catch to run these tests -// otherwise the linker omits the auto registration foo -// and the tests will not run -#include "sfip_test.cc" -#endif - diff --git a/src/sfip/sfip_test.cc b/src/sfip/sfip_test.cc index 51d613a33..b9dbe758b 100644 --- a/src/sfip/sfip_test.cc +++ b/src/sfip/sfip_test.cc @@ -22,17 +22,18 @@ #include "config.h" #endif -#include -#include #include #include "catch/catch.hpp" - +#include "catch/unit_test.h" #include "main/snort_types.h" + #include "sf_ip.h" //--------------------------------------------------------------- +SNORT_CATCH_FORCED_INCLUSION_DEFINITION(sfip_test); + static int s_debug = 0; static const char* const codes[] = diff --git a/src/sfrt/sfrt.cc b/src/sfrt/sfrt.cc index 343687c54..a2b8172f6 100644 --- a/src/sfrt/sfrt.cc +++ b/src/sfrt/sfrt.cc @@ -806,8 +806,3 @@ int main() #endif /* DEBUG_SFRT */ -#ifdef UNIT_TEST -// FIXIT-L Catch issue; see sfip/sf_ip.cc -#include "sfrt_test.cc" -#endif - diff --git a/src/sfrt/sfrt_test.cc b/src/sfrt/sfrt_test.cc index 7ca7d6c82..151e5369b 100644 --- a/src/sfrt/sfrt_test.cc +++ b/src/sfrt/sfrt_test.cc @@ -18,21 +18,18 @@ //-------------------------------------------------------------------------- // sfrt_test.cc author Hui Cao -#include -#include -#include -#include - #include "catch/catch.hpp" -#include "main/snort_types.h" +#include "catch/unit_test.h" +#include "sfip/sf_ip.h" #include "utils/util.h" -#include "sfrt/sfrt.h" -#include "sfip/sf_ip.h" +#include "sfrt.h" #define NUM_IPS 32 #define NUM_DATA 4 +SNORT_CATCH_FORCED_INCLUSION_DEFINITION(sfrt_test); + typedef struct { const char* ip_str; diff --git a/src/time/packet_time.cc b/src/time/packet_time.cc index 8f70573e7..bf89dde2e 100644 --- a/src/time/packet_time.cc +++ b/src/time/packet_time.cc @@ -32,11 +32,6 @@ #include "time/packet_time.h" #include "main/thread.h" -#ifdef UNIT_TEST -#include "stopwatch.h" -#include "catch/catch.hpp" -#endif - static THREAD_LOCAL struct timeval s_recent_packet = { 0, 0 }; static THREAD_LOCAL uint32_t s_first_packet = 0; @@ -63,6 +58,3 @@ void packet_gettimeofday(struct timeval* tv) *tv = s_recent_packet; } -#ifdef UNIT_TEST -#include "stopwatch_test.cc" -#endif diff --git a/src/time/stopwatch_test.cc b/src/time/stopwatch_test.cc index cad2c27e6..5ee728c7c 100644 --- a/src/time/stopwatch_test.cc +++ b/src/time/stopwatch_test.cc @@ -18,9 +18,13 @@ // stopwatch_test.cc author Joel Cornett +#include "catch/catch.hpp" +#include "catch/unit_test.h" + #include "clock_defs.h" #include "stopwatch.h" -#include "catch/catch.hpp" + +SNORT_CATCH_FORCED_INCLUSION_DEFINITION(stopwatch_test); namespace t_stopwatch { diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 15f5ecb8b..6ea5509f5 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,7 +1,3 @@ -find_package(Threads REQUIRED) - -# FIXIT-M remove. -add_definitions(" -std=c++11 -DHAVE_PTHREADS ") if ( BUILD_SNPRINTF ) SET (SNPRINTF_SOURCES @@ -28,6 +24,10 @@ set( UTIL_INCLUDES util_utf.h ) +if ( ENABLE_UNIT_TESTS ) + set(TEST_FILES bitop_test.cc) +endif() + ADD_LIBRARY( utils STATIC ${UTIL_INCLUDES} ${SNPRINTF_SOURCES} @@ -47,6 +47,7 @@ ADD_LIBRARY( utils STATIC util_net.h util_unfold.cc util_utf.cc + ${TEST_FILES} ) target_link_libraries(utils diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index 49e4b0645..01a022814 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -35,3 +35,6 @@ util_net.cc util_net.h \ util_unfold.cc \ util_utf.cc +if ENABLE_UNIT_TESTS +libutils_a_SOURCES += bitop_test.cc +endif diff --git a/src/utils/bitop_test.cc b/src/utils/bitop_test.cc index dd92281cd..58fc08f05 100644 --- a/src/utils/bitop_test.cc +++ b/src/utils/bitop_test.cc @@ -1,5 +1,9 @@ -#include "bitop.h" #include "catch/catch.hpp" +#include "catch/unit_test.h" + +#include "bitop.h" + +SNORT_CATCH_FORCED_INCLUSION_DEFINITION(bitop_test); static bool t_bitop_buffer_zero(BitOp& bitop) { diff --git a/src/utils/util.cc b/src/utils/util.cc index 2e817ab4b..261e0e061 100644 --- a/src/utils/util.cc +++ b/src/utils/util.cc @@ -930,6 +930,3 @@ const char* get_error(int errnum) #endif } -#ifdef UNIT_TEST -#include "bitop_test.cc" -#endif