Final work on tests (for now)

This commit is contained in:
Sijmen 2021-01-26 01:25:35 +01:00
parent fd0652c623
commit 32dec9b4d8
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
6 changed files with 122 additions and 1 deletions

View file

@ -32,5 +32,14 @@ $(TARGET): $(OBJS)
@echo " LINK $@"
$(Q)$(CXX) $(CFLAGS) -o $(TARGET) $(OBJS)
generate-inputs: $(SRCOBJS) generate-inputs.o
@echo " LINK $@"
$(Q)$(CXX) $(CFLAGS) -o generate-inputs $(SRCOBJS) generate-inputs.o
./generate-inputs
afl: $(SRCOBJS) afl.o
@echo " LINK $@"
$(Q)afl-clang++ $(CFLAGS) -o afl $(SRCOBJS) afl.o
clean:
$(Q)$(RM) $(SRCDIR)/*.o $(TESTDIR)/*.o $(SRCDIR)/*.d $(TESTDIR)/*.d $(TARGET) kernel
$(Q)$(RM) $(SRCDIR)/*.o $(TESTDIR)/*.o $(SRCDIR)/*.d $(TESTDIR)/*.d $(TARGET) kernel test afl generate-inputs *.profraw *.profdata

49
tests/afl.cpp Normal file
View file

@ -0,0 +1,49 @@
#include <array>
#include <cassert>
#include <cstring>
#include <fstream>
#include <src/types.h>
#include <uspi.h>
static uint8_t uspiBuffer[USPI_FRAME_BUFFER_SIZE];
static size_t uspiBufferLength;
extern "C" int USPiEthernetAvailable(void)
{
return 1;
}
extern "C" int USPiReceiveFrame(void* pBuffer, unsigned* pResultLength)
{
memcpy(pBuffer, uspiBuffer, uspiBufferLength);
*pResultLength = uspiBufferLength;
return 1;
}
extern "C" int USPiSendFrame(const void* pBuffer, unsigned nLength)
{
assert(nLength <= USPI_FRAME_BUFFER_SIZE);
memcpy(uspiBuffer, pBuffer, nLength);
uspiBufferLength = nLength;
return nLength;
}
extern "C" void USPiGetMACAddress(unsigned char Buffer[6])
{
for (size_t i = 0; i < 6; i++)
Buffer[i] = i << 4;
}
extern "C" void MsDelay(unsigned) {}
#include <src/net-arp.h>
int main(int, char**)
{
fread(uspiBuffer, 1, sizeof(uspiBuffer), stdin);
Net::Ethernet::Header ethernetHeader;
Net::Arp::HandlePacket(ethernetHeader, uspiBuffer, sizeof(uspiBuffer));
return 0;
}

View file

@ -0,0 +1,41 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <src/net-arp.h>
#include <src/types.h>
#include <uspi.h>
using namespace Net;
extern "C" int USPiSendFrame(const void* pBuffer, unsigned nLength)
{
return std::min(nLength, static_cast<unsigned>(USPI_FRAME_BUFFER_SIZE));
}
extern "C" void USPiGetMACAddress(unsigned char Buffer[6]) {}
static void generateNetArpPacketValid()
{
std::array<uint8_t, Arp::Packet::SerializedLength()> buffer;
std::ofstream stream("inputs/net-arp-packet-valid.bin");
const Utils::MacAddress sourceMac{10, 11, 12, 13, 14, 15};
const Utils::MacAddress destMac{20, 21, 22, 23, 24, 25};
Arp::Packet packet(Arp::Operation::ARP_OPERATION_REQUEST);
packet.senderMac = sourceMac;
packet.senderIp = Utils::Ipv4Address;
packet.targetMac = destMac;
packet.targetIp = Utils::Ipv4Address;
packet.Serialize(buffer.data(), buffer.size());
stream.write(reinterpret_cast<char*>(buffer.data()), buffer.size());
std::cerr << "Generated Net::Arp::Packet (valid)" << std::endl;
}
int main(int, char**)
{
Utils::Ipv4Address = 0xC0FFEEEE;
generateNetArpPacketValid();
return 0;
}

View file

@ -160,3 +160,23 @@ void TestNetArpHandlePacketInvalid()
TEST_CHECK(buffer == bufferRef);
}
}
void TestNetArpHandlePacketRequest()
{
Ethernet::Header ethernetHeader;
std::array<uint8_t, USPI_FRAME_BUFFER_SIZE> buffer;
Arp::Packet reference(Arp::ARP_OPERATION_REQUEST);
reference.targetIp = Utils::Ipv4Address;
{
auto packet = reference;
const auto size = packet.Serialize(buffer.data(), buffer.size());
const auto bufferRef = buffer;
Arp::HandlePacket(ethernetHeader, buffer.data(), size);
// Check if we've got a proper response
TEST_CHECK(buffer != bufferRef);
}
}

View file

@ -6,3 +6,4 @@ void TestNetArpSendRequest();
void TestNetArpSendReply();
void TestNetArpSendAnnouncement();
void TestNetArpHandlePacketInvalid();
void TestNetArpHandlePacketRequest();

View file

@ -37,5 +37,6 @@ TEST_LIST = {
{"Net::Arp::SendReply", TestNetArpSendReply},
{"Net::Arp::SendAnnouncement", TestNetArpSendAnnouncement},
{"Net::Arp::HandlePacket Invalid", TestNetArpHandlePacketInvalid},
{"Net::Arp::HandlePacket Request", TestNetArpHandlePacketRequest},
{nullptr, nullptr},
};