From: Michael Altizer (mialtize) Date: Mon, 28 Oct 2019 14:09:14 +0000 (-0400) Subject: Merge pull request #1822 in SNORT/snort3 from ~SBAIGAL/snort3:databus_utest to master X-Git-Tag: 3.0.0-263~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cce730b92efe20a36a0bd3d31edbd0615eec7047;p=thirdparty%2Fsnort3.git Merge pull request #1822 in SNORT/snort3 from ~SBAIGAL/snort3:databus_utest to master Squashed commit of the following: commit 19facb8667cfdbca840d17050e8c0662c72d7c59 Author: Steven Baigal (sbaigal) Date: Thu Oct 24 10:48:14 2019 -0400 data_bus: add unit test cases --- diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index bd7747121..a4db5a5f7 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(test) set (FRAMEWORK_INCLUDES base_api.h diff --git a/src/framework/test/CMakeLists.txt b/src/framework/test/CMakeLists.txt new file mode 100644 index 000000000..ba7457616 --- /dev/null +++ b/src/framework/test/CMakeLists.txt @@ -0,0 +1,4 @@ + +add_cpputest( data_bus_test + SOURCES ../data_bus.cc +) diff --git a/src/framework/test/data_bus_test.cc b/src/framework/test/data_bus_test.cc new file mode 100644 index 000000000..d237572be --- /dev/null +++ b/src/framework/test/data_bus_test.cc @@ -0,0 +1,159 @@ +//-------------------------------------------------------------------------- +// Copyright (C) 2019-2019 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. +//-------------------------------------------------------------------------- +// data_bus_test.cc author Steven Baigal + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "framework/data_bus.h" +#include "main/snort_config.h" + + +#include +#include + +using namespace snort; + +//-------------------------------------------------------------------------- +// mocks +//-------------------------------------------------------------------------- +InspectionPolicy::InspectionPolicy(unsigned int) {} +InspectionPolicy::~InspectionPolicy() {} +namespace snort +{ +SnortConfig::SnortConfig(snort::SnortConfig const*) +{ + global_dbus = new DataBus(); +} +THREAD_LOCAL SnortConfig* snort_conf = nullptr; +SnortConfig* SnortConfig::get_conf() +{ return snort_conf; } +SnortConfig::~SnortConfig() +{ + delete global_dbus; +} + +static InspectionPolicy* my_inspection_policy = nullptr; +InspectionPolicy* get_inspection_policy() +{ return my_inspection_policy; } +} +//-------------------------------------------------------------------------- +class UTestEvent : public DataEvent +{ +public: + UTestEvent(int m) : msg(m) { } + + int get_message() + { return msg; } + +private: + int msg; +}; + +class UTestHandler : public DataHandler +{ +public: + UTestHandler() : DataHandler("unit_test") + { } + + void handle(DataEvent&, Flow*) override; + + int evt_msg = 0; +}; + +void UTestHandler::handle(DataEvent& event, Flow*) +{ + UTestEvent* evt = (UTestEvent*)&event; + evt_msg = evt->get_message(); +} +#define DB_UTEST_EVENT "unit.test.event" + +//-------------------------------------------------------------------------- +// data bus unit tests +//-------------------------------------------------------------------------- + +TEST_GROUP(data_bus) +{ + void setup() override + { + snort_conf = new SnortConfig(); + my_inspection_policy = new InspectionPolicy(); + } + + void teardown() override + { + delete my_inspection_policy; + delete snort_conf; + } +}; + +TEST(data_bus, subscribe_global) +{ + SnortConfig* sc = SnortConfig::get_conf(); + UTestHandler* h = new UTestHandler(); + DataBus::subscribe_global(DB_UTEST_EVENT, h, sc); + + UTestEvent event(100); + DataBus::publish(DB_UTEST_EVENT, event); + CHECK(100 == h->evt_msg); + + UTestEvent event1(200); + DataBus::publish(DB_UTEST_EVENT, event1); + CHECK(200 == h->evt_msg); + + DataBus::unsubscribe_global(DB_UTEST_EVENT, h, sc); + + UTestEvent event2(300); + DataBus::publish(DB_UTEST_EVENT, event2); + CHECK(200 == h->evt_msg); // unsubscribed! + + delete h; +} + +TEST(data_bus, subscribe) +{ + UTestHandler* h = new UTestHandler(); + DataBus::subscribe(DB_UTEST_EVENT, h); + + UTestEvent event(100); + DataBus::publish(DB_UTEST_EVENT, event); + CHECK(100 == h->evt_msg); + + UTestEvent event1(200); + DataBus::publish(DB_UTEST_EVENT, event1); + CHECK(200 == h->evt_msg); + + DataBus::unsubscribe(DB_UTEST_EVENT, h); + + UTestEvent event2(300); + DataBus::publish(DB_UTEST_EVENT, event2); + CHECK(200 == h->evt_msg); // unsubscribed! + + delete h; +} + +//------------------------------------------------------------------------- +// main +//------------------------------------------------------------------------- + +int main(int argc, char** argv) +{ + return CommandLineTestRunner::RunAllTests(argc, argv); +} +