From 9a643a8dd039ef57c2ca26cd0bd0364173f5e181 Mon Sep 17 00:00:00 2001 From: "Russ Combs (rucombs)" Date: Wed, 18 May 2016 07:06:33 -0400 Subject: [PATCH] Merge pull request #466 in SNORT/snort3 from ha-sc to master Squashed commit of the following: commit b2f8cf7996253e13761d0ec773400a41e14c4b47 Author: Ed Borgoyn Date: Tue May 17 15:02:59 2016 -0400 A few small code review fixes. commit b903d32fe80c4a4f19e80145a420696716cdd535 Author: Ed Borgoyn Date: Fri May 6 10:07:21 2016 -0400 Fix a compile warning. commit bfbd5abccc7a79b3283430a3307ae3402517953d Author: Ed Borgoyn Date: Fri May 6 09:28:29 2016 -0400 Forgot to add the file_connector_module_test to cmake commit a99fb124eeae1e6d52f40a6217cbe61f4ada4a00 Merge: 08612c6 fe16e75 Author: Ed Borgoyn Date: Thu May 5 14:10:32 2016 -0400 Merge branch 'ha-sc' of https://bitbucket-eng-rtp1.cisco.com/bitbucket/scm/snort/snort3 into ha-sc commit 08612c66de9c1750015fcbd1033661931bcda05e Author: Ed Borgoyn Date: Thu May 5 14:08:35 2016 -0400 UT's for file_connector. commit 2c5d4c13e44a644572e012e593e72ebe3d2b2b59 Author: Ed Borgoyn Date: Wed May 4 10:07:52 2016 -0400 Fixed test stubs to eliminate memory leaks. commit cc50492ed3d0f1ef5ded6ffcf3bedc238f7e9e4b Author: Ed Borgoyn Date: Tue May 3 15:06:02 2016 -0400 Makefile.am cosmetic changes. commit 03ff7b57fde7dd0ac747ed2f04707aefed0e6da9 Author: Ed Borgoyn Date: Tue May 3 13:00:14 2016 -0400 Remove extraneous stuff. commit 6b73c2c7ec76339f974875a6aa0f41825f70ea5d Author: Ed Borgoyn Date: Tue May 3 10:53:29 2016 -0400 Additional UT's for side_channel. commit fe16e7515225e59e75c6a975262d99260a1235b1 Author: Ed Borgoyn Date: Wed May 4 10:07:52 2016 -0400 Fixed test stubs to eliminate memory leaks. commit 67dec135d68701f4e12e4eeb94a6b8d86ac85afb Author: Ed Borgoyn Date: Tue May 3 15:06:02 2016 -0400 Makefile.am cosmetic changes. commit b83d121adaed4f6118b14319f6ce2d0dfc46d4f6 Author: Ed Borgoyn Date: Tue May 3 13:00:14 2016 -0400 Remove extraneous stuff. commit 0f903af76892e136b3a86ff538f8bbd5858ba0a6 Author: Ed Borgoyn Date: Tue May 3 10:53:29 2016 -0400 Additional UT's for side_channel. --- .../file_connector/test/CMakeLists.txt | 1 + .../file_connector/test/Makefile.am | 15 +- .../test/file_connector_module_test.cc | 102 ++++++ .../test/file_connector_test.cc | 301 +++++++++++++++++- src/side_channel/side_channel.cc | 13 +- src/side_channel/test/CMakeLists.txt | 1 + src/side_channel/test/Makefile.am | 18 +- .../test/side_channel_module_test.cc | 107 +++++++ src/side_channel/test/side_channel_test.cc | 219 +++++++++++-- 9 files changed, 734 insertions(+), 43 deletions(-) create mode 100644 src/connectors/file_connector/test/file_connector_module_test.cc create mode 100644 src/side_channel/test/side_channel_module_test.cc diff --git a/src/connectors/file_connector/test/CMakeLists.txt b/src/connectors/file_connector/test/CMakeLists.txt index 69a856c01..53b6fdf2f 100644 --- a/src/connectors/file_connector/test/CMakeLists.txt +++ b/src/connectors/file_connector/test/CMakeLists.txt @@ -1,2 +1,3 @@ add_cpputest(file_connector_test file_connector)) +add_cpputest(file_connector_module_test file_connector_module)) diff --git a/src/connectors/file_connector/test/Makefile.am b/src/connectors/file_connector/test/Makefile.am index fb0158dab..24ae6e13a 100644 --- a/src/connectors/file_connector/test/Makefile.am +++ b/src/connectors/file_connector/test/Makefile.am @@ -2,11 +2,22 @@ AM_DEFAULT_SOURCE_EXT = .cc check_PROGRAMS = \ -file_connector_test +file_connector_test \ +file_connector_module_test TESTS = $(check_PROGRAMS) file_connector_test_CPPFLAGS = @AM_CPPFLAGS@ @CPPUTEST_CPPFLAGS@ -file_connector_test_LDADD = ../libfile_connector.a ../../../framework/libframework.a @CPPUTEST_LDFLAGS@ +file_connector_test_LDADD = \ +../file_connector.o \ +../../../framework/libframework.a \ +@CPPUTEST_LDFLAGS@ + +file_connector_module_test_LDADD = \ +../file_connector_module.o \ +../../../framework/libframework.a \ +../../../sfip/libsfip.a \ +../../../catch/libcatch_tests.a \ +@CPPUTEST_LDFLAGS@ diff --git a/src/connectors/file_connector/test/file_connector_module_test.cc b/src/connectors/file_connector/test/file_connector_module_test.cc new file mode 100644 index 000000000..b46975460 --- /dev/null +++ b/src/connectors/file_connector/test/file_connector_module_test.cc @@ -0,0 +1,102 @@ +//-------------------------------------------------------------------------- +// Copyright (C) 2015-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. +//-------------------------------------------------------------------------- + +// file_connector_module_test.cc author Ed Borgoyn +// unit test main + +#include +#include + +#include "connectors/file_connector/file_connector_module.h" +#include "profiler/profiler.h" + +#include "main/snort_debug.h" + +THREAD_LOCAL SimpleStats file_connector_stats; +THREAD_LOCAL ProfileStats file_connector_perfstats; + +void show_stats(PegCount*, const PegInfo*, unsigned, const char*) { } + +void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*) { } + +void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*, FILE*) { } + +void Debug::print(const char*, int, uint64_t, const char*, ...) { } + +TEST_GROUP(file_connector_module) +{ + void setup() + { + MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); + } + + void teardown() + { + MemoryLeakWarningPlugin::turnOnNewDeleteOverloads(); + } +}; + +TEST(file_connector_module, test) +{ + Value connector_val("rx"); + Value name_val("rx"); + Value format_val("binary"); + Value direction_val("receive"); + Parameter direction_param = + {"direction", Parameter::PT_ENUM, "receive | transmit | duplex", nullptr, "direction"}; + Parameter format_param = + {"format", Parameter::PT_ENUM, "binary | text", nullptr, "format"}; + Parameter connector_param = + {"connector", Parameter::PT_STRING, nullptr, nullptr, "connector"}; + Parameter name_param = + {"name", Parameter::PT_STRING, nullptr, nullptr, "name"}; + + FileConnectorModule module; + + name_val.set(&name_param); + direction_val.set(&direction_param); + format_val.set(&format_param); + connector_val.set(&connector_param); + + module.begin("file_connector", 0, nullptr); + module.begin("file_connector", 1, nullptr); + module.set("file_connector.name", name_val, nullptr); + module.set("file_connector.direction", direction_val, nullptr); + module.set("file_connector.connector", connector_val, nullptr); + module.set("file_connector.format", format_val, nullptr); + module.end("file_connector", 1, nullptr); + module.end("file_connector", 0, nullptr); + + FileConnectorConfig::FileConnectorConfigSet* config_set = module.get_and_clear_config(); + + CHECK(config_set != nullptr); + + CHECK(config_set->size() == 1); + + FileConnectorConfig config = *(config_set->front()); + CHECK(config.name == "rx"); + CHECK(config.connector_name == "rx"); + CHECK(config.direction == Connector::CONN_RECEIVE); + CHECK(config.text_format == false); +} + +int main(int argc, char** argv) +{ + return CommandLineTestRunner::RunAllTests(argc, argv); +} + diff --git a/src/connectors/file_connector/test/file_connector_test.cc b/src/connectors/file_connector/test/file_connector_test.cc index 857a8d111..482f35276 100644 --- a/src/connectors/file_connector/test/file_connector_test.cc +++ b/src/connectors/file_connector/test/file_connector_test.cc @@ -27,6 +27,23 @@ #include "main/snort_debug.h" +extern const BaseApi* file_connector; +ConnectorApi* fc_api = nullptr; + +FileConnectorConfig connector_tx_text_config; +FileConnectorConfig connector_rx_text_config; +FileConnectorConfig connector_tx_binary_config; +FileConnectorConfig connector_rx_binary_config; + +Module* mod; + +ConnectorCommon* connector_common; + +Connector* connector_tt; +Connector* connector_rt; +Connector* connector_tb; +Connector* connector_rb; + void show_stats(PegCount*, const PegInfo*, unsigned, const char*) { } void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*) { } @@ -34,12 +51,294 @@ void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*) { } void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*, FILE*) { } const char* get_instance_file(std::string& file, const char* name) -{ UNUSED(file); UNUSED(name); return "filename"; } +{ file += name; return nullptr; } void Debug::print(const char*, int, uint64_t, const char*, ...) { } +FileConnectorModule::FileConnectorModule() : + Module("FC", "FC Help", nullptr) +{ } + +FileConnectorConfig::FileConnectorConfigSet* FileConnectorModule::get_and_clear_config() +{ + FileConnectorConfig::FileConnectorConfigSet* config_set = new FileConnectorConfig::FileConnectorConfigSet; + + return config_set; +} + +FileConnectorModule::~FileConnectorModule() { } + +ProfileStats* FileConnectorModule::get_profile() const { return nullptr; } + +bool FileConnectorModule::set(const char*, Value&, SnortConfig*) { return true; } + +bool FileConnectorModule::begin(const char*, int, SnortConfig*) { return true; } + +bool FileConnectorModule::end(const char*, int, SnortConfig*) { return true; } + +const PegInfo* FileConnectorModule::get_pegs() const { return nullptr; } + +PegCount* FileConnectorModule::get_counts() const { return nullptr; } + + +TEST_GROUP(file_connector) +{ + void setup() + { + // FIXIT-H - Workaround for CppUTest mem leak detector issue + MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); + fc_api = (ConnectorApi*)file_connector; + connector_tx_text_config.direction = Connector::CONN_TRANSMIT; + connector_tx_text_config.connector_name = "tx_t"; + connector_tx_text_config.name = "tx_t"; + connector_tx_text_config.text_format = true; + connector_rx_text_config.direction = Connector::CONN_RECEIVE; + connector_rx_text_config.connector_name = "rx_t"; + connector_rx_text_config.name = "rx_t"; + connector_rx_text_config.text_format = true; + connector_tx_binary_config.direction = Connector::CONN_TRANSMIT; + connector_tx_binary_config.connector_name = "tx_t"; + connector_tx_binary_config.name = "tx_t"; + connector_tx_binary_config.text_format = false; + connector_rx_binary_config.direction = Connector::CONN_RECEIVE; + connector_rx_binary_config.connector_name = "rx_t"; + connector_rx_binary_config.name = "rx_t"; + connector_rx_binary_config.text_format = false; + } + + void teardown() + { + MemoryLeakWarningPlugin::turnOnNewDeleteOverloads(); + } +}; + +TEST(file_connector, mod_ctor_dtor) +{ + CHECK(file_connector != nullptr); + mod = file_connector->mod_ctor(); + CHECK(mod != nullptr); + file_connector->mod_dtor(mod); +} + +TEST(file_connector, mod_instance_ctor_dtor) +{ + CHECK(file_connector != nullptr); + mod = file_connector->mod_ctor(); + CHECK(mod != nullptr); + connector_common = fc_api->ctor(mod); + CHECK(connector_common != nullptr); + fc_api->dtor(connector_common); + file_connector->mod_dtor(mod); +} + +TEST_GROUP(file_connector_tinit_tterm) +{ + void setup() + { + // FIXIT-H - Workaround for CppUTest mem leak detector issue + MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); + fc_api = (ConnectorApi*)file_connector; + connector_tx_text_config.direction = Connector::CONN_TRANSMIT; + connector_tx_text_config.connector_name = "tx_t"; + connector_tx_text_config.name = "tx_t"; + connector_tx_text_config.text_format = true; + connector_rx_text_config.direction = Connector::CONN_RECEIVE; + connector_rx_text_config.connector_name = "rx_t"; + connector_rx_text_config.name = "rx_t"; + connector_rx_text_config.text_format = true; + connector_tx_binary_config.direction = Connector::CONN_TRANSMIT; + connector_tx_binary_config.connector_name = "tx_b"; + connector_tx_binary_config.name = "tx_b"; + connector_tx_binary_config.text_format = false; + connector_rx_binary_config.direction = Connector::CONN_RECEIVE; + connector_rx_binary_config.connector_name = "rx_b"; + connector_rx_binary_config.name = "rx_b"; + connector_rx_binary_config.text_format = false; + CHECK(file_connector != nullptr); + mod = file_connector->mod_ctor(); + CHECK(mod != nullptr); + connector_common = fc_api->ctor(mod); + CHECK(connector_common != nullptr); + connector_tt = fc_api->tinit(&connector_tx_text_config); + connector_rt = fc_api->tinit(&connector_rx_text_config); + connector_tb = fc_api->tinit(&connector_tx_binary_config); + connector_rb = fc_api->tinit(&connector_rx_binary_config); + CHECK(connector_tt != nullptr); + CHECK(connector_rt != nullptr); + CHECK(connector_tb != nullptr); + CHECK(connector_rb != nullptr); + } + + void teardown() + { + fc_api->tterm(connector_tt); + fc_api->tterm(connector_rt); + fc_api->tterm(connector_tb); + fc_api->tterm(connector_rb); + fc_api->dtor(connector_common); + file_connector->mod_dtor(mod); + MemoryLeakWarningPlugin::turnOnNewDeleteOverloads(); + } +}; + +TEST(file_connector_tinit_tterm, null) +{ + CHECK(1==1); +} + +TEST(file_connector_tinit_tterm, alloc_discard) +{ + const uint8_t* data = nullptr; + FileConnector* fc_rt = (FileConnector*)connector_rt; + + FileConnectorMsgHandle* handle = (FileConnectorMsgHandle*)(fc_rt->alloc_message(40,&data)); + CHECK(data != nullptr); + CHECK(handle->connector_msg.length == 40); + CHECK(handle->connector_msg.data == data); + fc_rt->discard_message(handle); +} + +TEST_GROUP(file_connector_text) +{ + void setup() + { + // FIXIT-H - Workaround for CppUTest mem leak detector issue + MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); + fc_api = (ConnectorApi*)file_connector; + connector_tx_text_config.direction = Connector::CONN_TRANSMIT; + connector_tx_text_config.connector_name = "tx_t"; + connector_tx_text_config.name = "tx_t"; + connector_tx_text_config.text_format = true; + connector_rx_text_config.direction = Connector::CONN_RECEIVE; + connector_rx_text_config.connector_name = "rx_t"; + connector_rx_text_config.name = "rx_t"; + connector_rx_text_config.text_format = true; + CHECK(file_connector != nullptr); + } + + void teardown() + { + MemoryLeakWarningPlugin::turnOnNewDeleteOverloads(); + } +}; + +TEST(file_connector_text, alloc_transmit_rename_receive_discard) +{ + mod = file_connector->mod_ctor(); + CHECK(mod != nullptr); + connector_common = fc_api->ctor(mod); + CHECK(connector_common != nullptr); + connector_tt = fc_api->tinit(&connector_tx_text_config); + CHECK(connector_tt != nullptr); + FileConnector* fc_tt = (FileConnector*)connector_tt; + + const uint8_t* data = nullptr; + FileConnectorMsgHandle* t_handle = (FileConnectorMsgHandle*)(fc_tt->alloc_message(40,&data)); + CHECK(data != nullptr); + CHECK(t_handle->connector_msg.length == 40); + CHECK(t_handle->connector_msg.data == data); + CHECK(fc_tt->transmit_message(t_handle) == true); + + fc_api->tterm(connector_tt); + fc_api->dtor(connector_common); + file_connector->mod_dtor(mod); + + std::rename("file_connector_tx_t_transmit", "file_connector_rx_t_receive"); + + mod = file_connector->mod_ctor(); + CHECK(mod != nullptr); + connector_common = fc_api->ctor(mod); + CHECK(connector_common != nullptr); + connector_rt = fc_api->tinit(&connector_rx_text_config); + CHECK(connector_rt != nullptr); + FileConnector* fc_rt = (FileConnector*)connector_rt; + + FileConnectorMsgHandle* r_handle = (FileConnectorMsgHandle*)(fc_rt->receive_message(true)); + CHECK(r_handle != nullptr); + CHECK(r_handle->connector_msg.length == 40); + + fc_rt->discard_message(r_handle); + + fc_api->tterm(connector_rt); + fc_api->dtor(connector_common); + file_connector->mod_dtor(mod); +} + +TEST_GROUP(file_connector_binary) +{ + void setup() + { + // FIXIT-H - Workaround for CppUTest mem leak detector issue + MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); + fc_api = (ConnectorApi*)file_connector; + connector_tx_binary_config.direction = Connector::CONN_TRANSMIT; + connector_tx_binary_config.connector_name = "tx_b"; + connector_tx_binary_config.name = "tx_b"; + connector_tx_binary_config.text_format = false; + connector_rx_binary_config.direction = Connector::CONN_RECEIVE; + connector_rx_binary_config.connector_name = "rx_b"; + connector_rx_binary_config.name = "rx_b"; + connector_rx_binary_config.text_format = false; + CHECK(file_connector != nullptr); + } + + void teardown() + { + MemoryLeakWarningPlugin::turnOnNewDeleteOverloads(); + } +}; + +TEST(file_connector_binary, alloc_transmit_rename_receive_discard) +{ + mod = file_connector->mod_ctor(); + CHECK(mod != nullptr); + connector_common = fc_api->ctor(mod); + CHECK(connector_common != nullptr); + connector_tb = fc_api->tinit(&connector_tx_binary_config); + CHECK(connector_tb != nullptr); + FileConnector* fc_tb = (FileConnector*)connector_tb; + + const uint8_t* data = nullptr; + FileConnectorMsgHandle* t_handle = (FileConnectorMsgHandle*)(fc_tb->alloc_message(40,&data)); + CHECK(data != nullptr); + CHECK(t_handle->connector_msg.length == 40); + CHECK(t_handle->connector_msg.data == data); + CHECK(fc_tb->transmit_message(t_handle) == true); + + fc_api->tterm(connector_tb); + fc_api->dtor(connector_common); + file_connector->mod_dtor(mod); + + std::rename("file_connector_tx_b_transmit", "file_connector_rx_b_receive"); + + mod = file_connector->mod_ctor(); + CHECK(mod != nullptr); + connector_common = fc_api->ctor(mod); + CHECK(connector_common != nullptr); + connector_rb = fc_api->tinit(&connector_rx_binary_config); + CHECK(connector_rb != nullptr); + FileConnector* fc_rb = (FileConnector*)connector_rb; + + FileConnectorMsgHandle* r_handle = (FileConnectorMsgHandle*)(fc_rb->receive_message(true)); + CHECK(r_handle != nullptr); + CHECK(r_handle->connector_msg.length == 40); + + fc_rb->discard_message(r_handle); + + fc_api->tterm(connector_rb); + fc_api->dtor(connector_common); + file_connector->mod_dtor(mod); +} + TEST_GROUP(file_connector_msg_handle) { + void setup() + { + } + + void teardown() + { + } }; TEST(file_connector_msg_handle, test) diff --git a/src/side_channel/side_channel.cc b/src/side_channel/side_channel.cc index 90eb24b3c..76f118cf1 100644 --- a/src/side_channel/side_channel.cc +++ b/src/side_channel/side_channel.cc @@ -79,6 +79,9 @@ SideChannel::SideChannel() DebugMessage(DEBUG_SIDE_CHANNEL,"SideChannel::SideChannel()\n"); sequence = 0; default_port = 0; + connector_receive = nullptr; + connector_transmit = nullptr; + receive_handler = nullptr; } SideChannel::~SideChannel() @@ -88,8 +91,9 @@ SideChannel::~SideChannel() void SideChannel::set_message_port(SCMessage* msg, SCPort port) { - if ( msg != nullptr ) - msg->hdr->port = port; + assert ( msg ); + assert ( msg->hdr ); + msg->hdr->port = port; } void SideChannel::set_default_port(SCPort port) @@ -107,8 +111,7 @@ void SideChannelManager::instantiate(const SCConnectors* connectors, const PortB scm->connectors = *connectors; scm->ports = *ports; - // convert to rvalue for move to vector - s_maps.push_back(std::move(scm)); + s_maps.push_back(scm); } // Initialize state to be ready to accept configuration @@ -160,7 +163,7 @@ void SideChannelManager::thread_init() } /* Save the thread specific map */ - map_list->push_back(std::move(map)); + map_list->push_back(map); } /* Finally, save the thread-specific list */ diff --git a/src/side_channel/test/CMakeLists.txt b/src/side_channel/test/CMakeLists.txt index 4e968db8a..3a373be7c 100644 --- a/src/side_channel/test/CMakeLists.txt +++ b/src/side_channel/test/CMakeLists.txt @@ -1,2 +1,3 @@ add_cpputest(side_channel_test side_channel) +add_cpputest(side_channel_module_test side_channel_module) diff --git a/src/side_channel/test/Makefile.am b/src/side_channel/test/Makefile.am index 6ea3b4033..f1a74a77f 100644 --- a/src/side_channel/test/Makefile.am +++ b/src/side_channel/test/Makefile.am @@ -2,11 +2,25 @@ AM_DEFAULT_SOURCE_EXT = .cc check_PROGRAMS = \ -side_channel_test +side_channel_test \ +side_channel_module_test TESTS = $(check_PROGRAMS) side_channel_test_CPPFLAGS = @AM_CPPFLAGS@ @CPPUTEST_CPPFLAGS@ +side_channel_module_test_CPPFLAGS = @AM_CPPFLAGS@ @CPPUTEST_CPPFLAGS@ -side_channel_test_LDADD = ../libside_channel.a ../../framework/libframework.a ../../sfip/libsfip.a ../../catch/libcatch_tests.a @CPPUTEST_LDFLAGS@ +side_channel_test_LDADD = \ +../side_channel.o \ +../../framework/libframework.a \ +../../sfip/libsfip.a \ +../../catch/libcatch_tests.a \ +@CPPUTEST_LDFLAGS@ + +side_channel_module_test_LDADD = \ +../side_channel_module.o \ +../../framework/libframework.a \ +../../sfip/libsfip.a \ +../../catch/libcatch_tests.a \ +@CPPUTEST_LDFLAGS@ diff --git a/src/side_channel/test/side_channel_module_test.cc b/src/side_channel/test/side_channel_module_test.cc new file mode 100644 index 000000000..1071b8954 --- /dev/null +++ b/src/side_channel/test/side_channel_module_test.cc @@ -0,0 +1,107 @@ +//-------------------------------------------------------------------------- +// Copyright (C) 2015-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. +//-------------------------------------------------------------------------- + +// side_channel_module_test.cc author Ed Borgoyn +// unit test main + +#include +#include + +#include "side_channel/side_channel.h" +#include "side_channel/side_channel_module.h" + +#include "log/messages.h" +#include "main/snort_debug.h" +#include "profiler/profiler.h" + +THREAD_LOCAL SimpleStats sc_stats; +THREAD_LOCAL ProfileStats sc_perf_stats; + +static bool port_1_set = false; + +static char* make_bit_string(int bit) +{ + static char bit_string[65537]; + for ( int i=0; i<65536; i++) + bit_string[i] = (bit == i) ? '1' : '0'; + + bit_string[65536] = '\0'; + + return bit_string; +} + +void SideChannelManager::instantiate(const SCConnectors*, const PortBitSet* ports) +{ + port_1_set = ports->test(1); +} + +void show_stats(PegCount*, const PegInfo*, unsigned, const char*) { } +void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*) { } +void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*, FILE*) { } + +void ParseWarning(WarningGroup, const char*, ...) { } + +void Debug::print(const char*, int, uint64_t, const char*, ...) { } + +TEST_GROUP(side_channel_module) +{ + void setup() + { + } + + void teardown() + { + } +}; + +TEST(side_channel_module, test_connector_module_valid) +{ + Value ports_val(make_bit_string(1)); + Value connector_t_val("transmit"); + Value connector_r_val("receive"); + Parameter ports_param = {"ports", Parameter::PT_BIT_LIST, "65535", nullptr, "ports"}; + Parameter connector_param = {"connector", Parameter::PT_STRING, nullptr, nullptr, "connector"}; + + SideChannelModule module; + + ports_val.set(&ports_param); + connector_t_val.set(&connector_param); + connector_r_val.set(&connector_param); + + module.begin("side_channel", 0, nullptr); + module.begin("side_channel", 1, nullptr); + module.begin("side_channel.connectors", 0, nullptr); + module.begin("side_channel.connectors", 1, nullptr); + module.set("side_channel.connectors.connector", connector_t_val, nullptr); + module.end("side_channel.connectors", 1, nullptr); + module.begin("side_channel.connectors", 2, nullptr); + module.set("side_channel.connectors.connector", connector_r_val, nullptr); + module.end("side_channel.connectors", 2, nullptr); + module.end("side_channel.connectors", 0, nullptr); + module.set("side_channel.ports", ports_val, nullptr); + module.end("side_channel", 1, nullptr); + module.end("side_channel", 0, nullptr); + + CHECK(port_1_set == true); +} + +int main(int argc, char** argv) +{ + return CommandLineTestRunner::RunAllTests(argc, argv); +} + diff --git a/src/side_channel/test/side_channel_test.cc b/src/side_channel/test/side_channel_test.cc index 455b71b15..5ac397f04 100644 --- a/src/side_channel/test/side_channel_test.cc +++ b/src/side_channel/test/side_channel_test.cc @@ -23,62 +23,93 @@ #include #include "side_channel/side_channel.h" -#include "side_channel/side_channel_module.h" #include "log/messages.h" #include "main/snort_debug.h" #include "managers/connector_manager.h" -class TestConnector : public Connector +class TestConnectorMsgHandle : public ConnectorMsgHandle { - ConnectorMsgHandle* alloc_message(const uint32_t, const uint8_t**) { return nullptr; } - void discard_message(ConnectorMsgHandle*) { } - bool transmit_message(ConnectorMsgHandle*) { return true; } - ConnectorMsgHandle* receive_message(bool) { return nullptr; } - ConnectorMsg* get_connector_msg(ConnectorMsgHandle*) { return nullptr; } - Direction get_connector_direction() { return CONN_UNDEFINED; } +public: + TestConnectorMsgHandle(const uint32_t length) + { + connector_msg.length = length; + connector_msg.data = new uint8_t[length]; + } + + ~TestConnectorMsgHandle() + { + delete[] connector_msg.data; + } + + ConnectorMsg connector_msg; }; class ReceiveConnector : public Connector { - ConnectorMsgHandle* alloc_message(const uint32_t, const uint8_t**) { return nullptr; } - void discard_message(ConnectorMsgHandle*) { } - bool transmit_message(ConnectorMsgHandle*) { return true; } - ConnectorMsgHandle* receive_message(bool) { return nullptr; } - ConnectorMsg* get_connector_msg(ConnectorMsgHandle*) { return nullptr; } - Direction get_connector_direction() { return CONN_RECEIVE; } + ConnectorMsgHandle* alloc_message(const uint32_t length, const uint8_t** data) + { + TestConnectorMsgHandle* msg = new TestConnectorMsgHandle(length); + *data = (uint8_t*)msg->connector_msg.data; + return msg; + } + void discard_message(ConnectorMsgHandle* msg) + { delete (TestConnectorMsgHandle*)msg; } + bool transmit_message(ConnectorMsgHandle* msg) + { delete (TestConnectorMsgHandle*)msg; return true; } + ConnectorMsgHandle* receive_message(bool) + { return new TestConnectorMsgHandle(30); } + ConnectorMsg* get_connector_msg(ConnectorMsgHandle* handle) + { return &((TestConnectorMsgHandle*)handle)->connector_msg; } + Direction get_connector_direction() + { return CONN_RECEIVE; } }; class TransmitConnector : public Connector { - ConnectorMsgHandle* alloc_message(const uint32_t, const uint8_t**) { return nullptr; } - void discard_message(ConnectorMsgHandle*) { } - bool transmit_message(ConnectorMsgHandle*) { return true; } - ConnectorMsgHandle* receive_message(bool) { return nullptr; } - ConnectorMsg* get_connector_msg(ConnectorMsgHandle*) { return nullptr; } - Direction get_connector_direction() { return CONN_TRANSMIT; } + ConnectorMsgHandle* alloc_message(const uint32_t length, const uint8_t** data) + { + TestConnectorMsgHandle* msg = new TestConnectorMsgHandle(length); + *data = (uint8_t*)msg->connector_msg.data; + return msg; + } + void discard_message(ConnectorMsgHandle* msg) + { delete (TestConnectorMsgHandle*)msg; } + bool transmit_message(ConnectorMsgHandle* msg) + { delete (TestConnectorMsgHandle*)msg; return true; } + ConnectorMsgHandle* receive_message(bool) + { return nullptr; } + ConnectorMsg* get_connector_msg(ConnectorMsgHandle*) + { return nullptr; } + Direction get_connector_direction() + { return CONN_TRANSMIT; } }; -void ConnectorManager::thread_init() { } +Connector* receive_connector = nullptr; +Connector* transmit_connector = nullptr; + +void ConnectorManager::thread_init() +{ + receive_connector = new ReceiveConnector; + transmit_connector = new TransmitConnector; +} -void ConnectorManager::thread_term() { } +void ConnectorManager::thread_term() +{ + delete receive_connector; + delete transmit_connector; +} Connector* ConnectorManager::get_connector(const std::string connector_name) { - if ( connector_name == "U" ) - return new TestConnector(); - else if ( connector_name == "R" ) - return new ReceiveConnector(); + if ( connector_name == "R" ) + return receive_connector; else if ( connector_name == "T" ) - return new TransmitConnector(); + return transmit_connector; else return nullptr; } -void show_stats(PegCount*, const PegInfo*, unsigned, const char*) { } - -void show_stats(PegCount*, const PegInfo*, IndexVec&, const char*) { } - void ParseWarning(WarningGroup, const char*, ...) { } void Debug::print(const char*, int, uint64_t, const char*, ...) { } @@ -87,16 +118,138 @@ TEST_GROUP(side_channel) { void setup() { + // FIXIT-H - Workaround for issue with CppUTest memory leak detection + MemoryLeakWarningPlugin::turnOffNewDeleteOverloads(); + SideChannelManager::pre_config_init(); + + SCConnectors test_connectors; + PortBitSet test_ports; + + test_connectors.push_back("R"); + test_connectors.push_back("T"); + test_ports.set(1); + + SideChannelManager::instantiate(&test_connectors, &test_ports); + + test_connectors.clear(); + test_ports.reset(1); + + test_connectors.push_back("R"); + test_ports.set(2); + + SideChannelManager::instantiate(&test_connectors, &test_ports); + + test_connectors.clear(); + test_ports.reset(2); + + test_connectors.push_back("T"); + test_ports.set(3); + + SideChannelManager::instantiate(&test_connectors, &test_ports); + + test_connectors.clear(); + test_ports.reset(3); + + test_ports.set(4); + + SideChannelManager::instantiate(&test_connectors, &test_ports); + + test_connectors.clear(); + test_ports.reset(4); + + SideChannelManager::thread_init(); } void teardown() { + SideChannelManager::thread_term(); + SideChannelManager::term(); + MemoryLeakWarningPlugin::turnOnNewDeleteOverloads(); } }; -TEST(side_channel, test) +TEST(side_channel, test_connector_null) { - CHECK(1==1); +} + +TEST(side_channel, test_connector_get_none) +{ + SideChannel* sc = SideChannelManager::get_side_channel(5); + CHECK(sc == nullptr); +} + +TEST(side_channel, test_connector_directions) +{ + SideChannel* sc = SideChannelManager::get_side_channel(1); + CHECK(sc != nullptr); + CHECK(sc->get_direction() == Connector::CONN_DUPLEX); + + sc = SideChannelManager::get_side_channel(2); + CHECK(sc != nullptr); + CHECK(sc->get_direction() == Connector::CONN_RECEIVE); + + sc = SideChannelManager::get_side_channel(3); + CHECK(sc != nullptr); + CHECK(sc->get_direction() == Connector::CONN_TRANSMIT); + + sc = SideChannelManager::get_side_channel(4); + CHECK(sc != nullptr); + CHECK(sc->get_direction() == Connector::CONN_UNDEFINED); +} + +TEST(side_channel, test_connector_alloc_discard) +{ + SideChannel* sc = SideChannelManager::get_side_channel(1); + CHECK(sc != nullptr); + CHECK(sc->get_direction() == Connector::CONN_DUPLEX); + + sc->set_default_port(1); + + SCMessage* msg = sc->alloc_transmit_message(30); + CHECK(msg != nullptr); + + bool success = sc->discard_message(msg); + CHECK(success == true); +} + +TEST(side_channel, test_connector_alloc_transmit) +{ + SideChannel* sc = SideChannelManager::get_side_channel(1); + CHECK(sc != nullptr); + CHECK(sc->get_direction() == Connector::CONN_DUPLEX); + + sc->set_default_port(2); + SCMessage* msg = sc->alloc_transmit_message(30); + CHECK(msg != nullptr); + CHECK(msg->content_length == 30); + CHECK(msg->content != nullptr); + CHECK(msg->hdr->port == 2); + + sc->set_message_port(msg,1); + CHECK(msg->hdr->port == 1); + + bool success = sc->transmit_message(msg); + CHECK(success == true); +} + +static void receive_handler(SCMessage* sc_msg) +{ + CHECK(sc_msg != nullptr); + CHECK(sc_msg->sc != nullptr); + sc_msg->sc->discard_message(sc_msg); +} + +TEST(side_channel, test_connector_receive_process_dispatch_discard) +{ + SideChannel* sc = SideChannelManager::get_side_channel(1); + CHECK(sc != nullptr); + + sc->register_receive_handler(receive_handler); + + bool success = sc->process(1); + CHECK(success == true); + + sc->unregister_receive_handler(); } int main(int argc, char** argv) -- 2.47.3