82 lines
2.7 KiB
C++
82 lines
2.7 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 "Ip6ToIp4PacketHandler.h"
|
|
#include "IpAddressTranslator.h"
|
|
#include "TinsNetworkInterfaceCard_t.h"
|
|
using namespace fakeit;
|
|
|
|
|
|
|
|
namespace TestIp6ToIp4PacketHandler
|
|
{
|
|
Tins::PDU * currentInputPdu = nullptr;
|
|
|
|
void compareToInputPdu(const Tins::PDU & answerPdu)
|
|
{
|
|
REQUIRE(currentInputPdu != nullptr);
|
|
REQUIRE(answerPdu.find_pdu<Tins::IPv6>() == nullptr);
|
|
|
|
const Tins::IP * ip = answerPdu.find_pdu<Tins::IP>();
|
|
REQUIRE(ip != nullptr);
|
|
|
|
const Tins::IPv6 * ip6 = currentInputPdu->find_pdu<Tins::IPv6>();
|
|
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 Ip6ToIp4PacketHandler", "[Ip6ToIp4PacketHandler]" ) {
|
|
Ip6ToIp4PacketHandler handler;
|
|
Mock<IPacketHandler> mockHandler;
|
|
When(Method(mockHandler, handle)).AlwaysDo([] (IN const Tins::PDU & pdu,...)
|
|
{
|
|
TestIp6ToIp4PacketHandler::compareToInputPdu(pdu);
|
|
return true;
|
|
});
|
|
|
|
Tins::EthernetII pkt = Tins::EthernetII() / Tins::IPv6() / Tins::TCP();
|
|
TestIp6ToIp4PacketHandler::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::IPv6("::1", "::2") / Tins::TCP();
|
|
REQUIRE(handler.handle(pkt, &mockHandler.get()) == true);
|
|
Verify(Method(mockHandler, handle)).Twice();
|
|
|
|
// test pkt hashes
|
|
Tins::EthernetII * clonePkt = pkt.clone();
|
|
REQUIRE((long) clonePkt != (long) &pkt);
|
|
Tins::PDU::serialization_type ser_pkt = pkt.serialize();
|
|
Tins::PDU::serialization_type ser_clonepkt = clonePkt->serialize();
|
|
REQUIRE(ser_pkt == ser_clonepkt);
|
|
|
|
Tins::PDU & pdu = *((Tins::PDU *)&pkt);
|
|
std::hash<Tins::PDU::serialization_type>pdu_hash;
|
|
std::size_t h1 = pdu_hash(ser_pkt);
|
|
std::size_t h2 = pdu_hash(ser_clonepkt);
|
|
|
|
REQUIRE(h1 == h2);
|
|
pkt = Tins::EthernetII("11:22:33:44:55:66", "66:55:44:33:22:11") / Tins::IPv6("::2", "::1") / Tins::TCP();
|
|
ser_pkt = pkt.serialize();
|
|
h1 = pdu_hash(ser_pkt);
|
|
REQUIRE(h1 != h2);
|
|
}
|