Reorganize tests into seperate .cpp files

This commit is contained in:
Sijmen 2021-01-13 12:14:18 +01:00
parent 755d025d3f
commit 81e426fef4
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
7 changed files with 153 additions and 121 deletions

View File

@ -2,7 +2,7 @@ SRCDIR = ../src
TESTDIR = .
SRCOBJS := net-utils.o net-arp.o net-ethernet.o
TESTOBJS := tests.o
TESTOBJS := test.o net-arp.o net-utils.o
OBJS := $(addprefix $(TESTDIR)/, $(TESTOBJS)) $(addprefix $(SRCDIR)/, $(SRCOBJS))
INCLUDE = -I../uspi/include/ -I..
@ -10,7 +10,6 @@ INCLUDE = -I../uspi/include/ -I..
CC := clang
CXX := clang++
AFLAGS += $(ARCH)
CFLAGS += $(ARCH) $(INCLUDE) -MMD -MP -Wall -Wno-psabi -fsigned-char -fno-builtin -g -DNDEBUG
CXXFLAGS := $(CFLAGS) $(CXXFLAGS) -std=c++11
CFLAGS += -fno-delete-null-pointer-checks -fdata-sections -ffunction-sections -u _printf_float -std=gnu99
@ -23,7 +22,6 @@ DEPENDS := $(patsubst %.o,%.d,$(OBJS))
.PHONY: all
all: $(TARGET)
./$(TARGET)
$(TARGET): $(OBJS)
@echo "$(OBJS)"

2
tests/common.h Normal file
View File

@ -0,0 +1,2 @@
#pragma once
extern uint8_t uspiBuffer[USPI_FRAME_BUFFER_SIZE];

91
tests/net-arp.cpp Normal file
View File

@ -0,0 +1,91 @@
#define TEST_NO_MAIN
#include "acutest.h"
#include <src/net-arp.h>
#include <src/types.h>
#include <uspi.h>
#include "common.h"
#include "net-arp.h"
using namespace Net;
static const auto targetMac = Utils::MacAddress{1, 2, 3, 4, 5, 6};
static const auto senderMac = Utils::MacAddress{11, 12, 13, 14, 15, 16};
static const uint32_t targetIp = 0xCAFE0000;
static const uint32_t senderIp = 0x00C0FFEE;
static void netArpCheckSentPacket(
const uint16_t operation, const Utils::MacAddress targetMac, const uint32_t targetIp)
{
// Check if SendPacket set the ethernet header correctly
Ethernet::Header ethernetHeader;
auto size = Ethernet::Header::Deserialize(ethernetHeader, uspiBuffer, USPI_FRAME_BUFFER_SIZE);
TEST_CHECK(size == Ethernet::Header::SerializedLength());
TEST_CHECK(ethernetHeader.macSource == senderMac);
TEST_CHECK(ethernetHeader.macDestination == targetMac);
// Check if the ARP packet fields were set correctly.
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()
{
constexpr auto expectedSize = Arp::Packet::SerializedLength();
const auto packet = Arp::Packet(Arp::ARP_OPERATION_REQUEST);
uint8_t buffer[expectedSize];
packet.Serialize(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);
TEST_CHECK(packet.hardwareAddressLength == deserialized.hardwareAddressLength);
TEST_CHECK(packet.protocolAddressLength == deserialized.protocolAddressLength);
TEST_CHECK(packet.operation == deserialized.operation);
TEST_CHECK(packet.senderMac == deserialized.senderMac);
TEST_CHECK(packet.senderIp == deserialized.senderIp);
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);
}

View File

@ -1,77 +1,7 @@
using namespace Net;
#pragma once
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()
{
constexpr auto expectedSize = Arp::Packet::SerializedLength();
const auto packet = Arp::Packet(Arp::ARP_OPERATION_REQUEST);
uint8_t buffer[expectedSize];
packet.Serialize(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);
TEST_CHECK(packet.hardwareAddressLength == deserialized.hardwareAddressLength);
TEST_CHECK(packet.protocolAddressLength == deserialized.protocolAddressLength);
TEST_CHECK(packet.operation == deserialized.operation);
TEST_CHECK(packet.senderMac == deserialized.senderMac);
TEST_CHECK(packet.senderIp == deserialized.senderIp);
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);
}
void TestNetArpPacket();
void TestNetArpSendPacket();
void TestNetArpSendRequest();
void TestNetArpSendReply();
void TestNetArpSendAnnouncement();

49
tests/net-utils.cpp Normal file
View File

@ -0,0 +1,49 @@
#define TEST_NO_MAIN
#include "acutest.h"
#include <src/net-utils.h>
using namespace Net;
void TestNetUtilsInternetChecksum()
{
{
const char buffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 61419);
}
{
const char buffer[] = {1, 2, 3, 4, 5, 6, 7};
uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 61427);
}
{
const char buffer[] = {};
uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 65535);
}
}
void TestNetUtilsCrc32()
{
{
const uint8_t buffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint32_t checksum = Utils::Crc32(buffer, sizeof(buffer));
TEST_CHECK(checksum == 1070237893);
}
{
const uint8_t buffer[] = {};
uint32_t checksum = Utils::Crc32(buffer, sizeof(buffer));
TEST_CHECK(checksum == 0);
}
}
void TestNetUtilsGetMacAddress()
{
auto mac = Utils::GetMacAddress();
auto expected = Utils::MacAddress{0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
TEST_CHECK(mac == expected);
}

View File

@ -1,42 +1,4 @@
void TestNetUtilsInternetChecksum()
{
{
const char buffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint16_t checksum = Net::Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 61419);
}
{
const char buffer[] = {1, 2, 3, 4, 5, 6, 7};
uint16_t checksum = Net::Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 61427);
}
{
const char buffer[] = {};
uint16_t checksum = Net::Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 65535);
}
}
void TestNetUtilsCrc32()
{
{
const uint8_t buffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint32_t checksum = Net::Utils::Crc32(buffer, sizeof(buffer));
TEST_CHECK(checksum == 1070237893);
}
{
const uint8_t buffer[] = {};
uint32_t checksum = Net::Utils::Crc32(buffer, sizeof(buffer));
TEST_CHECK(checksum == 0);
}
}
void TestNetUtilsGetMacAddress()
{
auto mac = Net::Utils::GetMacAddress();
auto expected = Net::Utils::MacAddress{0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
TEST_CHECK(mac == expected);
}
#pragma once
void TestNetUtilsInternetChecksum();
void TestNetUtilsCrc32();
void TestNetUtilsGetMacAddress();