From 982a6561f5e587c4491f7ad33e16f78c6e56fc98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Thu, 21 Sep 2023 13:20:32 +0300 Subject: [PATCH] Add test files (WIP) --- Common++/header/Logger.h | 1 + Packet++/header/SmtpLayer.h | 22 ++++++- Packet++/src/TcpLayer.cpp | 5 ++ Tests/Packet++Test/PacketExamples/Smtp.pcap | Bin 0 -> 586 bytes .../PacketExamples/smtpCommand.dat | 1 + .../Packet++Test/PacketExamples/smtpIpv6.dat | 1 + .../PacketExamples/smtpMultiLine.dat | 1 + .../PacketExamples/smtpResponse.dat | 1 + Tests/Packet++Test/TestDefinition.h | 7 ++- Tests/Packet++Test/Tests/SmtpTests.cpp | 54 ++++++++++++++++++ Tests/Packet++Test/main.cpp | 4 ++ 11 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 Tests/Packet++Test/PacketExamples/Smtp.pcap create mode 100644 Tests/Packet++Test/PacketExamples/smtpCommand.dat create mode 100644 Tests/Packet++Test/PacketExamples/smtpIpv6.dat create mode 100644 Tests/Packet++Test/PacketExamples/smtpMultiLine.dat create mode 100644 Tests/Packet++Test/PacketExamples/smtpResponse.dat diff --git a/Common++/header/Logger.h b/Common++/header/Logger.h index 425a55d092..cb3c888d89 100644 --- a/Common++/header/Logger.h +++ b/Common++/header/Logger.h @@ -73,6 +73,7 @@ namespace pcpp PacketLogModuleSomeIpLayer, ///< SomeIpLayer module (Packet++) PacketLogModuleSomeIpSdLayer, ///< SomeIpSdLayer module (Packet++) PacketLogModuleWakeOnLanLayer, ///< WakeOnLanLayer module (Packet++) + PacketLogModuleSmtpLayer, ///< SmtpLayer module (Packet++) PcapLogModuleWinPcapLiveDevice, ///< WinPcapLiveDevice module (Pcap++) PcapLogModuleRemoteDevice, ///< WinPcapRemoteDevice module (Pcap++) PcapLogModuleLiveDevice, ///< PcapLiveDevice module (Pcap++) diff --git a/Packet++/header/SmtpLayer.h b/Packet++/header/SmtpLayer.h index 02823094ac..fc51d61e6d 100644 --- a/Packet++/header/SmtpLayer.h +++ b/Packet++/header/SmtpLayer.h @@ -95,7 +95,27 @@ namespace pcpp /// Send mail to terminal or to mailbox SOML = ('S') | ('O' << 8) | ('M' << 16) | ('L' << 24), /// Send mail to terminal and mailbox - SAML = ('S') | ('A' << 8) | ('M' << 16) | ('L' << 24) + SAML = ('S') | ('A' << 8) | ('M' << 16) | ('L' << 24), + /// + TLS = ('T' << 8) | ('L' << 16) | ('S' << 24), + /// Start TLS handshake + STLS = ('S') | ('T' << 8) | ('L' << 16) | ('S' << 24), + /// Authenticate client and server + AUTH = ('A') | ('U' << 8) | ('T' << 16) | ('H' << 24), + /// Reverse the role of sender and receiver + ATRN = ('A') | ('T' << 8) | ('R' << 16) | ('N' << 24), + /// Submit mail contents + BDAT = ('B') | ('D' << 8) | ('A' << 16) | ('T' << 24), + /// Request to start SMTP queue processing + ETRN = ('E') | ('T' << 8) | ('R' << 16) | ('N' << 24), + /// Release status of the channel + XADR = ('X') | ('A' << 8) | ('D' << 16) | ('R' << 24), + /// Release status of the circuit checking facility + XCIR = ('X') | ('C' << 8) | ('I' << 16) | ('R' << 24), + /// Release status of the number of messages in channel queues + XSTA = ('X') | ('S' << 8) | ('T' << 16) | ('A' << 24), + /// Release status of whether a compiled configuration and character set are in use + XGEN = ('X') | ('G' << 8) | ('E' << 16) | ('N' << 24), }; /** A constructor that creates the layer from an existing packet raw data diff --git a/Packet++/src/TcpLayer.cpp b/Packet++/src/TcpLayer.cpp index 1b51207288..5782b9d3f0 100644 --- a/Packet++/src/TcpLayer.cpp +++ b/Packet++/src/TcpLayer.cpp @@ -15,6 +15,7 @@ #include "TpktLayer.h" #include "FtpLayer.h" #include "SomeIpLayer.h" +#include "SmtpLayer.h" #include "PacketUtils.h" #include "Logger.h" #include @@ -380,6 +381,10 @@ void TcpLayer::parseNextLayer() m_NextLayer = SomeIpLayer::parseSomeIpLayer(payload, payloadLen, this, m_Packet); else if (TpktLayer::isDataValid(payload, payloadLen) && TpktLayer::isTpktPort(portSrc, portDst)) m_NextLayer = new TpktLayer(payload, payloadLen, this, m_Packet); + else if (SmtpLayer::isSmtpPort(portSrc) && SmtpLayer::isDataValid(payload, payloadLen)) + m_NextLayer = new SmtpResponseLayer(payload, payloadLen, this, m_Packet); + else if (SmtpLayer::isSmtpPort(portDst) && SmtpLayer::isDataValid(payload, payloadLen)) + m_NextLayer = new SmtpRequestLayer(payload, payloadLen, this, m_Packet); else m_NextLayer = new PayloadLayer(payload, payloadLen, this, m_Packet); } diff --git a/Tests/Packet++Test/PacketExamples/Smtp.pcap b/Tests/Packet++Test/PacketExamples/Smtp.pcap new file mode 100644 index 0000000000000000000000000000000000000000..1333b7cee27c5c7dc2fc465586ca2648116cb949 GIT binary patch literal 586 zcmca|c+)~A1{MYcU}0bcaxV0o^x}_VW_S%`gD}Gb85{9K4D!Y|8xuGfToV}XDn4>x zFk*B5>t)(AlZ%Uyg+Y>a-?}%68|sb(Zw!zSyU^vrz+hx#pj(k_X`ol0npB)wl3Jdc zlboNMss|(#T!Vc>0u)>;GIJG7^vo<3lnoVp^Ye5R3``aLlS>qg3=Aw43=FLdOsz~! z6m$(t4Ga``xj+Vnrz)i6E9B*uC?uAaWaJlRR;4PGWTYyT7N;uYrvaIn#R|oh#U-h^ z3MKgpB}IvO#Rd6AB?_f^#rZjz$v|hM=s=88NX$#o&o5F)D$U7ONY%|v%*@f_o-j%wS+} z3=Q#6@bP!|^n;lA@)<9K2hcDO#t2L+#iyXabeW49m`U$|rXB+YX0i_hgR!xRLRf@f zPI9hkMpAgF884TX@WhadwTuk4KqEmoWATxnp5H=BFP!9VY`dEP6k%{@vruFIvB80d zhZ)2L0x1PXmV&4I@2&L=S1fs<jyDd;Ms7w47~@NxkFozbzT literal 0 HcmV?d00001 diff --git a/Tests/Packet++Test/PacketExamples/smtpCommand.dat b/Tests/Packet++Test/PacketExamples/smtpCommand.dat new file mode 100644 index 0000000000..b3c204691a --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/smtpCommand.dat @@ -0,0 +1 @@ +001f33d9816000e01c3c17c2080045000034252440008006f3c30a0a01044a358c9905be00197ec453baaeec62ee5018fec18968000041555448204c4f47494e0d0a \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/smtpIpv6.dat b/Tests/Packet++Test/PacketExamples/smtpIpv6.dat new file mode 100644 index 0000000000..17b909f83a --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/smtpIpv6.dat @@ -0,0 +1 @@ +68a3c4f949f65475d0c90b8186dd60000000004706382607f8b0400c0c03000000000000001a20010470e5bfdead49572174e82c48870019f9c79563979d03a0883150180150e6870000323230206d782e676f6f676c652e636f6d2045534d5450206d313773693130353135393376636b2e32202d2067736d74700d0a \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/smtpMultiLine.dat b/Tests/Packet++Test/PacketExamples/smtpMultiLine.dat new file mode 100644 index 0000000000..40d026af6b --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/smtpMultiLine.dat @@ -0,0 +1 @@ +00e01c3c17c2001f33d981600800456000dd21e24000320643fd4a358c990a0a0104001905beaeec61b07ec453b1501816d08a4400003232302d786339302e7765627369746577656c636f6d652e636f6d2045534d5450204578696d20342e3639202331204d6f6e2c203035204f637420323030392030313a30353a3534202d30353030200d0a3232302d576520646f206e6f7420617574686f72697a652074686520757365206f6620746869732073797374656d20746f207472616e73706f727420756e736f6c6963697465642c200d0a32323020616e642f6f722062756c6b20652d6d61696c2e0d0a \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/smtpResponse.dat b/Tests/Packet++Test/PacketExamples/smtpResponse.dat new file mode 100644 index 0000000000..d5fbb51bfb --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/smtpResponse.dat @@ -0,0 +1 @@ +00e01c3c17c2001f33d9816008004560003a21e540003206449d4a358c990a0a0104001905beaeec62ee7ec453c6501816d0634c00003333342056584e6c636d3568625755360d0a \ No newline at end of file diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index d4c1ee8571..eb7ff52178 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -241,5 +241,10 @@ PTF_TEST_CASE(TpktLayerTest); PTF_TEST_CASE(VrrpParsingTest); PTF_TEST_CASE(VrrpCreateAndEditTest); -//Implemented in CotpTests.cpp +// Implemented in CotpTests.cpp PTF_TEST_CASE(CotpLayerTest); + +// Implemented in SmtpTests.cpp +PTF_TEST_CASE(SmtpParsingTests); +PTF_TEST_CASE(SmtpCreationTests); +PTF_TEST_CASE(SmtpEditTests); diff --git a/Tests/Packet++Test/Tests/SmtpTests.cpp b/Tests/Packet++Test/Tests/SmtpTests.cpp index 7dc27e835e..89031bd31b 100644 --- a/Tests/Packet++Test/Tests/SmtpTests.cpp +++ b/Tests/Packet++Test/Tests/SmtpTests.cpp @@ -13,16 +13,70 @@ PTF_TEST_CASE(SmtpParsingTests) { timeval time; gettimeofday(&time, nullptr); + + // Command + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/smtpCommand.dat"); + + pcpp::Packet smtpPacket1(&rawPacket1); + pcpp::SmtpRequestLayer *smtpLayer1 = smtpPacket1.getLayerOfType(); + + PTF_ASSERT_NOT_NULL(smtpLayer1); + PTF_ASSERT_EQUAL(int(smtpLayer1->getCommand()), int(pcpp::SmtpRequestLayer::SmtpCommand::AUTH)); + PTF_ASSERT_EQUAL(smtpLayer1->getCommandString(), "AUTH"); + PTF_ASSERT_EQUAL(smtpLayer1->getCommandOption(), "LOGIN"); + PTF_ASSERT_EQUAL(smtpLayer1->toString(), "SMTP Request: AUTH"); + PTF_ASSERT_FALSE(smtpLayer1->isMultiLine()); + + // PTF_ASSERT_EQUAL(pcpp::SmtpRequestLayer::getCommandInfo(pcpp::SmtpRequestLayer::SmtpCommand::AUTH), + // "Authenticate client and server"); + PTF_ASSERT_EQUAL(pcpp::SmtpRequestLayer::getCommandAsString(pcpp::SmtpRequestLayer::SmtpCommand::AUTH), "AUTH"); + + // Response packet + READ_FILE_AND_CREATE_PACKET(2, "PacketExamples/smtpResponse.dat"); + + pcpp::Packet smtpPacket2(&rawPacket2); + pcpp::SmtpResponseLayer *smtpLayer2 = smtpPacket2.getLayerOfType(); + + PTF_ASSERT_NOT_NULL(smtpLayer2); + PTF_ASSERT_EQUAL(int(smtpLayer2->getStatusCode()), int(pcpp::SmtpResponseLayer::SmtpStatusCode::SERVER_CHALLENGE)); + PTF_ASSERT_EQUAL(smtpLayer2->getStatusCodeString(), "334"); + PTF_ASSERT_EQUAL(smtpLayer2->getStatusOption(), "VXNlcm5hbWU6"); + PTF_ASSERT_EQUAL(smtpLayer2->toString(), "SMTP Response: 334"); + PTF_ASSERT_FALSE(smtpLayer2->isMultiLine()); + + PTF_ASSERT_EQUAL( + pcpp::SmtpResponseLayer::getStatusCodeAsString(pcpp::SmtpResponseLayer::SmtpStatusCode::SERVER_CHALLENGE), + "Server challenge"); + + // Multiline + + // IPv6 } PTF_TEST_CASE(SmtpCreationTests) { timeval time; gettimeofday(&time, nullptr); + + // Request + + // Response packet + + // Multiline + + // IPv6 } PTF_TEST_CASE(SmtpEditTests) { timeval time; gettimeofday(&time, nullptr); + + // Request + + // Response packet + + // Multiline + + // IPv6 } diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index 731bf31fdd..53da2e5b56 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -316,5 +316,9 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(CotpLayerTest, "cotp"); + PTF_RUN_TEST(SmtpParsingTests, "smtp"); + PTF_RUN_TEST(SmtpCreationTests, "smtp"); + PTF_RUN_TEST(SmtpEditTests, "smtp"); + PTF_END_RUNNING_TESTS; }