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)
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()
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
--- /dev/null
+//--------------------------------------------------------------------------
+// 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
+
return dynNode;
}
-#ifdef UNIT_TEST
-// FIXIT-L Catch issue; see sfip/sf_ip.cc
-#include "sfrf_test.cc"
-#endif
-
#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"
#endif // THD_DEBUG
-#ifdef UNIT_TEST
-// FIXIT-L Catch issue; see sfip/sf_ip.cc
-#include "sfthd_test.cc"
-#endif
-
//--------------------------------------------------------------------------
// 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"
#include "utils/safec.h"
#ifdef UNIT_TEST
-#include "catch/unit_test.h"
+#include "catch/unit_test_main.h"
#endif
#ifdef PIGLET
+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
sfdaq_module.h
trough.cc
trough.h
+ ${TEST_FILES}
)
+AUTOMAKE_OPTIONS = subdir-objects
noinst_LIBRARIES = libpacket_io.a
trough.cc \
trough.h
+if ENABLE_UNIT_TESTS
+libpacket_io_a_SOURCES += test/sfdaq_module_test.cc
+endif
+
#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[] =
return (PegCount*) &ds;
}
-#ifdef UNIT_TEST
-#include "test/sfdaq_module_test.cc"
-#endif
// 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]")
{
}
}
-#endif
-
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;
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;
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 */
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
-
#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[] =
#endif /* DEBUG_SFRT */
-#ifdef UNIT_TEST
-// FIXIT-L Catch issue; see sfip/sf_ip.cc
-#include "sfrt_test.cc"
-#endif
-
//--------------------------------------------------------------------------
// 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;
#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;
*tv = s_recent_packet;
}
-#ifdef UNIT_TEST
-#include "stopwatch_test.cc"
-#endif
// 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
{
-find_package(Threads REQUIRED)
-
-# FIXIT-M remove.
-add_definitions(" -std=c++11 -DHAVE_PTHREADS ")
if ( BUILD_SNPRINTF )
SET (SNPRINTF_SOURCES
util_utf.h
)
+if ( ENABLE_UNIT_TESTS )
+ set(TEST_FILES bitop_test.cc)
+endif()
+
ADD_LIBRARY( utils STATIC
${UTIL_INCLUDES}
${SNPRINTF_SOURCES}
util_net.h
util_unfold.cc
util_utf.cc
+ ${TEST_FILES}
)
target_link_libraries(utils
util_unfold.cc \
util_utf.cc
+if ENABLE_UNIT_TESTS
+libutils_a_SOURCES += bitop_test.cc
+endif
-#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)
{
#endif
}
-#ifdef UNIT_TEST
-#include "bitop_test.cc"
-#endif