From 81d4434e24512d0b8ad2f8e7600b52c9a9c65e5d Mon Sep 17 00:00:00 2001 From: Vivien Voros Date: Tue, 22 Aug 2023 13:31:37 +0200 Subject: [PATCH] modify toString and add test --- Packet++/src/S7commLayer.cpp | 95 ++++++++++++------------ Tests/Packet++Test/Tests/S7commTests.cpp | 1 + 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/Packet++/src/S7commLayer.cpp b/Packet++/src/S7commLayer.cpp index de0f82a9d8..e58760a180 100644 --- a/Packet++/src/S7commLayer.cpp +++ b/Packet++/src/S7commLayer.cpp @@ -1,22 +1,23 @@ #include "EndianPortable.h" #include "TpktLayer.h" -#include -#include "TcpLayer.h" #include "S7commLayer.h" +#include "TcpLayer.h" +#include #include #include -namespace pcpp { +namespace pcpp +{ - S7commLayer::S7commLayer(uint8_t msg_type, uint16_t pdu_ref, - uint16_t param_length, uint16_t data_length) { + S7commLayer::S7commLayer(uint8_t msg_type, uint16_t pdu_ref, uint16_t param_length, uint16_t data_length) + { const size_t headerLen = sizeof(s7commhdr); m_DataLen = headerLen; m_Data = new uint8_t[headerLen]; memset(m_Data, 0, headerLen); - s7commhdr *s7commHdr = (s7commhdr *) m_Data; + s7commhdr *s7commHdr = (s7commhdr *)m_Data; s7commHdr->protocol_id = 0x32; s7commHdr->msg_type = msg_type; s7commHdr->reserved = 0x0000; @@ -26,22 +27,30 @@ namespace pcpp { m_Protocol = S7COMM; } - std::string S7commLayer::toString() const { - std::ostringstream msgTypeStream; - msgTypeStream << getMsgType(); - std::ostringstream pduRefStream; - pduRefStream << getPduRef(); - std::ostringstream paramLengthStream; - paramLengthStream << getParamLength(); - std::ostringstream dataLengthStream; - dataLengthStream << getDataLength(); - - return "S7comm Layer, msg_type: " + msgTypeStream.str() + - ", pdu_ref: " + pduRefStream.str() + - ", param_length: " + paramLengthStream.str() + - ", data_length: " + dataLengthStream.str(); - - } + std::string S7commLayer::toString() const + { + // std::ostringstream msgTypeStream; + // msgTypeStream << getMsgType(); + // std::ostringstream pduRefStream; + // pduRefStream << getPduRef(); + // std::ostringstream paramLengthStream; + // paramLengthStream << getParamLength(); + // std::ostringstream dataLengthStream; + // dataLengthStream << getDataLength(); + + // return "S7comm Layer, msg_type: " + msgTypeStream.str() + + // ", pdu_ref: " + pduRefStream.str() + + // ", param_length: " + paramLengthStream.str() + + // ", data_length: " + dataLengthStream.str(); + + std::ostringstream str; + str << "S7comm Layer, " + << "msg_type: " << std::to_string(getMsgType()) + << ", pdu_ref: " << std::to_string(getPduRef()) + << ", param_length: " << std::to_string(getParamLength()) + << ", data_length: " << std::to_string(getDataLength()); + return str.str(); + } bool S7commLayer::isDataValid(const uint8_t *data, size_t dataSize) { @@ -51,43 +60,33 @@ namespace pcpp { return data[0] == 0x32; } - uint8_t S7commLayer::getProtocolId() const { - return getS7commHeader()->protocol_id; - } + uint8_t S7commLayer::getProtocolId() const { return getS7commHeader()->protocol_id; } - uint8_t S7commLayer::getMsgType() const { - return getS7commHeader()->msg_type; - } + uint8_t S7commLayer::getMsgType() const { return getS7commHeader()->msg_type; } - uint16_t S7commLayer::getReserved() const { - return htobe16(getS7commHeader()->reserved); - } + uint16_t S7commLayer::getReserved() const { return htobe16(getS7commHeader()->reserved); } - uint16_t S7commLayer::getParamLength() const { - return htobe16(getS7commHeader()->param_length); - } + uint16_t S7commLayer::getParamLength() const { return htobe16(getS7commHeader()->param_length); } - uint16_t S7commLayer::getPduRef() const { - return htobe16(getS7commHeader()->pdu_ref);; - } + uint16_t S7commLayer::getPduRef() const + { + return htobe16(getS7commHeader()->pdu_ref); + ; + } - uint16_t S7commLayer::getDataLength() const { - return htobe16(getS7commHeader()->data_length); - } + uint16_t S7commLayer::getDataLength() const { return htobe16(getS7commHeader()->data_length); } - void S7commLayer::setMsgType(uint8_t msg_type) const { - getS7commHeader()->msg_type = msg_type; - } + void S7commLayer::setMsgType(uint8_t msg_type) const { getS7commHeader()->msg_type = msg_type; } - void S7commLayer::setParamLength(uint16_t param_length) const { + void S7commLayer::setParamLength(uint16_t param_length) const + { getS7commHeader()->param_length = htobe16(param_length); } - void S7commLayer::setPduRef(uint16_t pdu_ref) const { - getS7commHeader()->pdu_ref = htobe16(pdu_ref); - } + void S7commLayer::setPduRef(uint16_t pdu_ref) const { getS7commHeader()->pdu_ref = htobe16(pdu_ref); } - void S7commLayer::setDataLength(uint16_t data_length) const { + void S7commLayer::setDataLength(uint16_t data_length) const + { getS7commHeader()->data_length = htobe16(data_length); } } // namespace pcpp diff --git a/Tests/Packet++Test/Tests/S7commTests.cpp b/Tests/Packet++Test/Tests/S7commTests.cpp index 8f6da9bfe7..352ef9f17b 100644 --- a/Tests/Packet++Test/Tests/S7commTests.cpp +++ b/Tests/Packet++Test/Tests/S7commTests.cpp @@ -22,6 +22,7 @@ PTF_TEST_CASE(S7commLayerTest) { PTF_ASSERT_EQUAL(s7commLayer->getPduRef(), 0xfd0b); PTF_ASSERT_EQUAL(s7commLayer->getParamLength(), 12); PTF_ASSERT_EQUAL(s7commLayer->getDataLength(), 212); + PTF_ASSERT_EQUAL(s7commLayer->toString(), "S7comm Layer, msg_type: 7, pdu_ref: 64779, param_length: 12, data_length: 212"); pcpp::S7commLayer newS7commPacket(0x09, 0xfd0c, 13, 213);