More ARP tests

This commit is contained in:
Sijmen 2021-01-12 22:29:09 +01:00
parent a102c4d984
commit 755d025d3f
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
4 changed files with 87 additions and 17 deletions

View File

@ -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()) if (bufferSize < SerializedLength())
{ {

View File

@ -35,7 +35,7 @@ namespace Net::Arp
sizeof(senderIp) + sizeof(targetMac) + sizeof(targetIp); 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); size_t Deserialize(const uint8_t* buffer, const size_t bufferSize);
}; };
@ -46,20 +46,20 @@ namespace Net::Arp
const Operation operation, const Operation operation,
const Utils::MacAddress targetMac, const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac, const Utils::MacAddress senderMac,
const uint32_t senderIp, const uint32_t targetIp,
const uint32_t targetIp); const uint32_t senderIp);
void SendRequest( void SendRequest(
const Utils::MacAddress targetMac, const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac, const Utils::MacAddress senderMac,
const uint32_t senderIp, const uint32_t targetIp,
const uint32_t targetIp); const uint32_t senderIp);
void SendReply( void SendReply(
const Utils::MacAddress targetMac, const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac, 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); void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip);

View File

@ -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() 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(); uint8_t buffer[expectedSize];
auto packet = Packet(ARP_OPERATION_REQUEST);
uint8_t buffer[size];
packet.Serialize(buffer, sizeof(buffer)); packet.Serialize(buffer, sizeof(buffer));
Packet deserialized; Arp::Packet deserialized;
deserialized.Deserialize(buffer, sizeof(buffer)); const auto size = deserialized.Deserialize(buffer, sizeof(buffer));
TEST_CHECK(size == expectedSize);
TEST_CHECK(packet.hardwareType == deserialized.hardwareType); TEST_CHECK(packet.hardwareType == deserialized.hardwareType);
TEST_CHECK(packet.protocolType == deserialized.protocolType); TEST_CHECK(packet.protocolType == deserialized.protocolType);
@ -21,3 +51,27 @@ void TestNetArpPacket()
TEST_CHECK(packet.targetMac == deserialized.targetMac); TEST_CHECK(packet.targetMac == deserialized.targetMac);
TEST_CHECK(packet.targetIp == deserialized.targetIp); 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);
}

View File

@ -1,8 +1,20 @@
#include "acutest.h" #include "acutest.h"
#include <cassert>
#include <cstring> #include <cstring>
#include <src/net-arp.h> #include <src/types.h>
#include <uspi.h>
extern "C" void USPiSendFrame(const void* pBuffer, unsigned nLength) {} #include <src/net-arp.h>
#include <src/net-ipv4.h>
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]) extern "C" void USPiGetMACAddress(unsigned char Buffer[6])
{ {
@ -20,5 +32,9 @@ TEST_LIST = {
{"Net::Utils::Crc32", TestNetUtilsCrc32}, {"Net::Utils::Crc32", TestNetUtilsCrc32},
{"Net::Utils::GetMacAddress", TestNetUtilsGetMacAddress}, {"Net::Utils::GetMacAddress", TestNetUtilsGetMacAddress},
{"Net::Arp::Packet", TestNetArpPacket}, {"Net::Arp::Packet", TestNetArpPacket},
{"Net::Arp::SendPacket", TestNetArpSendPacket},
{"Net::Arp::SendRequest", TestNetArpSendRequest},
{"Net::Arp::SendReply", TestNetArpSendReply},
{"Net::Arp::SendAnnouncement", TestNetArpSendAnnouncement},
{nullptr, nullptr}, {nullptr, nullptr},
}; };