]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #714 in SNORT/snort3 from catch_tests to master
authorMichael Altizer (mialtize) <mialtize@cisco.com>
Mon, 21 Nov 2016 21:12:32 +0000 (16:12 -0500)
committerMichael Altizer (mialtize) <mialtize@cisco.com>
Mon, 21 Nov 2016 21:12:32 +0000 (16:12 -0500)
Squashed commit of the following:

commit a7190df88455d404405a9b7a2b92ce0e7e018946
Author: Michael Altizer <mialtize@cisco.com>
Date:   Mon Nov 21 15:01:27 2016 -0500

    catch: Support compiling catch tests in standalone source files

commit 6223f5aea12c005c39d3760d449a47f7e72503d9
Author: Michael Altizer <mialtize@cisco.com>
Date:   Mon Nov 21 14:01:00 2016 -0500

    wizard: Make DCE curses static

24 files changed:
src/catch/dev_notes.txt
src/catch/unit_test.cc
src/catch/unit_test.h
src/catch/unit_test_main.h [new file with mode: 0644]
src/filters/sfrf.cc
src/filters/sfrf_test.cc
src/filters/sfthd.cc
src/filters/sfthd_test.cc
src/main.cc
src/packet_io/CMakeLists.txt
src/packet_io/Makefile.am
src/packet_io/sfdaq_module.cc
src/packet_io/test/sfdaq_module_test.cc
src/service_inspectors/wizard/curses.cc
src/sfip/sf_ip.cc
src/sfip/sfip_test.cc
src/sfrt/sfrt.cc
src/sfrt/sfrt_test.cc
src/time/packet_time.cc
src/time/stopwatch_test.cc
src/utils/CMakeLists.txt
src/utils/Makefile.am
src/utils/bitop_test.cc
src/utils/util.cc

index ed4a8e635a2ef349e8609939a196a1711c8e7acd..dcedf1bb68cf615382f965f6aa8347c36129e596 100644 (file)
@@ -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)
index dbdbf6239dbc8964310cd997c4f4eec7a9a73f59..628a887732b6e45c9001ce35a29c660bfe0f6ac9 100644 (file)
@@ -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()
index db54ad4c8989320aae164003cacebab91bb71b59..a2dad3f49e30e9026519bfc89e1b9747623dc0db 100644 (file)
@@ -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 (file)
index 0000000..b1dce81
--- /dev/null
@@ -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 <mialtize@cisco.com>
+
+#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
+
index a63441fc995a5cdfff5f3da91249da60413599e4..df19ec69f095d87ffacbdf038f5ec34a5a3bbb87 100644 (file)
@@ -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
-
index 06e5771cecf944d7bfa5a70b4bf2c59ab049d17e..2426671e8e9a0b42ad821c7a7098d9f600d58d24 100644 (file)
 #include "config.h"
 #endif
 
-#include <stdio.h>
-#include <stdlib.h>
-
 #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"
index c1e88336d0789bf932784d6e5506fde6ae0a0c84..cb6b4e1ed718501f427b426429fe514edd03350b 100644 (file)
@@ -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
-
index 083501ae9c64f87aeb517caf44b553320f46fd6f..0d154edb6fe22b43e7de96f07439e3b07a84a8e5 100644 (file)
 //--------------------------------------------------------------------------
 // sfthd_test.cc author Russ Combs <rcombs@sourcefire.com>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #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"
index f3e20a6d5c5109902dba4c328b73097c18af571d..a9d5c77577111d1c791d97b72f614b5c98919dd0 100644 (file)
@@ -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
index 761e2ba095e0efc4965ee1054d84f2b17cab9391..385ae640747eb7d5b0e487776e559c44045479f0 100644 (file)
@@ -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}
 )
 
index f85ee17c824cf3209d535f02800ee190601d46da..e1a1541cc78e9428c1c77e0437ac3981cb07ea56 100644 (file)
@@ -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
+
index b1f537fc9553cff647da0a3c54c95738bc75eaeb..7a0dc5279dacefb31939d93643878fa28d989b6d 100644 (file)
 
 #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
index 9c798451b927f63a8a632b9130f8f2207f69d538..dfa69ebcf2e8df0c6b7ff96f3dd96f9446cb4281 100644 (file)
 // 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
-
index 853c1cc71539b3343303959ff09f004155e2ddfe..5fa7dc1549c59f9e36872ade8e3ca40b148ffcaa 100644 (file)
@@ -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 */
index 230e72cbe312692ea189a8141d645aab3c7b5414..1e2f759d82b274e962819c94254ea717c67e6ad8 100644 (file)
@@ -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
-
index 51d613a333e94788254e2c5c774d51f596d62a35..b9dbe758b29948987288feb628073a52b3efec76 100644 (file)
 #include "config.h"
 #endif
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #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[] =
index 343687c54d84043e2da9a0f544eadde51014aa8d..a2b8172f6bda3683a404d626032234bc0a7d765b 100644 (file)
@@ -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
-
index 7ca7d6c826a14bacf59c6fa898f766e0e782a368..151e5369b65398f41b5f0950bfec60e76bbe6ccb 100644 (file)
 //--------------------------------------------------------------------------
 // sfrt_test.cc author Hui Cao <hcao@sourcefire.com>
 
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #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;
index 8f70573e7e978f9f661035ec5998ee72f7cb8ce8..bf89dde2e0a9b9bfe40386243fba4f4fc0033fa8 100644 (file)
 #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
index cad2c27e6bf9b61181bef958ce44bb5b22e055b2..5ee728c7ce71ed7c2e694835c918a7e1d2cb498d 100644 (file)
 
 // stopwatch_test.cc author Joel Cornett <jocornet@cisco.com>
 
+#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
 {
index 15f5ecb8b2fc7026f1f067469fe7d5726b210901..6ea5509f5a638b5a4ce1a69196da4ac94d384b66 100644 (file)
@@ -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
index 49e4b06455fb4d05ebb0c95e26e5707dc182fbd3..01a02281464625f611f75179c0c2934b10e2de6c 100644 (file)
@@ -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
index dd92281cdf9198dace2d5d40bd17c364abb249be..58fc08f052774d266e43286aa39b0f5f14a9fc64 100644 (file)
@@ -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)
 {
index 2e817ab4ba9d0bf02e162093d8b52218676b2988..261e0e06175a28a283f31c60d71df52ee065ed33 100644 (file)
@@ -930,6 +930,3 @@ const char* get_error(int errnum)
 #endif
 }
 
-#ifdef UNIT_TEST
-#include "bitop_test.cc"
-#endif