From: Hui Cao (huica) Date: Fri, 27 Apr 2018 15:50:14 +0000 (-0400) Subject: Merge pull request #1203 in SNORT/snort3 from ProtocolReference_UT to master X-Git-Tag: 3.0.0-245~30 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6423e782757dcff79abc7e05b31fbc65a14d601c;p=thirdparty%2Fsnort3.git Merge pull request #1203 in SNORT/snort3 from ProtocolReference_UT to master Squashed commit of the following: commit 346ade396bba9a3212e4e28e5d58bf8eab1bc735 Author: Victor Roemer Date: Mon Apr 23 15:18:37 2018 -0400 target_based: unit tests for ProtocolReference class --- diff --git a/src/target_based/CMakeLists.txt b/src/target_based/CMakeLists.txt index d04bce8df..6c5617c7b 100644 --- a/src/target_based/CMakeLists.txt +++ b/src/target_based/CMakeLists.txt @@ -15,3 +15,5 @@ set ( TARGET_BASED_INCLUDES install (FILES ${TARGET_BASED_INCLUDES} DESTINATION "${INCLUDE_INSTALL_PATH}/target_based" ) + +add_subdirectory( test ) diff --git a/src/target_based/snort_protocols.cc b/src/target_based/snort_protocols.cc index 41d9a9f48..4b15cdd41 100644 --- a/src/target_based/snort_protocols.cc +++ b/src/target_based/snort_protocols.cc @@ -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)); } diff --git a/src/target_based/snort_protocols.h b/src/target_based/snort_protocols.h index ca331aef3..2d7b460f9 100644 --- a/src/target_based/snort_protocols.h +++ b/src/target_based/snort_protocols.h @@ -84,8 +84,7 @@ private: std::vector ind_map; std::unordered_map 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 index 000000000..2d9d62cd2 --- /dev/null +++ b/src/target_based/test/CMakeLists.txt @@ -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 index 000000000..a30132275 --- /dev/null +++ b/src/target_based/test/proto_ref_test.cc @@ -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, . + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "main/snort_config.h" +#include "main/snort_debug.h" +#include "target_based/snort_protocols.h" + +#include +#include + +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); +} +