diff --git a/tests/Makefile b/tests/Makefile index 7ea880c..856a7af 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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)" diff --git a/tests/common.h b/tests/common.h new file mode 100644 index 0000000..a30f49d --- /dev/null +++ b/tests/common.h @@ -0,0 +1,2 @@ +#pragma once +extern uint8_t uspiBuffer[USPI_FRAME_BUFFER_SIZE]; diff --git a/tests/net-arp.cpp b/tests/net-arp.cpp new file mode 100644 index 0000000..39be104 --- /dev/null +++ b/tests/net-arp.cpp @@ -0,0 +1,91 @@ +#define TEST_NO_MAIN +#include "acutest.h" + +#include +#include +#include + +#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); +} diff --git a/tests/net-arp.h b/tests/net-arp.h index f9d35d1..95eefea 100644 --- a/tests/net-arp.h +++ b/tests/net-arp.h @@ -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(); diff --git a/tests/net-utils.cpp b/tests/net-utils.cpp new file mode 100644 index 0000000..bbc4762 --- /dev/null +++ b/tests/net-utils.cpp @@ -0,0 +1,49 @@ +#define TEST_NO_MAIN +#include "acutest.h" + +#include + +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); +} diff --git a/tests/net-utils.h b/tests/net-utils.h index 9664258..ff05131 100644 --- a/tests/net-utils.h +++ b/tests/net-utils.h @@ -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(); diff --git a/tests/tests.cpp b/tests/test.cpp similarity index 100% rename from tests/tests.cpp rename to tests/test.cpp