]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #1822 in SNORT/snort3 from ~SBAIGAL/snort3:databus_utest to master
authorMichael Altizer (mialtize) <mialtize@cisco.com>
Mon, 28 Oct 2019 14:09:14 +0000 (10:09 -0400)
committerMichael Altizer (mialtize) <mialtize@cisco.com>
Mon, 28 Oct 2019 14:09:14 +0000 (10:09 -0400)
Squashed commit of the following:

commit 19facb8667cfdbca840d17050e8c0662c72d7c59
Author: Steven Baigal (sbaigal) <sbaigal@cisco.com>
Date:   Thu Oct 24 10:48:14 2019 -0400

    data_bus: add unit test cases

src/framework/CMakeLists.txt
src/framework/test/CMakeLists.txt [new file with mode: 0644]
src/framework/test/data_bus_test.cc [new file with mode: 0644]

index bd77471215d59ccc9b113e71a9bd1f8bdcd167b2..a4db5a5f7056d6fdfafceac23c40876690cba38c 100644 (file)
@@ -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 (file)
index 0000000..ba74576
--- /dev/null
@@ -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 (file)
index 0000000..d237572
--- /dev/null
@@ -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 <sbaigal@cisco.com>
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "framework/data_bus.h"
+#include "main/snort_config.h"
+
+
+#include <CppUTest/CommandLineTestRunner.h>
+#include <CppUTest/TestHarness.h>
+
+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);
+}
+