Skip to content

Commit

Permalink
renaming, small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wivien19 committed Oct 24, 2023
1 parent 2f01981 commit d73006e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 104 deletions.
10 changes: 2 additions & 8 deletions Packet++/header/S7CommLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace pcpp
*/
class S7CommLayer : public Layer
{
public:
public:
/**
* A constructor that allocates a new S7comm header
* @param[in] msgType The general type of the message
Expand Down Expand Up @@ -160,12 +160,6 @@ namespace pcpp
*/
void setPduRef(uint16_t pduRef) const;

/**
* Set the value of the parameter length
* @param[in] paramLength The value of the parameter length
*/
void setParamLength(uint16_t paramLength) const;

/**
* Set the value of the data length
* @param[in] dataLength The value of the data length
Expand Down Expand Up @@ -210,7 +204,7 @@ namespace pcpp

OsiModelLayer getOsiModelLayer() const override { return OsiModelApplicationLayer; }

private:
private:
s7commhdr *getS7commHeader() const { return (s7commhdr *)m_Data; }

s7comm_ack_data_hdr *getS7commAckDataHeader() const
Expand Down
8 changes: 2 additions & 6 deletions Packet++/src/S7CommLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ namespace pcpp
s7commHdr->dataLength = htobe16(dataLength);
}

m_Parameter = nullptr;
m_Protocol = S7COMM;
}

std::string S7CommLayer::toString() const
{
std::ostringstream str;

str << "S7Comm Layer, Job Request";
str << "S7Comm Layer";

return str.str();
}
Expand Down Expand Up @@ -81,11 +82,6 @@ namespace pcpp

uint8_t S7CommLayer::getErrorClass() const { return getS7commAckDataHeader()->errorClass; }

void S7CommLayer::setParamLength(uint16_t paramLength) const
{
getS7commHeader()->paramLength = htobe16(paramLength);
}

void S7CommLayer::setPduRef(uint16_t pduRef) const { getS7commHeader()->pduRef = htobe16(pduRef); }

void S7CommLayer::setDataLength(uint16_t dataLength) const { getS7commHeader()->dataLength = htobe16(dataLength); }
Expand Down
3 changes: 2 additions & 1 deletion Tests/Packet++Test/TestDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,5 @@ PTF_TEST_CASE(VrrpCreateAndEditTest);
PTF_TEST_CASE(CotpLayerTest);

// Implemented in S7commTests.cpp
PTF_TEST_CASE(S7CommLayerTest);
PTF_TEST_CASE(S7CommLayerParsingTest);
PTF_TEST_CASE(S7CommLayerCreationTest);
63 changes: 33 additions & 30 deletions Tests/Packet++Test/Tests/S7CommTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "S7CommLayer.h"
#include "SystemUtils.h"

PTF_TEST_CASE(S7CommLayerTest)
PTF_TEST_CASE(S7CommLayerParsingTest)
{
timeval time;
gettimeofday(&time, nullptr);
Expand All @@ -16,57 +16,60 @@ PTF_TEST_CASE(S7CommLayerTest)
auto *S7CommLayer = S7CommLayerTest.getLayerOfType<pcpp::S7CommLayer>();
PTF_ASSERT_NOT_NULL(S7CommLayer);

PTF_ASSERT_EQUAL(S7CommLayer->getProtocolId(), 0x32);
PTF_ASSERT_EQUAL(S7CommLayer->getMsgType(), 0x07);
PTF_ASSERT_EQUAL(S7CommLayer->getPduRef(), 0xfd0b);
PTF_ASSERT_EQUAL(S7CommLayer->getProtocolId(), 50);
PTF_ASSERT_EQUAL(S7CommLayer->getMsgType(), 7);
PTF_ASSERT_EQUAL(S7CommLayer->getPduRef(), 64779);
PTF_ASSERT_EQUAL(S7CommLayer->getParamLength(), 12);
PTF_ASSERT_EQUAL(S7CommLayer->getDataLength(), 212);
PTF_ASSERT_EQUAL(S7CommLayer->getHeaderLen(), 0xea);
PTF_ASSERT_EQUAL(S7CommLayer->toString(), "S7Comm Layer, Job Request");
PTF_ASSERT_EQUAL(S7CommLayer->getHeaderLen(), 234);
PTF_ASSERT_EQUAL(S7CommLayer->toString(), "S7Comm Layer");

PTF_ASSERT_EQUAL(S7CommLayer->getParameter()->getDataLength(), 12);
uint8_t expectedParameterData[] = {0x00, 0x01, 0x12, 0x08, 0x12, 0x84, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
uint8_t expectedParameterData[] = {0, 1, 18, 8, 18, 132, 1, 1, 0, 0, 0, 0};
PTF_ASSERT_BUF_COMPARE(S7CommLayer->getParameter()->getData(), expectedParameterData, 12);
} // S7CommLayerParsingTest

pcpp::S7CommLayer newS7commPacket(0x09, 0xfd0c, 13, 213);
PTF_TEST_CASE(S7CommLayerCreationTest)
{
timeval time;
gettimeofday(&time, nullptr);
pcpp::S7CommLayer newS7commPacket(9, 64780, 13, 213);

PTF_ASSERT_EQUAL(newS7commPacket.getMsgType(), 0x09);
PTF_ASSERT_EQUAL(newS7commPacket.getPduRef(), 0xfd0c);
PTF_ASSERT_EQUAL(newS7commPacket.getMsgType(), 9);
PTF_ASSERT_EQUAL(newS7commPacket.getPduRef(), 64780);
PTF_ASSERT_EQUAL(newS7commPacket.getParamLength(), 13);
PTF_ASSERT_EQUAL(newS7commPacket.getDataLength(), 213);

newS7commPacket.setMsgType(0x06);
newS7commPacket.setPduRef(0xfd0a);
newS7commPacket.setParamLength(15);
newS7commPacket.setMsgType(6);
newS7commPacket.setPduRef(64778);
newS7commPacket.setDataLength(215);

PTF_ASSERT_EQUAL(newS7commPacket.getMsgType(), 0x06);
PTF_ASSERT_EQUAL(newS7commPacket.getPduRef(), 0xfd0a);
PTF_ASSERT_EQUAL(newS7commPacket.getParamLength(), 15);
PTF_ASSERT_EQUAL(newS7commPacket.getMsgType(), 6);
PTF_ASSERT_EQUAL(newS7commPacket.getPduRef(), 64778);
PTF_ASSERT_EQUAL(newS7commPacket.getDataLength(), 215);

READ_FILE_AND_CREATE_PACKET(2, "PacketExamples/s7comm_error_code.dat");
READ_FILE_AND_CREATE_PACKET(2, "PacketExamples/s7comm_ack_data.dat");

pcpp::Packet S7CommLayerErrorTest(&rawPacket2);
PTF_ASSERT_TRUE(S7CommLayerErrorTest.isPacketOfType(pcpp::S7COMM));
auto *s7commErrorLayer = S7CommLayerErrorTest.getLayerOfType<pcpp::S7CommLayer>();
PTF_ASSERT_NOT_NULL(s7commErrorLayer);
PTF_ASSERT_EQUAL(s7commErrorLayer->getProtocolId(), 0x32);
PTF_ASSERT_EQUAL(s7commErrorLayer->getMsgType(), 0x03);
PTF_ASSERT_EQUAL(s7commErrorLayer->getPduRef(), 0x0000);
PTF_ASSERT_EQUAL(s7commErrorLayer->getProtocolId(), 50);
PTF_ASSERT_EQUAL(s7commErrorLayer->getMsgType(), 3);
PTF_ASSERT_EQUAL(s7commErrorLayer->getPduRef(), 0);
PTF_ASSERT_EQUAL(s7commErrorLayer->getParamLength(), 2);
PTF_ASSERT_EQUAL(s7commErrorLayer->getDataLength(), 68);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorClass(), 0x00);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorCode(), 0x00);
PTF_ASSERT_EQUAL(s7commErrorLayer->getHeaderLen(), 0x52);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorClass(), 0);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorCode(), 0);
PTF_ASSERT_EQUAL(s7commErrorLayer->getHeaderLen(), 82);

PTF_ASSERT_EQUAL(s7commErrorLayer->toString(), "S7Comm Layer, Job Request");
PTF_ASSERT_EQUAL(s7commErrorLayer->toString(), "S7Comm Layer");

s7commErrorLayer->setErrorCode(0x06);
s7commErrorLayer->setErrorClass(0x07);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorClass(), 0x07);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorCode(), 0x06);
s7commErrorLayer->setErrorCode(6);
s7commErrorLayer->setErrorClass(7);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorClass(), 7);
PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorCode(), 6);
PTF_ASSERT_EQUAL(s7commErrorLayer->getParameter()->getDataLength(), 2);
uint8_t expectedErrorParameterData[] = {0x04, 0x01};
uint8_t expectedErrorParameterData[] = {4, 1};
PTF_ASSERT_BUF_COMPARE(s7commErrorLayer->getParameter()->getData(), expectedErrorParameterData, 2);
} // S7CommLayerTest
} // S7CommLayerCreationTest
121 changes: 62 additions & 59 deletions Tests/Packet++Test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,86 +1,88 @@
#include "../../Tests/Packet++Test/Utils/TestUtils.h"
#include "Logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "PcapPlusPlusVersion.h"
#include "PcppTestFrameworkRun.h"
#include "TestDefinition.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "Logger.h"
#include "../../Tests/Packet++Test/Utils/TestUtils.h"

static struct option PacketTestOptions[] = {{"include-tags", required_argument, nullptr, 't'},
{"exclude-tags", required_argument, nullptr, 'x'},
{"show-skipped-tests", no_argument, nullptr, 'w'},
{"mem-verbose", no_argument, nullptr, 'm'},
{"verbose", no_argument, nullptr, 'v'},
{"skip-mem-leak-check", no_argument, nullptr, 's'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}};
static struct option PacketTestOptions[] =
{
{"include-tags", required_argument, nullptr, 't'},
{"exclude-tags", required_argument, nullptr, 'x'},
{"show-skipped-tests", no_argument, nullptr, 'w' },
{"mem-verbose", no_argument, nullptr, 'm' },
{"verbose", no_argument, nullptr, 'v' },
{"skip-mem-leak-check", no_argument, nullptr, 's' },
{"help", no_argument, nullptr, 'h' },
{nullptr, 0, nullptr, 0}
};

void printUsage()
{
std::cout << "Usage: Packet++Test [-t tags] [-m] [-s] [-v] [-h]\n\n"
<< "Flags:\n"
<< "-t --include-tags A list of semicolon separated tags for tests to run\n"
<< "-x --exclude-tags A list of semicolon separated tags for tests to exclude\n"
<< "-w --show-skipped-tests Show tests that are skipped. Default is to hide them in tests results\n"
<< "-v --verbose Run in verbose mode (emits more output in several tests)\n"
<< "-m --mem-verbose Output information about each memory allocation and deallocation\n"
<< "-s --skip-mem-leak-check Skip memory leak check\n"
<< "-h --help Display this help message and exit\n";
<< "Flags:\n"
<< "-t --include-tags A list of semicolon separated tags for tests to run\n"
<< "-x --exclude-tags A list of semicolon separated tags for tests to exclude\n"
<< "-w --show-skipped-tests Show tests that are skipped. Default is to hide them in tests results\n"
<< "-v --verbose Run in verbose mode (emits more output in several tests)\n"
<< "-m --mem-verbose Output information about each memory allocation and deallocation\n"
<< "-s --skip-mem-leak-check Skip memory leak check\n"
<< "-h --help Display this help message and exit\n";
}

int main(int argc, char *argv[])

int main(int argc, char* argv[])
{
int optionIndex = 0;
int opt = 0;
std::string userTagsInclude = "", userTagsExclude = "", configTags = "";
bool memVerbose = false;
bool skipMemLeakCheck = false;

while ((opt = getopt_long(argc, argv, "msvwht:x:", PacketTestOptions, &optionIndex)) != -1)
while((opt = getopt_long(argc, argv, "msvwht:x:", PacketTestOptions, &optionIndex)) != -1)
{
switch (opt)
{
case 0:
break;
case 't':
userTagsInclude = optarg;
break;
case 'x':
userTagsExclude = optarg;
break;
case 's':
skipMemLeakCheck = true;
break;
case 'm':
memVerbose = true;
break;
case 'w':
PTF_SHOW_SKIPPED_TESTS(true);
break;
case 'v':
PTF_SET_VERBOSE_MODE(true);
break;
case 'h':
printUsage();
exit(0);
default:
printUsage();
exit(-1);
case 0:
break;
case 't':
userTagsInclude = optarg;
break;
case 'x':
userTagsExclude = optarg;
break;
case 's':
skipMemLeakCheck = true;
break;
case 'm':
memVerbose = true;
break;
case 'w':
PTF_SHOW_SKIPPED_TESTS(true);
break;
case 'v':
PTF_SET_VERBOSE_MODE(true);
break;
case 'h':
printUsage();
exit(0);
default:
printUsage();
exit(-1);
}
}

std::cout << "PcapPlusPlus version: " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;

#ifdef NDEBUG
#ifdef NDEBUG
skipMemLeakCheck = true;
std::cout << "Disabling memory leak check in MSVC Release builds due to caching logic in stream objects that looks "
"like a memory leak:"
<< std::endl
<< " https://github.com/cpputest/cpputest/issues/786#issuecomment-148921958" << std::endl;
#endif
std::cout << "Disabling memory leak check in MSVC Release builds due to caching logic in stream objects that looks like a memory leak:" << std::endl
<< " https://github.com/cpputest/cpputest/issues/786#issuecomment-148921958" << std::endl;
#endif

// The logger singleton looks like a memory leak. Invoke it before starting the memory check
pcpp::Logger::getInstance();
Expand Down Expand Up @@ -313,8 +315,9 @@ int main(int argc, char *argv[])
PTF_RUN_TEST(VrrpCreateAndEditTest, "vrrp");

PTF_RUN_TEST(CotpLayerTest, "cotp");

PTF_RUN_TEST(S7CommLayerTest, "s7comm");

PTF_RUN_TEST(S7CommLayerParsingTest, "s7comm");
PTF_RUN_TEST(S7CommLayerCreationTest, "s7comm");

PTF_END_RUNNING_TESTS;
}

0 comments on commit d73006e

Please sign in to comment.