From d4658d72ab15954ff0425505c89fdbe905e40b9c Mon Sep 17 00:00:00 2001 From: raissi-oussema Date: Sun, 8 Dec 2024 23:39:23 -0500 Subject: [PATCH] Fix serval issues related to multi-cross platform --- Packet++/header/DoIpLayerData.h | 2 +- Packet++/src/DoIpLayer.cpp | 9 ++++- Packet++/src/DoIpLayerData.cpp | 2 +- Tests/Packet++Test/Tests/DoIpTests.cpp | 37 +++++++++---------- Tests/Pcap++Test/Common/PcapFileNamesDef.h | 1 - Tests/Pcap++Test/TestDefinition.h | 3 -- Tests/Pcap++Test/Tests/PacketParsingTests.cpp | 17 --------- Tests/Pcap++Test/main.cpp | 1 - 8 files changed, 27 insertions(+), 45 deletions(-) diff --git a/Packet++/header/DoIpLayerData.h b/Packet++/header/DoIpLayerData.h index 5bad30dfcd..1b3c3e8736 100644 --- a/Packet++/header/DoIpLayerData.h +++ b/Packet++/header/DoIpLayerData.h @@ -600,7 +600,7 @@ namespace pcpp * - `maxConcurrentSockets`: Set to `0`, meaning no concurrent sockets are allowed by default. * - `currentlyOpenSockets`: Set to `0`, indicating no sockets are currently open. * - `maxDataSize`: Set to `nullptr`, meaning no data for this field. can be assigned after object - * creation by : + * * @code * EntityStatusResponseData data; * data.maxDataSize = std::unique_ptr>(new std::array {0x00, 0x01, 0x02, diff --git a/Packet++/src/DoIpLayer.cpp b/Packet++/src/DoIpLayer.cpp index fca1085f91..8a96cc3973 100644 --- a/Packet++/src/DoIpLayer.cpp +++ b/Packet++/src/DoIpLayer.cpp @@ -133,6 +133,11 @@ namespace pcpp std::string DoIpLayer::toString() const { std::stringstream os; + if (!resolveLayer()) + { + os << "Malformed doip Packet"; + return os.str(); + } DoIpProtocolVersion version = getProtocolVersion(); DoIpPayloadTypes type = getPayloadType(); uint32_t length = getPayloadLength(); @@ -140,8 +145,8 @@ namespace pcpp os << "DOIP Layer:" << std::endl; os << "Protocol Version: " << DoIpEnumToStringProtocolVersion.at(version) << std::hex << " (0x" << unsigned((uint8_t)version) << ")" << std::endl; - os << "Payload Type: " << getPayloadTypeAsStr() << std::hex << " (0x" << htole16((uint16_t)type) << ")" - << std::endl; + os << "Payload Type: " << getPayloadTypeAsStr() << std::hex << " (0x" << std::setw(4) << std::setfill('0') + << (uint16_t)type << ")" << std::endl; os << std::dec << "Payload Length: " << length << std::endl; return os.str(); diff --git a/Packet++/src/DoIpLayerData.cpp b/Packet++/src/DoIpLayerData.cpp index b480134daf..0a814e0653 100644 --- a/Packet++/src/DoIpLayerData.cpp +++ b/Packet++/src/DoIpLayerData.cpp @@ -281,7 +281,7 @@ namespace pcpp std::string VehicleIdentificationRequestVINData::toString() const { std::stringstream os; - os << "VIN: " << std::string(vin.data(), vin.end()) << std::endl; + os << "VIN: " << std::string(reinterpret_cast(vin.data()), vin.size()) << std::endl; return os.str(); } std::vector VehicleIdentificationRequestVINData::getData() const diff --git a/Tests/Packet++Test/Tests/DoIpTests.cpp b/Tests/Packet++Test/Tests/DoIpTests.cpp index 6214b4f8f8..accc79c1bb 100644 --- a/Tests/Packet++Test/Tests/DoIpTests.cpp +++ b/Tests/Packet++Test/Tests/DoIpTests.cpp @@ -49,7 +49,7 @@ PTF_TEST_CASE(DoIpGenericHeaderNackPacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 1); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Generic DOIP header Nack (0x0)\nPayload Length: 1\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Generic DOIP header Nack (0x0000)\nPayload Length: 1\n") } PTF_TEST_CASE(DoIpGenericHeaderNackPacketCreation) @@ -90,7 +90,7 @@ PTF_TEST_CASE(DoIpGenericHeaderNackPacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 1); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Generic DOIP header Nack (0x0)\nPayload Length: 1\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Generic DOIP header Nack (0x0000)\nPayload Length: 1\n") } // DoIpVehicleIdentificationRequestPacketParsing PTF_TEST_CASE(DoIpVehicleIdentificationRequestPacketParsing) @@ -124,7 +124,7 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestPacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadTypeAsStr(), "Vehicle identification request"); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request (0x1)\nPayload Length: 0\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request (0x0001)\nPayload Length: 0\n") PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 0x0); } // DoIpVehicleIdentificationRequestPacketParsing @@ -161,7 +161,7 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestPacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadTypeAsStr(), "Vehicle identification request"); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request (0x1)\nPayload Length: 0\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request (0x0001)\nPayload Length: 0\n") PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 0x0); } // VehicleIdentificationWithVIN @@ -195,7 +195,6 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestVINPacketParsing) if (data.buildFromLayer(doipLayer)) // std::cout << data.toString(); PTF_ASSERT_EQUAL(data.toString(), "VIN: BAUNEE4MZ17042403\n"); - // PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012); PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass); PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD); PTF_ASSERT_EQUAL(doipLayer->getPayloadType(), pcpp::DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN, @@ -204,7 +203,7 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestVINPacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 0x11); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with VIN (0x3)\nPayload Length: 17\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with VIN (0x0003)\nPayload Length: 17\n") } // DoIpVehicleIdentificationRequestVINPacketParsing @@ -248,7 +247,7 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestVINPacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 17); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with VIN (0x3)\nPayload Length: 17\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with VIN (0x0003)\nPayload Length: 17\n") } // VehicleIdentificationWithEID PTF_TEST_CASE(DoIpVehicleIdentificationRequestEIDPacketParsing) @@ -289,7 +288,7 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestEIDPacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 0x6); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with EID (0x2)\nPayload Length: 6\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with EID (0x0002)\nPayload Length: 6\n") } // DoIpVehicleIdentificationRequestVINPacketParsing @@ -335,7 +334,7 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestEIDPacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 6); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with EID (0x2)\nPayload Length: 6\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle identification request with EID (0x0002)\nPayload Length: 6\n") } // VehicleAnnouncement PTF_TEST_CASE(DoIpVehicleAnnouncementPacketParsing) @@ -379,7 +378,7 @@ PTF_TEST_CASE(DoIpVehicleAnnouncementPacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 32); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle announcement message / vehicle identification response message (0x4)\nPayload Length: 32\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle announcement message / vehicle identification response message (0x0004)\nPayload Length: 32\n") } // DoIpVehicleAnnouncementPacketParsing @@ -435,7 +434,7 @@ PTF_TEST_CASE(DoIpVehicleAnnouncementPacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 33); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle announcement message / vehicle identification response message (0x4)\nPayload Length: 33\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Vehicle announcement message / vehicle identification response message (0x0004)\nPayload Length: 33\n") } // RoutingActivationRequest PTF_TEST_CASE(DoIpRoutingActivationRequestPacketParsing) @@ -477,7 +476,7 @@ PTF_TEST_CASE(DoIpRoutingActivationRequestPacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 11); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation request (0x5)\nPayload Length: 11\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation request (0x0005)\nPayload Length: 11\n") } PTF_TEST_CASE(DoIpRoutingActivationRequestPacketCreation) @@ -524,7 +523,7 @@ PTF_TEST_CASE(DoIpRoutingActivationRequestPacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 11); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation request (0x5)\nPayload Length: 11\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation request (0x0005)\nPayload Length: 11\n") } // RoutingActivationResponse PTF_TEST_CASE(DoIpRoutingActivationResponsePacketParsing) @@ -564,7 +563,7 @@ PTF_TEST_CASE(DoIpRoutingActivationResponsePacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 9); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation response (0x6)\nPayload Length: 9\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation response (0x0006)\nPayload Length: 9\n") } PTF_TEST_CASE(DoIpRoutingActivationResponsePacketCreation) @@ -612,7 +611,7 @@ PTF_TEST_CASE(DoIpRoutingActivationResponsePacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 13); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation response (0x6)\nPayload Length: 13\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Routing activation response (0x0006)\nPayload Length: 13\n") } // --------------- // AliveCheckRequestPacket @@ -645,7 +644,7 @@ PTF_TEST_CASE(DoIpAliveCheckRequestPacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 0); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check request (0x7)\nPayload Length: 0\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check request (0x0007)\nPayload Length: 0\n") } PTF_TEST_CASE(DoIpAliveCheckRequestPacketCreation) @@ -685,7 +684,7 @@ PTF_TEST_CASE(DoIpAliveCheckRequestPacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 0); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check request (0x7)\nPayload Length: 0\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check request (0x0007)\nPayload Length: 0\n") } // --------------- // AliveCheckResponsePacket @@ -724,7 +723,7 @@ PTF_TEST_CASE(DoIpAliveCheckResponsePacketParsing) PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 2); PTF_ASSERT_EQUAL( doipLayer->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check response (0x8)\nPayload Length: 2\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check response (0x0008)\nPayload Length: 2\n") } PTF_TEST_CASE(DoIpAliveCheckResponsePacketCreation) @@ -764,7 +763,7 @@ PTF_TEST_CASE(DoIpAliveCheckResponsePacketCreation) PTF_ASSERT_EQUAL(_doipLayer2->getPayloadLength(), 2); PTF_ASSERT_EQUAL( _doipLayer2->toString(), - "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check response (0x8)\nPayload Length: 2\n") + "DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Alive check response (0x0008)\nPayload Length: 2\n") } // ------------------ // EntityStatusRequestPacket diff --git a/Tests/Pcap++Test/Common/PcapFileNamesDef.h b/Tests/Pcap++Test/Common/PcapFileNamesDef.h index d5ed769d31..41d724152b 100644 --- a/Tests/Pcap++Test/Common/PcapFileNamesDef.h +++ b/Tests/Pcap++Test/Common/PcapFileNamesDef.h @@ -34,4 +34,3 @@ #define EXAMPLE_PCAP_DESTRUCTOR2_PATH "PcapExamples/destructor2.pcap" #define EXAMPLE_PCAP_NANO_PATH "PcapExamples/nanosecs.pcap" #define EXAMPLE_PCAPNG_NANO_PATH "PcapExamples/nanosecs.pcapng" -#define EXAMPLE_PCAPNG_DOIP_PATH "PcapExamples/doip.pcap" diff --git a/Tests/Pcap++Test/TestDefinition.h b/Tests/Pcap++Test/TestDefinition.h index 2f6ebf149e..ccaaec4756 100644 --- a/Tests/Pcap++Test/TestDefinition.h +++ b/Tests/Pcap++Test/TestDefinition.h @@ -62,9 +62,6 @@ PTF_TEST_CASE(TestHttpResponseParsing); PTF_TEST_CASE(TestPrintPacketAndLayers); PTF_TEST_CASE(TestDnsParsing); -// Implemented in DoIpTests.cpp -PTF_TEST_CASE(DoIpPacketParsing); - // Implemented in TcpReassemblyTests.cpp PTF_TEST_CASE(TestTcpReassemblySanity); PTF_TEST_CASE(TestTcpReassemblyRetran); diff --git a/Tests/Pcap++Test/Tests/PacketParsingTests.cpp b/Tests/Pcap++Test/Tests/PacketParsingTests.cpp index cae1d41687..0c613f786e 100644 --- a/Tests/Pcap++Test/Tests/PacketParsingTests.cpp +++ b/Tests/Pcap++Test/Tests/PacketParsingTests.cpp @@ -475,20 +475,3 @@ PTF_TEST_CASE(TestDnsParsing) // wireshark filter: dns.count.add_rr > 0 and dns.resp.type == 47 PTF_ASSERT_EQUAL(additionalWithTypeNSEC, 14); } // TestDnsParsing - -PTF_TEST_CASE(DoIpPacketParsing) -{ - pcpp::PcapFileReaderDevice reader(EXAMPLE_PCAPNG_DOIP_PATH); - PTF_ASSERT_TRUE(reader.open()); - pcpp::RawPacket rawPacket; - while (reader.getNextPacket(rawPacket)) - { - pcpp::Packet packet(&rawPacket); - std::cout << packet.toString(); - // pcpp::DoIpLayer* doipLayer = packet.getLayerOfType(); - // PTF_ASSERT_TRUE(doipLayer->resolveProtocolVersion(doipLayer->getProtocolVersion(), - // doipLayer->getInvertProtocolVersion(), doipLayer->getPayloadType())); - // PTF_ASSERT_TRUE(doipLayer->resolvePayloadLength(doipLayer->getPayloadLength(), doipLayer->getPayloadType())); - pcpp::multiPlatformSleep(1); - } -} diff --git a/Tests/Pcap++Test/main.cpp b/Tests/Pcap++Test/main.cpp index 002f119c92..ca6c2b50b4 100644 --- a/Tests/Pcap++Test/main.cpp +++ b/Tests/Pcap++Test/main.cpp @@ -199,7 +199,6 @@ int main(int argc, char* argv[]) PTF_START_RUNNING_TESTS(userTagsInclude, userTagsExclude, configTags); testSetUp(); - PTF_RUN_TEST(DoIpPacketParsing, "no_network"); PTF_RUN_TEST(TestIPAddress, "no_network;ip"); PTF_RUN_TEST(TestMacAddress, "no_network;mac"); PTF_RUN_TEST(TestLRUList, "no_network");