Implement first few tests and fix issues with tested files
This commit is contained in:
parent
81ea9e27ca
commit
a102c4d984
11 changed files with 1939 additions and 26 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -6,4 +6,5 @@
|
||||||
*.lst
|
*.lst
|
||||||
*.map
|
*.map
|
||||||
*.swp
|
*.swp
|
||||||
|
test
|
||||||
|
kernel
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -28,7 +28,7 @@ uspi/libuspi.a:
|
||||||
$(MAKE) -C uspi
|
$(MAKE) -C uspi
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(Q)$(RM) $(OBJS) $(TARGET).elf $(TARGET).map $(TARGET).lst $(TARGET).img
|
$(Q)$(RM) $(OBJS) $(DEPENDS) $(TARGET).elf $(TARGET).map $(TARGET).lst $(TARGET).img
|
||||||
$(MAKE) -C uspi clean
|
$(MAKE) -C uspi clean
|
||||||
|
|
||||||
include Makefile.rules
|
include Makefile.rules
|
||||||
|
|
|
@ -28,11 +28,11 @@ namespace Net::Arp
|
||||||
Packet();
|
Packet();
|
||||||
Packet(const uint16_t operation);
|
Packet(const uint16_t operation);
|
||||||
|
|
||||||
constexpr size_t SerializedLength() const
|
constexpr static size_t SerializedLength()
|
||||||
{
|
{
|
||||||
return sizeof(hardwareType) + sizeof(protocolType) + sizeof(hardwareAddressLength) +
|
return sizeof(hardwareType) + sizeof(protocolType) + sizeof(hardwareAddressLength) +
|
||||||
sizeof(protocolAddressLength) + sizeof(operation) + senderMac.size() +
|
sizeof(protocolAddressLength) + sizeof(operation) + sizeof(senderMac) +
|
||||||
sizeof(senderIp) + targetMac.size() + 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);
|
||||||
|
|
|
@ -29,14 +29,14 @@ namespace Net::Icmp
|
||||||
buffer[i++] = static_cast<uint8_t>(type);
|
buffer[i++] = static_cast<uint8_t>(type);
|
||||||
buffer[i++] = code;
|
buffer[i++] = code;
|
||||||
buffer[i++] = 0;
|
buffer[i++] = 0;
|
||||||
buffer[i++] = 0 >> 8;
|
buffer[i++] = 0;
|
||||||
|
|
||||||
std::memcpy(buffer + i, data, dataSize);
|
std::memcpy(buffer + i, data, dataSize);
|
||||||
i += dataSize;
|
i += dataSize;
|
||||||
|
|
||||||
uint16_t checksum = Utils::InternetChecksum(buffer, i);
|
uint16_t checksum = Utils::InternetChecksum(buffer, i);
|
||||||
buffer[2] = checksum;
|
buffer[2] = checksum >> 8;
|
||||||
buffer[3] = checksum >> 8;
|
buffer[3] = checksum;
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ namespace Net::Ipv4
|
||||||
|
|
||||||
// Zero the checksum before calculating it
|
// Zero the checksum before calculating it
|
||||||
buffer[i++] = 0;
|
buffer[i++] = 0;
|
||||||
buffer[i++] = 0 >> 8;
|
buffer[i++] = 0;
|
||||||
|
|
||||||
buffer[i++] = sourceIp >> 24;
|
buffer[i++] = sourceIp >> 24;
|
||||||
buffer[i++] = sourceIp >> 16;
|
buffer[i++] = sourceIp >> 16;
|
||||||
|
@ -68,8 +68,8 @@ namespace Net::Ipv4
|
||||||
buffer[i++] = destinationIp;
|
buffer[i++] = destinationIp;
|
||||||
|
|
||||||
uint16_t checksum = Net::Utils::InternetChecksum(buffer, i);
|
uint16_t checksum = Net::Utils::InternetChecksum(buffer, i);
|
||||||
buffer[10] = checksum;
|
buffer[10] = checksum >> 8;
|
||||||
buffer[11] = checksum >> 8;
|
buffer[11] = checksum;
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,24 +68,19 @@ namespace Net::Utils
|
||||||
|
|
||||||
uint16_t InternetChecksum(const void* data, size_t size)
|
uint16_t InternetChecksum(const void* data, size_t size)
|
||||||
{
|
{
|
||||||
|
if (size == 0)
|
||||||
|
return ~0;
|
||||||
|
|
||||||
|
auto u8data = static_cast<const uint8_t*>(data);
|
||||||
|
|
||||||
uint32_t sum = 0;
|
uint32_t sum = 0;
|
||||||
while (size > 1)
|
for (size_t i = 0; i < size - 1; i += 2)
|
||||||
{
|
sum += u8data[i] << 8 | u8data[i + 1];
|
||||||
sum += *(uint16_t*)data;
|
|
||||||
data = (uint16_t*)data + 1;
|
|
||||||
|
|
||||||
size -= 2;
|
if (size % 2 == 1)
|
||||||
}
|
sum += u8data[size - 1] << 8;
|
||||||
|
|
||||||
if (size > 0)
|
sum = (sum >> 16) + (sum & 0xFFFF);
|
||||||
{
|
|
||||||
sum += *(uint16_t*)data;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (sum >> 16)
|
|
||||||
{
|
|
||||||
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ~sum;
|
return ~sum;
|
||||||
}
|
}
|
||||||
|
|
34
tests/Makefile
Normal file
34
tests/Makefile
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
SRCDIR = ../src
|
||||||
|
TESTDIR = .
|
||||||
|
|
||||||
|
SRCOBJS := net-utils.o net-arp.o net-ethernet.o
|
||||||
|
TESTOBJS := tests.o
|
||||||
|
OBJS := $(addprefix $(TESTDIR)/, $(TESTOBJS)) $(addprefix $(SRCDIR)/, $(SRCOBJS))
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
TARGET ?= test
|
||||||
|
|
||||||
|
DEPENDS := $(patsubst %.o,%.d,$(OBJS))
|
||||||
|
-include $(DEPENDS)
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
@echo "$(OBJS)"
|
||||||
|
@echo " LINK $@"
|
||||||
|
$(Q)$(CXX) $(CFLAGS) -o $(TARGET) $(OBJS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(Q)$(RM) $(SRCDIR)/*.o $(TESTDIR)/*.o $(SRCDIR)/*.d $(TESTDIR)/*.d $(TARGET) kernel
|
1794
tests/acutest.h
Normal file
1794
tests/acutest.h
Normal file
File diff suppressed because it is too large
Load diff
23
tests/net-arp.h
Normal file
23
tests/net-arp.h
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
void TestNetArpPacket()
|
||||||
|
{
|
||||||
|
using namespace Net::Arp;
|
||||||
|
|
||||||
|
constexpr auto size = Packet::SerializedLength();
|
||||||
|
auto packet = Packet(ARP_OPERATION_REQUEST);
|
||||||
|
|
||||||
|
uint8_t buffer[size];
|
||||||
|
packet.Serialize(buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
Packet deserialized;
|
||||||
|
deserialized.Deserialize(buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
42
tests/net-utils.h
Normal file
42
tests/net-utils.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
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);
|
||||||
|
}
|
24
tests/tests.cpp
Normal file
24
tests/tests.cpp
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#include "acutest.h"
|
||||||
|
#include <cstring>
|
||||||
|
#include <src/net-arp.h>
|
||||||
|
|
||||||
|
extern "C" void USPiSendFrame(const void* pBuffer, unsigned nLength) {}
|
||||||
|
|
||||||
|
extern "C" void USPiGetMACAddress(unsigned char Buffer[6])
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
Buffer[i] = i << 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "net-arp.h"
|
||||||
|
#include "net-utils.h"
|
||||||
|
|
||||||
|
TEST_LIST = {
|
||||||
|
{"Net::Utils::InternetChecksum", TestNetUtilsInternetChecksum},
|
||||||
|
{"Net::Utils::Crc32", TestNetUtilsCrc32},
|
||||||
|
{"Net::Utils::GetMacAddress", TestNetUtilsGetMacAddress},
|
||||||
|
{"Net::Arp::Packet", TestNetArpPacket},
|
||||||
|
{nullptr, nullptr},
|
||||||
|
};
|
Loading…
Reference in a new issue