From 755d025d3f81228aedc4cb601d04b7dd2a0719b1 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Tue, 12 Jan 2021 22:29:09 +0100 Subject: [PATCH] More ARP tests --- src/net-arp.cpp | 2 +- src/net-arp.h | 14 +++++----- tests/net-arp.h | 68 ++++++++++++++++++++++++++++++++++++++++++++----- tests/tests.cpp | 20 +++++++++++++-- 4 files changed, 87 insertions(+), 17 deletions(-) diff --git a/src/net-arp.cpp b/src/net-arp.cpp index 6edb8e4..18f0be5 100644 --- a/src/net-arp.cpp +++ b/src/net-arp.cpp @@ -21,7 +21,7 @@ namespace Net::Arp { } - size_t Packet::Serialize(uint8_t* buffer, const size_t bufferSize) + size_t Packet::Serialize(uint8_t* buffer, const size_t bufferSize) const { if (bufferSize < SerializedLength()) { diff --git a/src/net-arp.h b/src/net-arp.h index 4b07c74..eb1b1a5 100644 --- a/src/net-arp.h +++ b/src/net-arp.h @@ -35,7 +35,7 @@ namespace Net::Arp sizeof(senderIp) + sizeof(targetMac) + sizeof(targetIp); } - size_t Serialize(uint8_t* buffer, const size_t bufferSize); + size_t Serialize(uint8_t* buffer, const size_t bufferSize) const; size_t Deserialize(const uint8_t* buffer, const size_t bufferSize); }; @@ -46,20 +46,20 @@ namespace Net::Arp const Operation operation, const Utils::MacAddress targetMac, const Utils::MacAddress senderMac, - const uint32_t senderIp, - const uint32_t targetIp); + const uint32_t targetIp, + const uint32_t senderIp); void SendRequest( const Utils::MacAddress targetMac, const Utils::MacAddress senderMac, - const uint32_t senderIp, - const uint32_t targetIp); + const uint32_t targetIp, + const uint32_t senderIp); void SendReply( const Utils::MacAddress targetMac, const Utils::MacAddress senderMac, - const uint32_t senderIp, - const uint32_t targetIp); + const uint32_t targetIp, + const uint32_t senderIp); void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip); diff --git a/tests/net-arp.h b/tests/net-arp.h index 72d8a98..f9d35d1 100644 --- a/tests/net-arp.h +++ b/tests/net-arp.h @@ -1,15 +1,45 @@ +using namespace Net; + +const auto targetMac = Utils::MacAddress{1, 2, 3, 4, 5, 6}; +const auto senderMac = Utils::MacAddress{11, 12, 13, 14, 15, 16}; +const uint32_t targetIp = 0xCAFE0000; +const uint32_t senderIp = 0x00C0FFEE; + +static void netArpCheckSentPacket( + const uint16_t operation, const Utils::MacAddress targetMac, const uint32_t targetIp) +{ + Ethernet::Header ethernetHeader; + auto size = Ethernet::Header::Deserialize(ethernetHeader, uspiBuffer, USPI_FRAME_BUFFER_SIZE); + TEST_CHECK(ethernetHeader.macSource == senderMac); + TEST_CHECK(ethernetHeader.macDestination == targetMac); + + Arp::Packet packet; + size = packet.Deserialize( + uspiBuffer + Ethernet::Header::SerializedLength(), USPI_FRAME_BUFFER_SIZE - size); + TEST_CHECK(size == Arp::Packet::SerializedLength()); + + TEST_CHECK(packet.hardwareType == 1); + TEST_CHECK(packet.protocolType == Ethernet::EtherType::Ipv4); + TEST_CHECK(packet.hardwareAddressLength == 6); + TEST_CHECK(packet.protocolAddressLength == 4); + TEST_CHECK(packet.operation == operation); + TEST_CHECK(packet.senderMac == senderMac); + TEST_CHECK(packet.senderIp == senderIp); + TEST_CHECK(packet.targetMac == targetMac); + TEST_CHECK(packet.targetIp == targetIp); +} + void TestNetArpPacket() { - using namespace Net::Arp; + constexpr auto expectedSize = Arp::Packet::SerializedLength(); + const auto packet = Arp::Packet(Arp::ARP_OPERATION_REQUEST); - constexpr auto size = Packet::SerializedLength(); - auto packet = Packet(ARP_OPERATION_REQUEST); - - uint8_t buffer[size]; + uint8_t buffer[expectedSize]; packet.Serialize(buffer, sizeof(buffer)); - Packet deserialized; - deserialized.Deserialize(buffer, sizeof(buffer)); + Arp::Packet deserialized; + const auto size = deserialized.Deserialize(buffer, sizeof(buffer)); + TEST_CHECK(size == expectedSize); TEST_CHECK(packet.hardwareType == deserialized.hardwareType); TEST_CHECK(packet.protocolType == deserialized.protocolType); @@ -21,3 +51,27 @@ void TestNetArpPacket() TEST_CHECK(packet.targetMac == deserialized.targetMac); TEST_CHECK(packet.targetIp == deserialized.targetIp); } + +void TestNetArpSendPacket() +{ + Arp::SendPacket(Arp::ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp); + netArpCheckSentPacket(Arp::ARP_OPERATION_REQUEST, targetMac, targetIp); +} + +void TestNetArpSendRequest() +{ + Arp::SendRequest(targetMac, senderMac, targetIp, senderIp); + netArpCheckSentPacket(Arp::ARP_OPERATION_REQUEST, targetMac, targetIp); +} + +void TestNetArpSendReply() +{ + Arp::SendReply(targetMac, senderMac, targetIp, senderIp); + netArpCheckSentPacket(Arp::ARP_OPERATION_REPLY, targetMac, targetIp); +} + +void TestNetArpSendAnnouncement() +{ + Arp::SendAnnouncement(senderMac, senderIp); + netArpCheckSentPacket(Arp::ARP_OPERATION_REPLY, Utils::MacBroadcast, senderIp); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 6a328e6..a53999a 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,8 +1,20 @@ #include "acutest.h" +#include #include -#include +#include +#include -extern "C" void USPiSendFrame(const void* pBuffer, unsigned nLength) {} +#include +#include + +uint8_t uspiBuffer[USPI_FRAME_BUFFER_SIZE]; + +extern "C" int USPiSendFrame(const void* pBuffer, unsigned nLength) +{ + assert(nLength <= USPI_FRAME_BUFFER_SIZE); + memcpy(uspiBuffer, pBuffer, nLength); + return nLength; +} extern "C" void USPiGetMACAddress(unsigned char Buffer[6]) { @@ -20,5 +32,9 @@ TEST_LIST = { {"Net::Utils::Crc32", TestNetUtilsCrc32}, {"Net::Utils::GetMacAddress", TestNetUtilsGetMacAddress}, {"Net::Arp::Packet", TestNetArpPacket}, + {"Net::Arp::SendPacket", TestNetArpSendPacket}, + {"Net::Arp::SendRequest", TestNetArpSendRequest}, + {"Net::Arp::SendReply", TestNetArpSendReply}, + {"Net::Arp::SendAnnouncement", TestNetArpSendAnnouncement}, {nullptr, nullptr}, };