More ARP tests
This commit is contained in:
parent
a102c4d984
commit
755d025d3f
4 changed files with 87 additions and 17 deletions
|
@ -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())
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,20 @@
|
|||
#include "acutest.h"
|
||||
#include <cassert>
|
||||
#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])
|
||||
{
|
||||
|
@ -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},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue