]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #2520 in SNORT/snort3 from ~OSHUMEIK/snort3:trace_ut to master
authorBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 6 Oct 2020 18:09:20 +0000 (18:09 +0000)
committerBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 6 Oct 2020 18:09:20 +0000 (18:09 +0000)
Squashed commit of the following:

commit 9cdf32b0307311ce02f67caf0524b047f2a96db2
Author: Oleksii Shumeiko <oshumeik@cisco.com>
Date:   Tue Sep 29 11:32:03 2020 +0300

    trace: update parser unit tests

commit 6de816f12b0e1ca6ad1b511b3c02de0039f22fe1
Author: Oleksii Shumeiko <oshumeik@cisco.com>
Date:   Wed Sep 30 11:40:18 2020 +0300

    framework: update packet constraints comparison to check only set fields

src/framework/packet_constraints.cc
src/trace/trace_config.cc
src/trace/trace_module.cc
src/trace/trace_parser.cc
src/trace/trace_parser.h
src/trace/trace_swap.cc

index 418eb49eb6083abcb44175217e6426fd30ff2013..884cbf49a128b84532ad84e5c859ab8c6e9431ea 100644 (file)
@@ -48,11 +48,11 @@ using namespace snort;
 bool PacketConstraints::operator==(const PacketConstraints& other) const
 {
     return set_bits == other.set_bits
-        and ip_proto == other.ip_proto
-        and src_port == other.src_port
-        and dst_port == other.dst_port
-        and src_ip == other.src_ip
-        and dst_ip == other.dst_ip;
+        and ( !(set_bits & IP_PROTO) or ip_proto == other.ip_proto )
+        and ( !(set_bits & SRC_PORT) or src_port == other.src_port )
+        and ( !(set_bits & DST_PORT) or dst_port == other.dst_port )
+        and ( !(set_bits & SRC_IP) or src_ip == other.src_ip )
+        and ( !(set_bits & DST_IP) or dst_ip == other.dst_ip );
 }
 
 bool PacketConstraints::packet_match(const Packet& p) const
@@ -92,6 +92,54 @@ bool PacketConstraints::flow_match(const Flow& f) const
 
 #include <catch/snort_catch.h>
 
+TEST_CASE("Packet constraints comparison", "[framework]")
+{
+    SECTION("all unset")
+    {
+        const PacketConstraints exp = { IpProtocol::TCP, 10, 20,
+            SfIp(), SfIp(), 0 };
+        const PacketConstraints act = { IpProtocol::UDP, 30, 40,
+            SfIp(), SfIp(), 0 };
+        CHECK( exp == act );
+    }
+
+    SECTION("dst unset")
+    {
+        const uint32_t ip1 = 0x01010101;
+        const uint32_t ip2 = 0x02020202;
+        const PacketConstraints exp = { IpProtocol::PROTO_NOT_SET, 10, 20,
+            SfIp(&ip1, AF_INET), SfIp(&ip2, AF_INET),
+            (uint8_t)~PacketConstraints::SetBits::DST_IP };
+        const PacketConstraints act = { IpProtocol::PROTO_NOT_SET, 10, 20,
+            SfIp(&ip1, AF_INET), SfIp(),
+            (uint8_t)~PacketConstraints::SetBits::DST_IP };
+        CHECK( exp == act );
+    }
+
+    SECTION("not equal")
+    {
+        const uint32_t ip1 = 0x01010101;
+        const uint32_t ip2 = 0x02020202;
+        const uint32_t ip3 = 0x03030303;
+        const PacketConstraints exp = { IpProtocol::PROTO_NOT_SET, 0, 0,
+            SfIp(&ip1, AF_INET), SfIp(&ip2, AF_INET), (uint8_t)-1 };
+        const PacketConstraints act = { IpProtocol::PROTO_NOT_SET, 0, 0,
+            SfIp(&ip3, AF_INET), SfIp(&ip2, AF_INET), (uint8_t)-1};
+        CHECK( !(exp == act) );
+    }
+
+    SECTION("equal")
+    {
+        const uint32_t ip1 = 0x01010101;
+        const uint32_t ip2 = 0x02020202;
+        const PacketConstraints exp = { IpProtocol::PROTO_NOT_SET, 0, 0,
+            SfIp(&ip1, AF_INET), SfIp(&ip2, AF_INET), (uint8_t)-1 };
+        const PacketConstraints act = { IpProtocol::PROTO_NOT_SET, 0, 0,
+            SfIp(&ip1, AF_INET), SfIp(&ip2, AF_INET), (uint8_t)-1 };
+        CHECK( exp == act );
+    }
+}
+
 TEST_CASE("Packet constraints matching", "[framework]")
 {
     PacketConstraints cs;
index 5c9320ce603a30615cc711bd9c4a62bdba2d13ea..a427f68a28381e5683e32ded6573d473087a47b4 100644 (file)
@@ -66,7 +66,7 @@ bool TraceConfig::set_trace(const std::string& module_name, const std::string& t
 {
     for ( auto& trace : traces )
     {
-        if ( strcmp(trace.module_name(), module_name.c_str()) == 0 )
+        if ( module_name == trace.module_name() )
             return trace.set(trace_option_name, trace_level);
     }
     return false;
index 0dd800626c19b4087bc5e9956bc307b6172bc089..b81277a1bb5d4a5c32ceb8e308e00b9ba1125a1d 100644 (file)
@@ -160,7 +160,7 @@ bool TraceModule::begin(const char* fqn, int, SnortConfig* sc)
 {
     if ( !strcmp(fqn, "trace") )
     {
-        trace_parser = new TraceParser(sc->trace_config);
+        trace_parser = new TraceParser(*sc->trace_config);
 
         // Init default output type based on Snort run-mode
         if ( sc->daemon_mode() or sc->log_syslog() )
@@ -191,7 +191,7 @@ bool TraceModule::set(const char* fqn, Value& v, SnortConfig*)
     }
     else if ( v.is("log_ntuple") )
     {
-        trace_parser->get_trace_config()->log_ntuple = v.get_bool();
+        trace_parser->get_trace_config().log_ntuple = v.get_bool();
         return true;
     }
     else if ( strstr(fqn, "trace.modules.") == fqn )
@@ -218,10 +218,10 @@ bool TraceModule::end(const char* fqn, int, SnortConfig* sc)
             switch ( log_output_type )
             {
             case OUTPUT_TYPE_STDOUT:
-                trace_parser->get_trace_config()->logger_factory = new StdoutLoggerFactory();
+                trace_parser->get_trace_config().logger_factory = new StdoutLoggerFactory();
                 break;
             case OUTPUT_TYPE_SYSLOG:
-                trace_parser->get_trace_config()->logger_factory = new SyslogLoggerFactory();
+                trace_parser->get_trace_config().logger_factory = new SyslogLoggerFactory();
                 break;
             default:
                 break;
index 9da7477e958ac590bc69a35ccb3db0e3ccaa11a4..54049de93024f47c663dd9f3516da8bfc46fc8b7 100644 (file)
@@ -32,11 +32,9 @@ using namespace snort;
 
 std::map<std::string, std::map<std::string, bool>> TraceParser::s_configured_trace_options;
 
-TraceParser::TraceParser(TraceConfig* tc)
+TraceParser::TraceParser(TraceConfig& tc)
     : trace_config(tc)
 {
-    assert(trace_config);
-
     // Will be initialized only once when first TraceParser instance created
     if ( s_configured_trace_options.empty() )
         init_configured_trace_options();
@@ -44,13 +42,13 @@ TraceParser::TraceParser(TraceConfig* tc)
         reset_configured_trace_options();
 }
 
-bool TraceParser::set_traces(const std::string& option_name, const Value& val)
+bool TraceParser::set_traces(const std::string& module_name, const Value& val)
 {
-    if ( !s_configured_trace_options.count(option_name)
-        and option_name != DEFAULT_TRACE_OPTION_NAME )
+    if ( !s_configured_trace_options.count(module_name)
+        and module_name != DEFAULT_TRACE_OPTION_NAME )
         return false;
 
-    if ( option_name == DEFAULT_TRACE_OPTION_NAME )
+    if ( module_name == DEFAULT_TRACE_OPTION_NAME )
     {
         for ( const auto& trace_options : s_configured_trace_options )
         {
@@ -60,7 +58,7 @@ bool TraceParser::set_traces(const std::string& option_name, const Value& val)
             for ( const auto& trace_option : trace_options.second )
             {
                 if ( !trace_option.second )
-                    trace_config->set_trace(trace_options.first, trace_option.first,
+                    trace_config.set_trace(trace_options.first, trace_option.first,
                         val.get_uint8());
             }
         }
@@ -69,11 +67,11 @@ bool TraceParser::set_traces(const std::string& option_name, const Value& val)
     }
     else if ( val.is(DEFAULT_TRACE_OPTION_NAME) )
     {
-        auto& trace_options = s_configured_trace_options[option_name];
+        auto& trace_options = s_configured_trace_options[module_name];
         for ( const auto& trace_option : trace_options )
         {
             if ( !trace_option.second )
-                trace_config->set_trace(option_name, trace_option.first, val.get_uint8());
+                trace_config.set_trace(module_name, trace_option.first, val.get_uint8());
         }
         trace_options[DEFAULT_TRACE_OPTION_NAME] = true;
 
@@ -81,8 +79,8 @@ bool TraceParser::set_traces(const std::string& option_name, const Value& val)
     }
     else
     {
-        bool res = trace_config->set_trace(option_name, val.get_name(), val.get_uint8());
-        s_configured_trace_options[option_name][val.get_name()] = res;
+        bool res = trace_config.set_trace(module_name, val.get_name(), val.get_uint8());
+        s_configured_trace_options[module_name][val.get_name()] = res;
         return res;
     }
 }
@@ -131,16 +129,16 @@ bool TraceParser::set_constraints(const Value& val)
 void TraceParser::finalize_constraints()
 {
     if ( !parsed_constraints.match or parsed_constraints.set_bits )
-        trace_config->constraints = new PacketConstraints(parsed_constraints);
+        trace_config.constraints = new PacketConstraints(parsed_constraints);
 }
 
 void TraceParser::clear_traces()
-{ trace_config->clear_traces(); }
+{ trace_config.clear_traces(); }
 
 void TraceParser::clear_constraints()
 {
-    delete trace_config->constraints;
-    trace_config->constraints = nullptr;
+    delete trace_config.constraints;
+    trace_config.constraints = nullptr;
 }
 
 void TraceParser::reset_configured_trace_options()
@@ -158,16 +156,16 @@ void TraceParser::init_configured_trace_options()
     for ( const auto* module : trace_modules )
     {
         const TraceOption* trace_options = module->get_trace_options();
-        if ( trace_options )
-        {
-            auto& module_trace_options = s_configured_trace_options[module->get_name()];
+        if ( !trace_options )
+            continue;
 
-            module_trace_options[DEFAULT_TRACE_OPTION_NAME] = false;
-            while ( trace_options->name )
-            {
-                module_trace_options[trace_options->name] = false;
-                ++trace_options;
-            }
+        auto& module_trace_options = s_configured_trace_options[module->get_name()];
+
+        module_trace_options[DEFAULT_TRACE_OPTION_NAME] = false;
+        while ( trace_options->name )
+        {
+            module_trace_options[trace_options->name] = false;
+            ++trace_options;
         }
     }
 }
@@ -176,144 +174,281 @@ void TraceParser::init_configured_trace_options()
 
 #include <catch/snort_catch.h>
 
-TEST_CASE("packet constraints", "[TraceParser]")
+#include "main/snort_config.h"
+
+#define CONFIG_OPTION(name, value, type, range)                         \
+    const Parameter name##_param(                                       \
+        #name, type, range, nullptr, #name " test");                    \
+    Value name(false);                                                  \
+    name.set(&name##_param);                                            \
+    name.set(value);
+
+#define MODULE_OPTION(name, value) \
+    CONFIG_OPTION(name, value, Parameter::PT_INT, "0:255")
+
+#define PROTO_OPTION(name, value) \
+    CONFIG_OPTION(name, value, Parameter::PT_INT, "0:255")
+
+#define ADDR_OPTION(name, value) \
+    CONFIG_OPTION(name, value, Parameter::PT_STRING, nullptr)
+
+#define PORT_OPTION(name, value) \
+    CONFIG_OPTION(name, value, Parameter::PT_INT, "0:65535")
+
+enum { OPT_1, OPT_2 };
+
+static const TraceOption trace_options[] =
 {
-    TraceConfig tc;
-    TraceParser tp(&tc);
+    { "option1", OPT_1, "test option 1" },
+    { "option2", OPT_2, "test option 2" },
+    { nullptr, 0, nullptr }
+};
 
-    SECTION("ip_proto")
-    {
-        const Parameter ip_proto_param("ip_proto", Parameter::PT_INT, "0:255", nullptr,
-            "test ip_proto param");
+static Trace *m1_trace, *m2_trace;
 
-        Value val(false);
-        val.set(&ip_proto_param);
+class Module1 : public Module
+{
+public:
+    Module1() : Module("mod_1", "testing trace parser module 1") { }
+    void set_trace(const Trace* t) const override { m1_trace = (Trace*)t; }
+    const TraceOption* get_trace_options() const override { return trace_options; }
 
-        val.set(6l);
-        CHECK( tp.set_constraints(val) );
-    }
-    SECTION("src_ip")
-    {
-        const Parameter src_ip_param("src_ip", Parameter::PT_STRING, nullptr, nullptr,
-          "test src_ip param");
+};
+
+class Module2 : public Module
+{
+public:
+    Module2() : Module("mod_2", "testing trace parser module 2") { }
+    void set_trace(const Trace* t) const override { m2_trace = (Trace*)t; }
+    const TraceOption* get_trace_options() const override { return trace_options; }
+
+};
 
-        Value val(false);
-        val.set(&src_ip_param);
+TEST_CASE("modules traces", "[TraceParser]")
+{
+    static bool once = []()
+    {
+        ModuleManager::add_module(new Module1);
+        ModuleManager::add_module(new Module2);
+        return true;
+    } ();
+    (void)once;
 
-        val.set("10.1.2.3");
-        CHECK( tp.set_constraints(val) );
+    TraceConfig tc;
+    TraceParser tp(tc);
+    tc.setup_module_trace();
 
-        val.set("10.1.2.300");
-        CHECK( !tp.set_constraints(val) );
+    SECTION("invalid module")
+    {
+        MODULE_OPTION(all, 10l);
+        CHECK(!tp.set_traces("invalid_module", all));
     }
-    SECTION("src_port")
+
+    SECTION("invalid option")
     {
-        const Parameter src_port_param("src_port", Parameter::PT_INT, "0:65535", nullptr,
-          "test src_port param");
+        MODULE_OPTION(invalid_option, 10l);
+        CHECK(!tp.set_traces("mod_1", invalid_option));
+    }
 
-        Value val(false);
-        val.set(&src_port_param);
+    SECTION("unset")
+    {
+        REQUIRE(m1_trace != nullptr);
+        REQUIRE(m2_trace != nullptr);
 
-        val.set(100l);
-        CHECK( tp.set_constraints(val) );
+        CHECK(!m1_trace->enabled(OPT_1));
+        CHECK(!m1_trace->enabled(OPT_2));
+        CHECK(!m2_trace->enabled(OPT_1));
+        CHECK(!m2_trace->enabled(OPT_2));
     }
-    SECTION("dst_ip")
+
+    SECTION("all modules")
     {
-        const Parameter dst_ip_param("dst_ip", Parameter::PT_STRING, nullptr, nullptr,
-          "test dst_ip param");
+        MODULE_OPTION(all, 3l);
+        CHECK(tp.set_traces("all", all));
 
-        Value val(false);
-        val.set(&dst_ip_param);
+        REQUIRE(m1_trace != nullptr);
+        REQUIRE(m2_trace != nullptr);
 
-        val.set("10.3.2.1");
-        CHECK( tp.set_constraints(val) );
+        CHECK(m1_trace->enabled(OPT_1, 3));
+        CHECK(m1_trace->enabled(OPT_2, 3));
+        CHECK(m2_trace->enabled(OPT_1, 3));
+        CHECK(m2_trace->enabled(OPT_2, 3));
 
-        val.set("10.300.2.1");
-        CHECK( !tp.set_constraints(val) );
+        CHECK(!m1_trace->enabled(OPT_1, 4));
+        CHECK(!m1_trace->enabled(OPT_2, 4));
+        CHECK(!m2_trace->enabled(OPT_1, 4));
+        CHECK(!m2_trace->enabled(OPT_2, 4));
     }
-    SECTION("dst_port")
+
+    SECTION("module all")
     {
-        const Parameter dst_port_param("dst_port", Parameter::PT_INT, "0:65535", nullptr,
-          "test dst_port param");
+        MODULE_OPTION(all, 3l);
+        CHECK(tp.set_traces("mod_1", all));
 
-        Value val(false);
-        val.set(&dst_port_param);
+        REQUIRE(m1_trace != nullptr);
+        REQUIRE(m2_trace != nullptr);
 
-        val.set(200l);
-        CHECK( tp.set_constraints(val) );
+        CHECK(m1_trace->enabled(OPT_1, 3));
+        CHECK(m1_trace->enabled(OPT_2, 3));
+        CHECK(!m2_trace->enabled(OPT_1, 3));
+        CHECK(!m2_trace->enabled(OPT_2, 3));
     }
-    SECTION("invalid_param")
-    {
-        const Parameter invalid_param("invalid_param", Parameter::PT_INT, "0:8", nullptr,
-          "test invalid param");
 
-        Value val(false);
-        val.set(&invalid_param);
+    SECTION("options")
+    {
+        MODULE_OPTION(option1, 1l);
+        MODULE_OPTION(option2, 5l);
+        CHECK(tp.set_traces("mod_1", option1));
+        CHECK(tp.set_traces("mod_1", option2));
+        CHECK(tp.set_traces("mod_2", option1));
+        CHECK(tp.set_traces("mod_2", option2));
+
+        REQUIRE(m1_trace != nullptr);
+        REQUIRE(m2_trace != nullptr);
+
+        CHECK(m1_trace->enabled(OPT_1, 1));
+        CHECK(m1_trace->enabled(OPT_2, 1));
+        CHECK(m2_trace->enabled(OPT_1, 1));
+        CHECK(m2_trace->enabled(OPT_2, 1));
+
+        CHECK(!m1_trace->enabled(OPT_1, 5));
+        CHECK(m1_trace->enabled(OPT_2, 5));
+        CHECK(!m2_trace->enabled(OPT_1, 5));
+        CHECK(m2_trace->enabled(OPT_2, 5));
+    }
 
-        val.set(5l);
-        CHECK( !tp.set_constraints(val) );
+    SECTION("override all modules")
+    {
+        MODULE_OPTION(option1, 1l);
+        MODULE_OPTION(option2, 2l);
+        MODULE_OPTION(all, 3l);
+        CHECK(tp.set_traces("mod_1", option1));
+        CHECK(tp.set_traces("mod_2", option2));
+        CHECK(tp.set_traces("all", all));
+
+        REQUIRE(m1_trace != nullptr);
+        REQUIRE(m2_trace != nullptr);
+
+        CHECK(m1_trace->enabled(OPT_1, 1));
+        CHECK(m1_trace->enabled(OPT_2, 1));
+        CHECK(m2_trace->enabled(OPT_1, 1));
+        CHECK(m2_trace->enabled(OPT_2, 1));
+
+        CHECK(!m1_trace->enabled(OPT_1, 2));
+        CHECK(m1_trace->enabled(OPT_2, 2));
+        CHECK(m2_trace->enabled(OPT_1, 2));
+        CHECK(m2_trace->enabled(OPT_2, 2));
+
+        CHECK(!m1_trace->enabled(OPT_1, 3));
+        CHECK(m1_trace->enabled(OPT_2, 3));
+        CHECK(m2_trace->enabled(OPT_1, 3));
+        CHECK(!m2_trace->enabled(OPT_2, 3));
     }
+
+    auto sc = SnortConfig::get_conf();
+    if (sc and sc->trace_config)
+        sc->trace_config->setup_module_trace();
 }
 
-TEST_CASE("modules traces", "[TraceParser]")
+TEST_CASE("packet constraints", "[TraceParser]")
 {
     TraceConfig tc;
-    TraceParser tp(&tc);
+    TraceParser tp(tc);
 
-    SECTION("set_option")
+    SECTION("ip_proto")
     {
-        const Parameter detection_rule_eval("rule_eval", Parameter::PT_INT, "0:255", nullptr,
-            "test detection_rule_eval param");
+        PROTO_OPTION(ip_proto, 6l);
+        const PacketConstraints exp = { IpProtocol::TCP, 0, 0,
+            SfIp(), SfIp(), PacketConstraints::IP_PROTO };
 
-        const Parameter detection_detect_engine("detect_engine", Parameter::PT_INT, "0:255", nullptr,
-            "test detection_detect_engine param");
+        CHECK(tp.set_constraints(ip_proto));
+        tp.finalize_constraints();
 
-        Value val_opt1(false);
-        Value val_opt2(false);
-        val_opt1.set(&detection_rule_eval);
-        val_opt2.set(&detection_detect_engine);
+        REQUIRE(tc.constraints != nullptr);
+        CHECK(tc.constraints->set_bits == exp.set_bits);
+        CHECK(tc.constraints->ip_proto == exp.ip_proto);
+        CHECK(*tc.constraints == exp);
+    }
 
-        val_opt1.set(1l);
-        CHECK( tp.set_traces("detection", val_opt1) );
+    SECTION("src_ip")
+    {
+        ADDR_OPTION(src_ip, "10.1.2.3");
+        const uint32_t exp_ip = 0x0302010a;
+        const PacketConstraints exp = { IpProtocol::PROTO_NOT_SET, 0, 0,
+            SfIp(&exp_ip, AF_INET), SfIp(), PacketConstraints::SRC_IP };
+
+        CHECK(tp.set_constraints(src_ip));
+        tp.finalize_constraints();
+
+        REQUIRE(tc.constraints != nullptr);
+        CHECK(tc.constraints->set_bits == exp.set_bits);
+        CHECK(tc.constraints->src_ip == exp.src_ip);
+        CHECK(*tc.constraints == exp);
+    }
 
-        val_opt2.set(1l);
-        CHECK( tp.set_traces("detection", val_opt2) );
+    SECTION("invalid src_ip")
+    {
+        ADDR_OPTION(src_ip, "10.1.2.300");
+        CHECK(!tp.set_constraints(src_ip));
     }
-    SECTION("set_all")
+
+    SECTION("src_port")
     {
-        const Parameter decode_all(DEFAULT_TRACE_OPTION_NAME, Parameter::PT_INT, "0:255", nullptr,
-            "test decode_all param");
+        const PacketConstraints exp = { IpProtocol::PROTO_NOT_SET, 100, 0,
+            SfIp(), SfIp(), PacketConstraints::SRC_PORT };
+        PORT_OPTION(src_port, 100l);
 
-        Value val_all(false);
-        val_all.set(&decode_all);
+        CHECK(tp.set_constraints(src_port));
+        tp.finalize_constraints();
 
-        val_all.set(1l);
-        CHECK( tp.set_traces("decode", val_all) );
-        CHECK( tp.set_traces("detection", val_all) );
+        REQUIRE(tc.constraints != nullptr);
+        CHECK(tc.constraints->set_bits == exp.set_bits);
+        CHECK(tc.constraints->src_port == exp.src_port);
+        CHECK(*tc.constraints == exp);
     }
-    SECTION("set_invalid_option")
-    {
-        const Parameter invalid_param("invalid_opt", Parameter::PT_INT, "0:255", nullptr,
-            "test invalid param");
 
-        Value invalid_val(false);
-        invalid_val.set(&invalid_param);
+    SECTION("dst_ip")
+    {
+        ADDR_OPTION(dst_ip, "10.3.2.1");
+        const uint32_t exp_ip = 0x0102030a;
+        const PacketConstraints exp = { IpProtocol::PROTO_NOT_SET, 0, 0,
+            SfIp(), SfIp(&exp_ip, AF_INET), PacketConstraints::DST_IP };
+
+        CHECK(tp.set_constraints(dst_ip));
+        tp.finalize_constraints();
+
+        REQUIRE(tc.constraints != nullptr);
+        CHECK(tc.constraints->set_bits == exp.set_bits);
+        CHECK(tc.constraints->dst_ip == exp.dst_ip);
+        CHECK(*tc.constraints == exp);
+    }
 
-        invalid_val.set(1l);
-        CHECK( !tp.set_traces("detection", invalid_val) );
+    SECTION("invalid dst_ip")
+    {
+        ADDR_OPTION(dst_ip, "10.300.2.1");
+        CHECK(!tp.set_constraints(dst_ip));
     }
-    SECTION("set_invalid_module")
+
+    SECTION("dst_port")
     {
-        const Parameter all_param(DEFAULT_TRACE_OPTION_NAME, Parameter::PT_INT, "0:255", nullptr,
-            "test all param");
+        PORT_OPTION(dst_port, 200l);
+        const PacketConstraints exp = { IpProtocol::PROTO_NOT_SET, 0, 200,
+            SfIp(), SfIp(), PacketConstraints::DST_PORT };
 
-        Value val(false);
-        val.set(&all_param);
+        CHECK(tp.set_constraints(dst_port));
+        tp.finalize_constraints();
 
-        val.set(1l);
-        CHECK( !tp.set_traces("invalid_module", val) );
+        REQUIRE(tc.constraints != nullptr);
+        CHECK(tc.constraints->set_bits == exp.set_bits);
+        CHECK(tc.constraints->dst_port == exp.dst_port);
+        CHECK(*tc.constraints == exp);
+    }
+
+    SECTION("invalid option")
+    {
+        CONFIG_OPTION(invalid_option, 5l, Parameter::PT_INT, "0:8");
+        CHECK(!tp.set_constraints(invalid_option));
     }
 }
 
 #endif // UNIT_TEST
-
index 89da5fc1d151641c29d801ea14f62b58e467688f..d71b85a97aee937c23b20f2671fae671c061047b 100644 (file)
@@ -36,7 +36,7 @@ class TraceConfig;
 class TraceParser
 {
 public:
-    TraceParser(TraceConfig*);
+    TraceParser(TraceConfig&);
 
     bool set_traces(const std::string& option_name, const snort::Value& val);
     bool set_constraints(const snort::Value& val);
@@ -47,15 +47,15 @@ public:
 
     void reset_configured_trace_options();
 
-    TraceConfig* get_trace_config() const
+    TraceConfig& get_trace_config() const
     { return trace_config; }
 
 private:
     void init_configured_trace_options();
 
 private:
-    TraceConfig* trace_config = nullptr;
-    snort::PacketConstraints parsed_constraints;
+    TraceConfig& trace_config;
+    snort::PacketConstraints parsed_constraints{};
 
     static std::map<std::string, std::map<std::string, bool>> s_configured_trace_options;
 };
index ba212020463f5e73cdda2d357c2be96d90b25afb..094c2548c2185d81d8921269d156b207ee868ba8 100644 (file)
@@ -151,7 +151,7 @@ static int set(lua_State* L)
     TraceConfig* trace_config = new TraceConfig(sc->overlay_trace_config
         ? *sc->overlay_trace_config : *sc->trace_config);
 
-    TraceParser trace_parser(trace_config);
+    TraceParser trace_parser(*trace_config);
 
     const Parameter* params_tree = TraceSwapParams::get_params();
     bool parse_err = false;
@@ -183,7 +183,7 @@ static int set(lua_State* L)
             if ( lua_isboolean(L, -1) and root_parameter )
             {
                 set_ntuple = true;
-                trace_parser.get_trace_config()->log_ntuple = bool(lua_toboolean(L, -1));
+                trace_parser.get_trace_config().log_ntuple = bool(lua_toboolean(L, -1));
             }
             else
             {
@@ -329,7 +329,7 @@ static int set(lua_State* L)
             trace_parser.finalize_constraints();
 
         main_broadcast_command(new TraceSwap(
-            trace_parser.get_trace_config(), set_traces, set_constraints, set_ntuple),
+            &trace_parser.get_trace_config(), set_traces, set_constraints, set_ntuple),
             true);
     }
     else