]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #1203 in SNORT/snort3 from ProtocolReference_UT to master
authorHui Cao (huica) <huica@cisco.com>
Fri, 27 Apr 2018 15:50:14 +0000 (11:50 -0400)
committerHui Cao (huica) <huica@cisco.com>
Fri, 27 Apr 2018 15:50:14 +0000 (11:50 -0400)
Squashed commit of the following:

commit 346ade396bba9a3212e4e28e5d58bf8eab1bc735
Author: Victor Roemer <viroemer@cisco.com>
Date:   Mon Apr 23 15:18:37 2018 -0400

    target_based: unit tests for ProtocolReference class

src/target_based/CMakeLists.txt
src/target_based/snort_protocols.cc
src/target_based/snort_protocols.h
src/target_based/test/CMakeLists.txt [new file with mode: 0644]
src/target_based/test/proto_ref_test.cc [new file with mode: 0644]

index d04bce8df46b564fd04af70589821745b473aaad..6c5617c7b6ecb216e666cc780b6064447fee1ff0 100644 (file)
@@ -15,3 +15,5 @@ set ( TARGET_BASED_INCLUDES
 install (FILES ${TARGET_BASED_INCLUDES}
     DESTINATION "${INCLUDE_INSTALL_PATH}/target_based"
 )
+
+add_subdirectory( test )
index 41d9a9f4816455e5c4f9db1ade3511f9686ea24f..4b15cdd419a6fe8597c760bb9fecad9c44b3a8fc 100644 (file)
@@ -112,11 +112,10 @@ SnortProtocolId ProtocolReference::find(const char* protocol)
 
 void ProtocolReference::init(ProtocolReference* old_proto_ref)
 {
-    id_map.push_back("unknown");
-
     if(!old_proto_ref)
     {
-        bool ok = ( add("ip") == SNORT_PROTO_IP );
+        bool ok = ( add("unknown") == UNKNOWN_PROTOCOL_ID );
+        ok = ( add("ip") == SNORT_PROTO_IP ) and ok;
         ok = ( add("icmp") == SNORT_PROTO_ICMP ) and ok;
         ok = ( add("tcp") == SNORT_PROTO_TCP ) and ok;
         ok = ( add("udp") == SNORT_PROTO_UDP ) and ok;
@@ -127,7 +126,7 @@ void ProtocolReference::init(ProtocolReference* old_proto_ref)
     else
     {
         // Copy old ProtocolReference ID/name pairs to new ProtocolReference
-        for(SnortProtocolId id = 1; id < old_proto_ref->get_count(); id++)
+        for(SnortProtocolId id = 0; id < old_proto_ref->get_count(); id++)
         {
             add(old_proto_ref->get_name(id));
         }
index ca331aef3ec417b61b66f96a23cb778fa1dbb022..2d7b460f954005f385721d8c49163de813fba86c 100644 (file)
@@ -84,8 +84,7 @@ private:
     std::vector<SnortProtocolId> ind_map;
     std::unordered_map<std::string, SnortProtocolId> ref_table;
 
-    // Start at 1 since 0 will be "unknown".
-    SnortProtocolId protocol_number = 1;
+    SnortProtocolId protocol_number = 0;
 
     void init(ProtocolReference* old_proto_ref);
 };
diff --git a/src/target_based/test/CMakeLists.txt b/src/target_based/test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..2d9d62c
--- /dev/null
@@ -0,0 +1,5 @@
+
+add_cpputest( proto_ref_test
+    SOURCES
+        ../snort_protocols.cc
+        )
diff --git a/src/target_based/test/proto_ref_test.cc b/src/target_based/test/proto_ref_test.cc
new file mode 100644 (file)
index 0000000..a301322
--- /dev/null
@@ -0,0 +1,193 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2015-2018 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.
+//--------------------------------------------------------------------------
+
+// proto_ref_test.cc author Victor Roemer, <viroemer@cisco.com>.
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <iostream>
+
+#include "main/snort_config.h"
+#include "main/snort_debug.h"
+#include "target_based/snort_protocols.h"
+
+#include <CppUTest/CommandLineTestRunner.h>
+#include <CppUTest/TestHarness.h>
+
+using namespace snort;
+
+void Debug::print(const char*, int, uint64_t, const char*, ...) { }
+
+TEST_GROUP(protocol_reference)
+{};
+
+// Service Protocols
+//
+// Verify normal behaviour of service protocols being registered.
+//   1. Check that ID's are unique
+//   2. Check that same ID's are returned on |add| and |find|
+//   3. Check the builtin tests
+//     * is_network_protocol()
+//     * is_builtin_protocol()
+//     * is_service_protocol()
+TEST(protocol_reference, service_protocols)
+{
+    ProtocolReference refs;
+
+    SnortProtocolId t0 = refs.add("test0");
+    SnortProtocolId t1 = refs.add("test1");
+    SnortProtocolId t2 = refs.add("test2");
+    SnortProtocolId t3 = refs.add("test3");
+
+    CHECK( refs.find("test0") == t0 );
+    CHECK( refs.find("test1") == t1 );
+    CHECK( refs.find("test2") == t2 );
+    CHECK( refs.find("test3") == t3 );
+
+    CHECK( t0 < t1 and t1 < t2 and t2 < t3 );
+
+    CHECK( !is_network_protocol(t0) );
+    CHECK( !is_builtin_protocol(t0) );
+    CHECK( is_service_protocol(t0) );
+
+    CHECK( !is_network_protocol(t1) );
+    CHECK( !is_builtin_protocol(t1) );
+    CHECK( is_service_protocol(t1) );
+
+    CHECK( !is_network_protocol(t2) );
+    CHECK( !is_builtin_protocol(t2) );
+    CHECK( is_service_protocol(t2) );
+
+    CHECK( !is_network_protocol(t3) );
+    CHECK( !is_builtin_protocol(t3) );
+    CHECK( is_service_protocol(t3) );
+}
+
+// Builtin Protocols (ip, icmp, tcp, udp, user and file)
+//
+// Verify normal behaviour of the builtin protocols.
+//   1. Check the builtin protocols match the hardcoded ID's
+//   2. Check the builtin tests
+//     * is_network_protocol()
+//     * is_builtin_protocol()
+//     * is_service_protocol()
+TEST(protocol_reference, builtin_protocols) 
+{
+    ProtocolReference refs;
+
+    SnortProtocolId unknown = refs.add("unknown");
+    CHECK( unknown == UNKNOWN_PROTOCOL_ID );
+    CHECK( !is_network_protocol(unknown) );
+    CHECK( is_builtin_protocol(unknown) );
+    CHECK( !is_service_protocol(unknown) );
+
+    SnortProtocolId ip = refs.add("ip");
+    CHECK( ip == SNORT_PROTO_IP );
+    CHECK( is_network_protocol(ip) );
+    CHECK( is_builtin_protocol(ip) );
+    CHECK( !is_service_protocol(ip) );
+
+    SnortProtocolId icmp = refs.add("icmp");
+    CHECK( icmp == SNORT_PROTO_ICMP );
+    CHECK( is_network_protocol(icmp) );
+    CHECK( is_builtin_protocol(icmp) );
+    CHECK( !is_service_protocol(icmp) );
+
+    SnortProtocolId tcp = refs.add("tcp");
+    CHECK( tcp == SNORT_PROTO_TCP );
+    CHECK( is_network_protocol(tcp) );
+    CHECK( is_builtin_protocol(tcp) );
+    CHECK( !is_service_protocol(tcp) );
+
+    SnortProtocolId udp = refs.add("udp");
+    CHECK( udp == SNORT_PROTO_UDP );
+    CHECK( is_network_protocol(udp) );
+    CHECK( is_builtin_protocol(udp) );
+    CHECK( !is_service_protocol(udp) );
+
+    SnortProtocolId user = refs.add("user");
+    CHECK( user == SNORT_PROTO_USER );
+    CHECK( !is_network_protocol(user) );
+    CHECK( is_builtin_protocol(user) );
+    CHECK( is_service_protocol(user) );
+
+    SnortProtocolId file = refs.add("file");
+    CHECK( file == SNORT_PROTO_FILE );
+    CHECK( !is_network_protocol(file) );
+    CHECK( is_builtin_protocol(file) );
+    CHECK( is_service_protocol(file) );
+}
+
+// Find none
+//
+// Verify UNKNOWN_PROTOCOL_ID is returned for non-existent protocols
+TEST(protocol_reference, find_none)
+{
+    ProtocolReference refs;
+    CHECK(refs.find("nothing") == UNKNOWN_PROTOCOL_ID);
+}
+
+// Double add
+//
+// Verify protocols only added once
+TEST(protocol_reference, double_add)
+{
+    ProtocolReference refs;
+    SnortProtocolId id = refs.add("http");
+    CHECK( id == refs.add("http") );
+}
+
+// Nullptr add
+//
+// Verify UNKNOWN_PROTOCOL_ID is returned when calling add with a null string
+TEST(protocol_reference, nullptr_add)
+{
+    ProtocolReference refs;
+    CHECK( refs.add(nullptr) == UNKNOWN_PROTOCOL_ID );
+}
+
+// Reverse Lookup Unknown
+//
+// Verify "unknown" is returned when looking up UNKNOWN_PROTOCOL_ID
+TEST(protocol_reference, reverse_lookup)
+{
+    ProtocolReference refs;
+    CHECK( std::string(refs.get_name(UNKNOWN_PROTOCOL_ID)) == "unknown");
+}
+
+// Construct from old references
+//
+// Verify construction from an previously existing ProtocolReference
+TEST(protocol_reference, constructors)
+{
+    ProtocolReference old;
+    SnortProtocolId unknown = old.find("unknown");
+    SnortProtocolId custom = old.add("custom");
+
+    ProtocolReference refs(&old);
+    CHECK( refs.find("custom") == custom );
+    CHECK( refs.find("unknown") == unknown );
+}
+
+int main(int argc, char** argv)
+{
+    return CommandLineTestRunner::RunAllTests(argc, argv);
+}
+