#include "fakeit.hpp" #include "tins/ethernetII.h" #include #include #include #include #include #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() == nullptr); const Tins::IPv6 * ip6 = answerPdu.find_pdu(); const Tins::IP * ip = currentInputPdu->find_pdu(); 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(); if (currEth == nullptr) { return; } const Tins::EthernetII * answerEth = answerPdu.find_pdu(); 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 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(); }