]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3841: Add check for missing Geneve layer in get_geneve_options
authorSteve Chew (stechew) <stechew@cisco.com>
Wed, 10 May 2023 15:42:34 +0000 (15:42 +0000)
committerSteve Chew (stechew) <stechew@cisco.com>
Wed, 10 May 2023 15:42:34 +0000 (15:42 +0000)
Merge in SNORT/snort3 from ~STECHEW/snort3:fix_get_geneve_option to master

Squashed commit of the following:

commit 26ce9e4993fb40e1487e5eb5c466ec61099fd536
Author: Steve Chew <stechew@cisco.com>
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 <stechew@cisco.com>
Date:   Wed May 10 08:31:54 2023 -0400

    protocols: Add check for missing Geneve layer in get_geneve_options.

src/codecs/misc/test/CMakeLists.txt
src/protocols/CMakeLists.txt
src/protocols/packet.cc
src/protocols/test/CMakeLists.txt [new file with mode: 0644]
src/protocols/test/framework/api_options.h [new file with mode: 0644]
src/protocols/test/get_geneve_opt_test.cc [new file with mode: 0644]
src/sfip/test/sf_ip_test.cc

index 14ed27aea63ed81044a39c081191d7e3b9ae3dec..0e447a5959ff16ac29b1b61b4416436522fae1da 100644 (file)
@@ -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
+)
index 09c4bd97abff7dd1d82f1e9701cc02df2462ba12..9dabfe13bd77dc1210ef5a892cd14f1e112db5c8 100644 (file)
@@ -41,6 +41,8 @@ add_library (protocols OBJECT
     packet_manager.cc
 )
 
+add_subdirectory(test)
+
 install (FILES ${PROTOCOL_HEADERS}
     DESTINATION "${INCLUDE_INSTALL_PATH}/protocols"
 )
index ab299bd3a7ed155a057bf8403f0bee89ea474924..bf090eebc8262ca4e00f7b1a408fe8817fb0a115 100644 (file)
@@ -279,7 +279,10 @@ uint32_t Packet::get_flow_geneve_vni() const
 std::vector<snort::geneve::GeneveOptData> 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 (file)
index 0000000..b43021b
--- /dev/null
@@ -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 (file)
index 0000000..bb5dd6b
--- /dev/null
@@ -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 (file)
index 0000000..e4802d9
--- /dev/null
@@ -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 <stechew@cisco.com>
+
+#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 <CppUTest/CommandLineTestRunner.h>
+#include <CppUTest/TestHarness.h>
+#include <CppUTestExt/MockSupport.h>
+
+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);
+}
+
index e8b492d653d2c13bb14a252b515501a357addf1c..6d5b968b513174e4f7b33ee362f6ef2d7e2591cf 100644 (file)
@@ -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 )