61 lines
2.0 KiB
C++
61 lines
2.0 KiB
C++
#include "fakeit.hpp"
|
|
#include "tins/ethernetII.h"
|
|
#include <tins/ip.h>
|
|
#include <tins/ipv6.h>
|
|
#include <tins/ip_address.h>
|
|
#include <tins/ipv6_address.h>
|
|
#include <tins/tcp.h>
|
|
#include "Ip4ToIp6PacketHandler.h"
|
|
#include "IpAddressTranslator.h"
|
|
using namespace fakeit;
|
|
|
|
|
|
namespace TestIp4ToIp6PacketHandler
|
|
{
|
|
Tins::PDU * currentInputPdu = nullptr;
|
|
|
|
void compareToInputPdu(const Tins::PDU & answerPdu)
|
|
{
|
|
REQUIRE(currentInputPdu != nullptr);
|
|
REQUIRE(answerPdu.find_pdu<Tins::IP>() == nullptr);
|
|
|
|
const Tins::IPv6 * ip6 = answerPdu.find_pdu<Tins::IPv6>();
|
|
const Tins::IP * ip = currentInputPdu->find_pdu<Tins::IP>();
|
|
REQUIRE(ip != nullptr);
|
|
REQUIRE(ip6 != nullptr);
|
|
REQUIRE(ip->src_addr() == Tins::IPv4Address(IpAddressTranslator::toIpv4AddressBytes(ip6->src_addr())));
|
|
REQUIRE(ip->dst_addr() == Tins::IPv4Address(IpAddressTranslator::toIpv4AddressBytes(ip6->dst_addr())));
|
|
REQUIRE(answerPdu.inner_pdu() != nullptr);
|
|
|
|
const Tins::EthernetII * currEth = currentInputPdu->find_pdu<Tins::EthernetII>();
|
|
if (currEth == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
const Tins::EthernetII * answerEth = answerPdu.find_pdu<Tins::EthernetII>();
|
|
REQUIRE(answerEth->src_addr() == currEth->src_addr());
|
|
REQUIRE(answerEth->dst_addr() == currEth->dst_addr());
|
|
}
|
|
}
|
|
|
|
TEST_CASE( "test Ip4ToIp6PacketHandler", "[Ip4ToIp6PacketHandler]" ) {
|
|
Ip4ToIp6PacketHandler handler("1::"); // tod fix prefix
|
|
Mock<IPacketHandler> mockHandler;
|
|
When(Method(mockHandler, handle)).AlwaysDo([] (IN const Tins::PDU & pdu,...)
|
|
{
|
|
TestIp4ToIp6PacketHandler::compareToInputPdu(pdu);
|
|
return true;
|
|
});
|
|
|
|
Tins::EthernetII pkt = Tins::EthernetII() / Tins::IP() / Tins::TCP();
|
|
TestIp4ToIp6PacketHandler::currentInputPdu = &pkt;
|
|
REQUIRE(handler.handle(pkt, nullptr) == false);
|
|
REQUIRE(handler.handle(pkt, &mockHandler.get()) == true);
|
|
Verify(Method(mockHandler, handle)).Once();
|
|
|
|
pkt = Tins::EthernetII("11:22:33:44:55:66", "66:55:44:33:22:11") / Tins::IP("1.2.3.4", "4.3.2.1") / Tins::TCP();
|
|
REQUIRE(handler.handle(pkt, &mockHandler.get()) == true);
|
|
Verify(Method(mockHandler, handle)).Twice();
|
|
}
|