From: Steve Chew (stechew) Date: Wed, 10 May 2023 15:42:34 +0000 (+0000) Subject: Pull request #3841: Add check for missing Geneve layer in get_geneve_options X-Git-Tag: 3.1.62.0~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=189bcbe8eb1ba9b88e825a20cc22bac5720a7dd7;p=thirdparty%2Fsnort3.git Pull request #3841: Add check for missing Geneve layer in get_geneve_options Merge in SNORT/snort3 from ~STECHEW/snort3:fix_get_geneve_option to master Squashed commit of the following: commit 26ce9e4993fb40e1487e5eb5c466ec61099fd536 Author: Steve Chew Date: Wed May 10 08:33:57 2023 -0400 sfip/test: Fix a miscalculation of the number of codes entries. commit 2bd6ed3dcc1e64a44fbdad95094d677f9cb00794 Author: Steve Chew Date: Wed May 10 08:31:54 2023 -0400 protocols: Add check for missing Geneve layer in get_geneve_options. --- diff --git a/src/codecs/misc/test/CMakeLists.txt b/src/codecs/misc/test/CMakeLists.txt index 14ed27aea..0e447a595 100644 --- a/src/codecs/misc/test/CMakeLists.txt +++ b/src/codecs/misc/test/CMakeLists.txt @@ -1,6 +1,4 @@ -if ( ENABLE_SHELL ) - add_cpputest(geneve_codec_test - SOURCES - ../../../framework/module.cc - ) -endif ( ENABLE_SHELL ) +add_cpputest(geneve_codec_test + SOURCES + ../../../framework/module.cc +) diff --git a/src/protocols/CMakeLists.txt b/src/protocols/CMakeLists.txt index 09c4bd97a..9dabfe13b 100644 --- a/src/protocols/CMakeLists.txt +++ b/src/protocols/CMakeLists.txt @@ -41,6 +41,8 @@ add_library (protocols OBJECT packet_manager.cc ) +add_subdirectory(test) + install (FILES ${PROTOCOL_HEADERS} DESTINATION "${INCLUDE_INSTALL_PATH}/protocols" ) diff --git a/src/protocols/packet.cc b/src/protocols/packet.cc index ab299bd3a..bf090eebc 100644 --- a/src/protocols/packet.cc +++ b/src/protocols/packet.cc @@ -279,7 +279,10 @@ uint32_t Packet::get_flow_geneve_vni() const std::vector Packet::get_geneve_options(bool inner) const { const snort::geneve::GeneveLyr* lyr = layer::get_geneve_layer(this, inner); - return lyr->get_opt_data(); + if (lyr) + return lyr->get_opt_data(); + else + return {}; } bool Packet::is_from_application_client() const diff --git a/src/protocols/test/CMakeLists.txt b/src/protocols/test/CMakeLists.txt new file mode 100644 index 000000000..b43021bb4 --- /dev/null +++ b/src/protocols/test/CMakeLists.txt @@ -0,0 +1,5 @@ + +add_cpputest( get_geneve_opt_test + SOURCES + ../packet.cc +) diff --git a/src/protocols/test/framework/api_options.h b/src/protocols/test/framework/api_options.h new file mode 100644 index 000000000..bb5dd6b9e --- /dev/null +++ b/src/protocols/test/framework/api_options.h @@ -0,0 +1 @@ +// Empty on purpose. Placeholder to make includes works. diff --git a/src/protocols/test/get_geneve_opt_test.cc b/src/protocols/test/get_geneve_opt_test.cc new file mode 100644 index 000000000..e4802d954 --- /dev/null +++ b/src/protocols/test/get_geneve_opt_test.cc @@ -0,0 +1,61 @@ +//-------------------------------------------------------------------------- +// Copyright (C) 2023-2023 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. +//-------------------------------------------------------------------------- +// get_geneve_opt_test.cc author Steve Chew + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "flow/expect_cache.h" +#include "framework/api_options.h" +#include "protocols/packet.h" +#include "protocols/packet_manager.h" + +#include +#include +#include + +using namespace snort; + +void snort::ExpectFlow::reset_expect_flows() {} +const char* PacketManager::get_proto_name(ProtocolId) { return nullptr; } +const vlan::VlanTagHdr* layer::get_vlan_layer(const Packet*) { return nullptr; } +const geneve::GeneveLyr* layer::get_geneve_layer(const Packet*, bool) { return nullptr; } +void ip::IpApi::reset() {} +THREAD_LOCAL uint8_t CodecManager::max_layers = 0; + + +TEST_GROUP(get_geneve_opt_tests) +{ +}; + +TEST(get_geneve_opt_tests, no_geneve_data) +{ + Packet packet; + auto vec = packet.get_geneve_options(false); + CHECK_TRUE( vec.empty() ); +} + +//------------------------------------------------------------------------- +// main +//------------------------------------------------------------------------- +int main(int argc, char** argv) +{ + return CommandLineTestRunner::RunAllTests(argc, argv); +} + diff --git a/src/sfip/test/sf_ip_test.cc b/src/sfip/test/sf_ip_test.cc index e8b492d65..6d5b968b5 100644 --- a/src/sfip/test/sf_ip_test.cc +++ b/src/sfip/test/sf_ip_test.cc @@ -346,7 +346,7 @@ static int FuncCheck(int i) result = RunFunc(f->func, f->arg1, f->arg2); - code = (0 <= result && (size_t)result < sizeof(codes)/sizeof(code[0])) ? + code = (0 <= result && (size_t)result < sizeof(codes)/sizeof(codes[0])) ? codes[result] : "uh oh"; if ( result != f->expected )