From: russ Date: Tue, 8 Sep 2015 15:48:59 +0000 (-0400) Subject: separate piglet, check, and catch tests; add catch tests for sfrim X-Git-Tag: 3.0.0-233~847^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3fe0b81e57bdbbbeea5f362de4929f4cd17bc1d0;p=thirdparty%2Fsnort3.git separate piglet, check, and catch tests; add catch tests for sfrim --- diff --git a/src/detection/sfrim.cc b/src/detection/sfrim.cc index 828b21078..a8553ba99 100644 --- a/src/detection/sfrim.cc +++ b/src/detection/sfrim.cc @@ -22,13 +22,19 @@ #include "sfrim.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include #include -#include "utils/util.h" +#ifdef UNIT_TEST +#include "test/catch.hpp" +#endif struct rule_number_t { @@ -80,3 +86,37 @@ bool RuleIndexMapGet(rule_index_map_t* rim, int index, unsigned& gid, unsigned& return false; } +//-------------------------------------------------------------------------- +// unit tests +//-------------------------------------------------------------------------- + +#ifdef UNIT_TEST + +TEST_CASE("basic", "[RuleIndexMap]") +{ + rule_index_map_t* rim = RuleIndexMapCreate(); + unsigned gid, sid; + + CHECK(RuleIndexMapAdd(rim, 1, 2) == 0); + CHECK(RuleIndexMapAdd(rim, 2, 4) == 1); + CHECK(RuleIndexMapAdd(rim, 4, 8) == 2); + + SECTION("valid") + { + CHECK(RuleIndexMapGet(rim, 1, gid, sid)); + + CHECK(gid == 2); + CHECK(sid == 4); + } + SECTION("invalid") + { + CHECK(!RuleIndexMapGet(rim, 3, gid, sid)); + + CHECK(gid == 0); + CHECK(sid == 0); + } + RuleIndexMapFree(rim); +} + +#endif + diff --git a/src/main.cc b/src/main.cc index e20aec3f3..3fc10665f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -692,24 +692,16 @@ static bool just_validate() static bool set_mode() { #ifdef PIGLET -#ifdef UNIT_TEST - if ( unit_test_enabled() ) - { - if ( Piglet::piglet_mode() ) - exit(unit_test() || Piglet::main()); - else - exit(unit_test()); - } - else -#endif // UNIT_TEST if ( Piglet::piglet_mode() ) exit(Piglet::main()); -#else +#endif #ifdef UNIT_TEST - if ( unit_test_enabled() ) - exit(unit_test()); -#endif // UNIT_TEST -#endif // PIGLET + if ( check_enabled() ) + exit(check_test()); + + if ( catch_enabled() ) + exit(catch_test()); +#endif if ( int k = get_parse_errors() ) { diff --git a/src/main/snort_module.cc b/src/main/snort_module.cc index 7a95afbea..648379059 100644 --- a/src/main/snort_module.cc +++ b/src/main/snort_module.cc @@ -441,11 +441,12 @@ static const Parameter s_params[] = "use drop, sdrop, and reject rules to ignore session traffic when not inline" }, #ifdef UNIT_TEST - { "--unit-test", Parameter::PT_SELECT, + { "--check-test", Parameter::PT_SELECT, "silent | minimal | normal | verbose | env (export CK_VERBOSITY)", nullptr, " run unit tests with given libcheck output mode" }, - { "--catch-tags", Parameter::PT_STRING, nullptr, nullptr, - "comma separated list of cat unit test tags" }, + + { "--catch-test", Parameter::PT_STRING, nullptr, nullptr, + "comma separated list of cat unit test tags or 'all'" }, #endif { "--version", Parameter::PT_IMPLIED, nullptr, nullptr, "show version number (same as -V)" }, @@ -834,9 +835,10 @@ bool SnortModule::set(const char*, Value& v, SnortConfig* sc) ConfigTreatDropAsIgnore(sc, v.get_string()); #ifdef UNIT_TEST - else if ( v.is("--unit-test") ) + else if ( v.is("--check-test") ) unit_test_mode(v.get_string()); - else if ( v.is("--catch-tags") ) + + else if ( v.is("--catch-test") ) unit_test_catch_test_filter(v.get_string()); #endif else if ( v.is("--version") ) diff --git a/src/test/unit_test.cc b/src/test/unit_test.cc index 616b45ca6..6f4bfee5f 100644 --- a/src/test/unit_test.cc +++ b/src/test/unit_test.cc @@ -39,6 +39,7 @@ #include "suite_decl.h" static print_output s_mode = CK_LAST; +static bool s_catch = false; static std::vector test_tags; typedef Suite* (* SuiteCtor_f)(); @@ -72,14 +73,22 @@ void unit_test_mode(const char* s) void unit_test_catch_test_filter(const char* s) { - test_tags.push_back( s ); + if ( s && strcmp(s, "all") ) + test_tags.push_back( s ); + + s_catch = true; } -bool unit_test_enabled() +bool check_enabled() { return s_mode != CK_LAST; } +bool catch_enabled() +{ + return s_catch; +} + static bool run_check() { int nErr; @@ -127,25 +136,30 @@ static bool run_check() static bool run_catch() { 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; return session.run() == 0; } -int unit_test() +int check_test() { - int ok = 0; - if ( !run_check() ) - ok = -1; + return -1; + return 0; +} + +int catch_test() +{ if ( !run_catch() ) - ok = -1; + return -1; - return ok; + return 0; } diff --git a/src/test/unit_test.h b/src/test/unit_test.h index 9598f2af0..74c7fd357 100644 --- a/src/test/unit_test.h +++ b/src/test/unit_test.h @@ -34,9 +34,12 @@ #define UNIT_TEST_MODE_ENVVAR "CK_VERBOSITY" void unit_test_mode(const char* = nullptr); -bool unit_test_enabled(); -int unit_test(); void unit_test_catch_test_filter(const char* s); +bool check_enabled(); +bool catch_enabled(); + +int check_test(); +int catch_test(); #endif