add test src
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,7 +1,7 @@
|
||||
|
||||
# Created by https://www.gitignore.io/api/c++,linux,codeblocks
|
||||
# project
|
||||
test
|
||||
test/vm
|
||||
projectfiles
|
||||
|
||||
### C++ ###
|
||||
|
||||
17
test/CMakeLists.txt
Normal file
17
test/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 2.4.2)
|
||||
if (CMAKE_VERSION VERSION_LESS "3.1")
|
||||
set (CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}")
|
||||
else ()
|
||||
set (CMAKE_CXX_STANDARD 11)
|
||||
endif ()
|
||||
|
||||
project(test_1261nat)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
|
||||
INCLUDE_DIRECTORIES(../lib/libtins/include)
|
||||
include_directories("lib/FakeIt/")
|
||||
include_directories("../src")
|
||||
file(GLOB_RECURSE test_1261nat_src_files "../src/*.h" "../src/*.cpp" "src/*.cpp" "lib/FakeIt/single_header/catch/fakeit.hpp")
|
||||
list(REMOVE_ITEM test_1261nat_src_files ${CMAKE_CURRENT_SOURCE_DIR}/../src/Main.cpp)
|
||||
add_executable(test_1261nat ${test_1261nat_src_files} )
|
||||
target_link_libraries (test_1261nat pthread tins)
|
||||
20
test/lib/FakeIt/LICENSE
Normal file
20
test/lib/FakeIt/LICENSE
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Eran Pe'er
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
141
test/lib/FakeIt/README.md
Normal file
141
test/lib/FakeIt/README.md
Normal file
@@ -0,0 +1,141 @@
|
||||
FakeIt
|
||||
======
|
||||
|
||||
[](https://gitter.im/eranpeer/FakeIt?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
|
||||
GCC: [](https://travis-ci.org/eranpeer/FakeIt)
|
||||
[](https://coveralls.io/github/eranpeer/FakeIt?branch=master)
|
||||
|
||||
MSC: [](https://ci.appveyor.com/project/eranpeer/fakeit)
|
||||
|
||||
FakeIt is a simple mocking framework for C++. It supports GCC, Clang and MS Visual C++.
|
||||
|
||||
FakeIt is written in C++11 and can be used for testing both C++11 and C++ projects.
|
||||
|
||||
```cpp
|
||||
struct SomeInterface {
|
||||
virtual int foo(int) = 0;
|
||||
virtual int bar(string) = 0;
|
||||
};
|
||||
```
|
||||
```cpp
|
||||
// Instantiate a mock object.
|
||||
Mock<SomeInterface> mock;
|
||||
|
||||
// Setup mock behavior.
|
||||
When(Method(mock,foo)).Return(1); // Method mock.foo will return 1 once.
|
||||
|
||||
// Fetch the mock instance.
|
||||
SomeInterface &i = mock.get();
|
||||
|
||||
// Will print "1".
|
||||
cout << i.foo(0);
|
||||
|
||||
|
||||
```
|
||||
Verify method invocation
|
||||
```cpp
|
||||
Mock<SomeInterface> mock;
|
||||
|
||||
When(Method(mock,foo)).Return(0);
|
||||
|
||||
SomeInterface &i = mock.get();
|
||||
|
||||
// Production code
|
||||
i.foo(1);
|
||||
|
||||
// Verify method mock.foo was invoked.
|
||||
Verify(Method(mock,foo));
|
||||
|
||||
// Verify method mock.foo was invoked with specific arguments.
|
||||
Verify(Method(mock,foo).Using(1));
|
||||
```
|
||||
|
||||
Checkout the [Quickstart](https://github.com/eranpeer/FakeIt/wiki/Quickstart) for many more examples!
|
||||
|
||||
Download the [Latest Release](https://github.com/eranpeer/FakeIt/releases/latest) and start using FakeIt now!
|
||||
|
||||
## Features
|
||||
* Packaged as a **single header file**.
|
||||
* Very simple API based on the expressiveness of C++11.
|
||||
* Supports all major compilers: GCC, Clang and MSC++.
|
||||
* Easily integrated with [**GTest**](https://code.google.com/p/googletest/), [**MS Test**](http://en.wikipedia.org/wiki/Visual_Studio_Unit_Testing_Framework) and [**Boost Test**](http://www.boost.org/doc/libs/1_56_0/libs/test/doc/html/index.html).
|
||||
* Expressive [Arrange-Act-Assert](http://xp123.com/articles/3a-arrange-act-assert/) syntax.
|
||||
* Create mock classes or **spy existing objects** instantly in one simple line.
|
||||
* No limitation on number of method arguments.
|
||||
* Supports dynamic casting.
|
||||
|
||||
## Installation
|
||||
FakeIt is a header only framework. It does not require any installation. For extra simplicity fakeit is packaged as a single header file.
|
||||
|
||||
FakeIt is pre-configured to work with some of the major unit testing frameworks. A pre-configured version will use the assertions mechanism of the unit testing framework to integrate the generated error messages into the unit testing framework output.
|
||||
|
||||
If you don't find your unit testing framework on the list, simply use the *standalone* configuration.
|
||||
|
||||
### Using a pre-packaged single header file
|
||||
Pre-packaged single header versions of FakeIt are located under the *single_header* folder.
|
||||
Depending on the unit testing framework you use, simply add one of the pre-packaged versions to the include path of your test project:
|
||||
* <fakeit_folder>/single_header/gtest
|
||||
* <fakeit_folder>/single_header/mstest
|
||||
* <fakeit_folder>/single_header/boost
|
||||
* <fakeit_folder>/single_header/catch
|
||||
* <fakeit_folder>/single_header/tpunit
|
||||
* <fakeit_folder>/single_header/standalone
|
||||
|
||||
For example, to use fakeit with **Google Test** simply add the *single_header/gtest* folder to the include path of your test project:
|
||||
```
|
||||
-I"<fakeit_folder>/single_header/gtest"
|
||||
```
|
||||
### Using the source header files
|
||||
Fakeit source code header files are located under the *include* foler. To use FakeIt directly from the source code all you need to do is to download the source files and add the *include* folder and the configuration folder of your choice to the include path of your project.
|
||||
For example:
|
||||
* To use fakeit with **Google Test** add the *include* folder and the *config/gtest* folder to the include path of your test project:
|
||||
```
|
||||
-I"<fakeit_folder>/include" -I"<fakeit_folder>/config/gtest"
|
||||
```
|
||||
* To use fakeit with **MS Test** add the *include* folder and the *config/mstest* folder to the include path of your test project:
|
||||
```
|
||||
-I"<fakeit_folder>/include" -I"<fakeit_folder>/config/mstest"
|
||||
```
|
||||
* To use fakeit with **Boost Test** add the *include* folder and the *config/boost* folder to the include path of your test project:
|
||||
```
|
||||
-I"<fakeit_folder>/include" -I"<fakeit_folder>/config/boost"
|
||||
```
|
||||
* To use fakeit with **Catch** add the *include* folder and the *config/catch* folder to the include path of your test project:
|
||||
```
|
||||
-I"<fakeit_folder>/include" -I"<fakeit_folder>/config/catch"
|
||||
```
|
||||
* To use fakeit with **tpunit** add the *include* folder and the *config/tpunit* folder to the include path of your test project:
|
||||
```
|
||||
-I"<fakeit_folder>/include" -I"<fakeit_folder>/config/tpunit"
|
||||
```
|
||||
* To use fakeit without any testing framework integration (**standalone**) add the *include* folder and the *config/standalone* folder to the include path of your test project:
|
||||
```
|
||||
-I"<fakeit_folder>/include" -I"<fakeit_folder>/config/standalone"
|
||||
```
|
||||
It is recommended to build and run the unit tests to make sure FakeIt fits your environment.
|
||||
#### Building and Running the Unit Tests with GCC
|
||||
```
|
||||
cd build
|
||||
make all
|
||||
```
|
||||
run the tests by typing
|
||||
```
|
||||
./fakeit_tests.exe
|
||||
```
|
||||
#### Building and Running the Unit Tests with Clang
|
||||
```
|
||||
cd build
|
||||
make -f clang_makefile all
|
||||
```
|
||||
run the tests by typing
|
||||
```
|
||||
./fakeit_tests.exe
|
||||
```
|
||||
#### Building and Running the Unit Tests with Visual Studio
|
||||
Open the tests/all_tests.vcxproj project file with Visual Studio 2013. Build and run the project and check the test results.
|
||||
## Limitations
|
||||
* Currently only GCC, Clang and MSC++ are supported.
|
||||
* Can't mock classes with multiple inheritance.
|
||||
* Can't mock classes with virtual inheritance.
|
||||
* Currently mocks are not thread safe.
|
||||
19630
test/lib/FakeIt/fakeit.hpp
Normal file
19630
test/lib/FakeIt/fakeit.hpp
Normal file
File diff suppressed because it is too large
Load Diff
112
test/src/TestArpToNdpPacketHandler.cpp
Normal file
112
test/src/TestArpToNdpPacketHandler.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <fakeit.hpp>
|
||||
#include <tins/arp.h>
|
||||
#include <tins/ethernetII.h>
|
||||
#include <tins/icmpv6.h>
|
||||
#include <tins/ip.h>
|
||||
#include <tins/ipv6.h>
|
||||
#include <tins/ip_address.h>
|
||||
#include <tins/ipv6_address.h>
|
||||
#include <tins/tcp.h>
|
||||
#include "ArpToNdpPacketHandler.h"
|
||||
#include "HwAddressTranslator.h"
|
||||
#include "IpAddressTranslator.h"
|
||||
using namespace fakeit;
|
||||
|
||||
#ifndef ArpToNdpPacketHandler
|
||||
#define ArpToNdpPacketHandlerTestPrefix "ff::"
|
||||
#endif
|
||||
|
||||
namespace TestArpToNdpPacketHandler
|
||||
{
|
||||
Tins::PDU * currentInputPdu = nullptr;
|
||||
|
||||
void compareToInputPdu(const Tins::PDU & answerPdu)
|
||||
{
|
||||
REQUIRE(currentInputPdu != nullptr);
|
||||
REQUIRE(answerPdu.find_pdu<Tins::IP>() == nullptr);
|
||||
|
||||
const Tins::ICMPv6 * ndp = answerPdu.find_pdu<Tins::ICMPv6>();
|
||||
REQUIRE(ndp != nullptr);
|
||||
|
||||
const Tins::IPv6 * ip = answerPdu.find_pdu<Tins::IPv6>();
|
||||
REQUIRE(ndp != nullptr);
|
||||
|
||||
const Tins::ARP * arp = currentInputPdu->find_pdu<Tins::ARP>();
|
||||
REQUIRE(arp != nullptr);
|
||||
|
||||
Tins::IPv6Address tmp_ipv6(ArpToNdpPacketHandlerTestPrefix);
|
||||
IpAddressTranslator::toIpv6Address(arp->sender_ip_addr(), tmp_ipv6);
|
||||
REQUIRE(ip->src_addr() == tmp_ipv6);
|
||||
bool isRequest = false;
|
||||
switch (arp->opcode())
|
||||
{
|
||||
case Tins::ARP::REQUEST:
|
||||
isRequest = true;
|
||||
REQUIRE(ndp->source_link_layer_addr() == arp->sender_hw_addr());
|
||||
|
||||
IpAddressTranslator::toIpv6Address(arp->target_ip_addr(), tmp_ipv6);
|
||||
REQUIRE(ndp->target_addr()== tmp_ipv6);
|
||||
|
||||
IpAddressTranslator::toSolicitedNodeAddress(arp->target_ip_addr(), tmp_ipv6);
|
||||
REQUIRE(ip->dst_addr() == tmp_ipv6);
|
||||
break;
|
||||
|
||||
case Tins::ARP::REPLY:
|
||||
REQUIRE(ndp->target_link_layer_addr() == arp->sender_hw_addr());
|
||||
REQUIRE(ndp->target_addr()== tmp_ipv6);
|
||||
|
||||
IpAddressTranslator::toIpv6Address(arp->target_ip_addr(), tmp_ipv6);
|
||||
REQUIRE(ip->dst_addr() == tmp_ipv6);
|
||||
break;
|
||||
default:
|
||||
REQUIRE( 1 == 0);
|
||||
}
|
||||
|
||||
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->src_addr() == arp->sender_hw_addr());
|
||||
|
||||
if (isRequest)
|
||||
{
|
||||
HwAddress tmpHw;
|
||||
HwAddressTranslator::toSolicitedNodeAddress(tmp_ipv6, tmpHw);
|
||||
REQUIRE(answerEth->dst_addr() == tmpHw);
|
||||
}
|
||||
else
|
||||
{
|
||||
REQUIRE(answerEth->dst_addr() == currEth->dst_addr());
|
||||
REQUIRE(answerEth->dst_addr() == arp->target_hw_addr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "test ArpToNdpPacketHandler", "[ArpToNdpPacketHandler]" ) {
|
||||
ArpToNdpPacketHandler handler(ArpToNdpPacketHandlerTestPrefix);
|
||||
Mock<IPacketHandler> mockHandler;
|
||||
When(Method(mockHandler, handle)).AlwaysDo([] (IN const Tins::PDU & pdu,...)
|
||||
{
|
||||
TestArpToNdpPacketHandler::compareToInputPdu(pdu);
|
||||
return true;
|
||||
});
|
||||
|
||||
Tins::IPv4Address to_resolve_address("192.168.0.1");
|
||||
Tins::IPv4Address own_address("192.168.0.2");
|
||||
HwAddress own_mac_address("01:02:03:12:34:56");
|
||||
Tins::EthernetII arp_request = Tins::ARP::make_arp_request(to_resolve_address, own_address, own_mac_address);
|
||||
TestArpToNdpPacketHandler::currentInputPdu = &arp_request;
|
||||
REQUIRE(handler.handle(arp_request, nullptr) == false);
|
||||
REQUIRE(handler.handle(arp_request, &mockHandler.get()) == true);
|
||||
Verify(Method(mockHandler, handle)).Once();
|
||||
|
||||
HwAddress to_resolve_mac_address("50:60:70:65:43:21");
|
||||
Tins::EthernetII arp_reply = Tins::ARP::make_arp_reply(to_resolve_address, own_address, to_resolve_mac_address, own_mac_address);
|
||||
TestArpToNdpPacketHandler::currentInputPdu = &arp_reply;
|
||||
REQUIRE(handler.handle(arp_reply, &mockHandler.get()) == true);
|
||||
Verify(Method(mockHandler, handle)).Twice();
|
||||
}
|
||||
18
test/src/TestHwAddressTranslator.cpp
Normal file
18
test/src/TestHwAddressTranslator.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "fakeit.hpp"
|
||||
#include <tins/ipv6_address.h>
|
||||
#include "HwAddressTranslator.h"
|
||||
using namespace fakeit;
|
||||
|
||||
|
||||
TEST_CASE( "test toHwSolicitedNodeAddress", "[HwAddressTranslator]" ) {
|
||||
HwAddress result;
|
||||
|
||||
HwAddressTranslator::toSolicitedNodeAddress(Tins::IPv6Address("2011:0DB8::"), result);
|
||||
REQUIRE(HwAddress("33:33:FF:00:00:00") == result);
|
||||
|
||||
HwAddressTranslator::toSolicitedNodeAddress(Tins::IPv6Address("2011:0DB8::ffff:ff12:3456"), result);
|
||||
REQUIRE(HwAddress("33:33:FF:12:34:56") == result);
|
||||
|
||||
HwAddressTranslator::toSolicitedNodeAddress(Tins::IPv6Address("2011:0DB8::5678:9912:3456"), result);
|
||||
REQUIRE(HwAddress("33:33:FF:12:34:56") == result);
|
||||
}
|
||||
60
test/src/TestIp4ToIp6PacketHandler.cpp
Normal file
60
test/src/TestIp4ToIp6PacketHandler.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#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();
|
||||
}
|
||||
60
test/src/TestIp6ToIp4PacketHandler.cpp
Normal file
60
test/src/TestIp6ToIp4PacketHandler.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#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"
|
||||
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();
|
||||
}
|
||||
114
test/src/TestIpAddressTranslator.cpp
Normal file
114
test/src/TestIpAddressTranslator.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "fakeit.hpp"
|
||||
#include <tins/ip_address.h>
|
||||
#include <tins/ipv6_address.h>
|
||||
#include <tins/ethernetII.h>
|
||||
#include "IpAddressTranslator.h"
|
||||
using namespace fakeit;
|
||||
|
||||
TEST_CASE( "test not operator", "[IpAddressTranslator]" ) {
|
||||
REQUIRE(Tins::IPv4Address("0.0.0.0") == ~Tins::IPv4Address("255.255.255.255"));
|
||||
REQUIRE(Tins::IPv4Address("0.0.0.255") == ~Tins::IPv4Address("255.255.255.0"));
|
||||
REQUIRE(Tins::IPv4Address("0.0.255.255") == ~Tins::IPv4Address("255.255.0.0"));
|
||||
REQUIRE(Tins::IPv4Address("0.255.255.255") == ~Tins::IPv4Address("255.0.0.0"));
|
||||
REQUIRE(Tins::IPv4Address("255.255.255.255") == ~Tins::IPv4Address("0.0.0.0"));
|
||||
REQUIRE(Tins::IPv4Address("0.255.1.255") == ~Tins::IPv4Address("255.0.254.0"));
|
||||
REQUIRE(Tins::IPv4Address("255.254.255.255") == ~Tins::IPv4Address("0.1.0.0"));
|
||||
|
||||
REQUIRE(Tins::IPv6Address("::0") == ~Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"));
|
||||
REQUIRE(Tins::IPv6Address("::FF") == ~Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FF00"));
|
||||
REQUIRE(Tins::IPv6Address("::FFFF") == ~Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:0"));
|
||||
REQUIRE(Tins::IPv6Address("::FFFF:FFFF") == ~Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:0:0"));
|
||||
REQUIRE(Tins::IPv6Address("FFFF:FFFF::FFFF:FFFF") == ~Tins::IPv6Address("0:0:FFFF:FFFF:FFFF:FFFF:0:0"));
|
||||
REQUIRE(Tins::IPv6Address("FFFF:0:FFFF:0:FFFF:0:FFFF:0") == ~Tins::IPv6Address("::FFFF:0:FFFF:0:FFFF:0:FFFF"));
|
||||
REQUIRE(Tins::IPv6Address("FFFF:1:FFFF:1:FFFF:1:FFFF:1") == ~Tins::IPv6Address("::FFFE:0:FFFE:0:FFFE:0:FFFE"));
|
||||
|
||||
REQUIRE(Tins::EthernetII::address_type("00:00:00:00:00:00") == ~Tins::EthernetII::address_type("FF:FF:FF:FF:FF:FF"));
|
||||
REQUIRE(Tins::EthernetII::address_type("00:00:00:FF:FF:FF") == ~Tins::EthernetII::address_type("FF:FF:FF:00:00:00"));
|
||||
REQUIRE(Tins::EthernetII::address_type("00:00:FF:FF:FF:FF") == ~Tins::EthernetII::address_type("FF:FF:00:00:00:00"));
|
||||
REQUIRE(Tins::EthernetII::address_type("00:FF:FF:FF:FF:FF") == ~Tins::EthernetII::address_type("FF:00:00:00:00:00"));
|
||||
REQUIRE(Tins::EthernetII::address_type("FF:FF:FF:FF:FF:FF") == ~Tins::EthernetII::address_type("00:00:00:00:00:00"));
|
||||
REQUIRE(Tins::EthernetII::address_type("00:FF:01:FF:FF:FF") == ~Tins::EthernetII::address_type("FF:00:FE:00:00:00"));
|
||||
REQUIRE(Tins::EthernetII::address_type("FF:FE:FF:FF:FF:FF") == ~Tins::EthernetII::address_type("00:01:00:00:00:00"));
|
||||
}
|
||||
|
||||
TEST_CASE( "test and operator", "[IpAddressTranslator]" ) {
|
||||
REQUIRE(Tins::IPv4Address("0.0.0.0") == (Tins::IPv4Address("0.0.0.0") & Tins::IPv4Address("255.255.255.255")));
|
||||
REQUIRE(Tins::IPv4Address("0.0.255.0") == (Tins::IPv4Address("0.0.255.255") & Tins::IPv4Address("255.255.255.0")));
|
||||
REQUIRE(Tins::IPv4Address("0.255.255.0") == (Tins::IPv4Address("0.255.255.255") & Tins::IPv4Address("255.255.255.0")));
|
||||
REQUIRE(Tins::IPv4Address("255.0.0.0") == (Tins::IPv4Address("255.255.255.255") & Tins::IPv4Address("255.0.0.0")));
|
||||
REQUIRE(Tins::IPv4Address("255.255.255.255") == (Tins::IPv4Address("255.255.255.255") & Tins::IPv4Address("255.255.255.255")));
|
||||
REQUIRE(Tins::IPv4Address("0.1.1.1") == (Tins::IPv4Address("0.255.1.255") & Tins::IPv4Address("1.1.255.1")));
|
||||
REQUIRE(Tins::IPv4Address("0.0.0.0") == (Tins::IPv4Address("255.254.255.255") & Tins::IPv4Address("0.1.0.0")));
|
||||
|
||||
REQUIRE(Tins::IPv6Address("::0") == (Tins::IPv6Address("::0") & Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")));
|
||||
REQUIRE(Tins::IPv6Address("::FF") == (Tins::IPv6Address("::FF") & Tins::IPv6Address("FFFF:FFFF:0000:FFFF:0000:FFFF:FFFF:FFFF")));
|
||||
REQUIRE(Tins::IPv6Address("1:0:1:0:1:0:1:0") == (Tins::IPv6Address("FFFF:0:FFFF:0:FFFF:0:FFFF:0") & Tins::IPv6Address("1:FFFF:1:FFFF:1:FFFF:1:FFFF")));
|
||||
|
||||
REQUIRE(Tins::EthernetII::address_type("00:00:00:00:00:00") == (Tins::EthernetII::address_type("00:00:00:00:00:00") & Tins::EthernetII::address_type("FF:FF:FF:FF:FF:FF")));
|
||||
REQUIRE(Tins::EthernetII::address_type("00:00:00:00:00:05") == (Tins::EthernetII::address_type("00:00:00:FF:FF:FF") & Tins::EthernetII::address_type("FF:FF:FF:00:00:05")));
|
||||
REQUIRE(Tins::EthernetII::address_type("00:07:00:00:00:05") == (Tins::EthernetII::address_type("00:07:00:FF:FF:FF") & Tins::EthernetII::address_type("FF:FF:FF:00:00:05")));
|
||||
}
|
||||
|
||||
TEST_CASE( "test or operator", "[IpAddressTranslator]" ) {
|
||||
REQUIRE(Tins::IPv4Address("255.255.255.255") == (Tins::IPv4Address("0.0.0.0") | Tins::IPv4Address("255.255.255.255")));
|
||||
REQUIRE(Tins::IPv4Address("0.255.255.255") == (Tins::IPv4Address("0.0.255.255") | Tins::IPv4Address("0.255.255.0")));
|
||||
REQUIRE(Tins::IPv4Address("255.255.255.255") == (Tins::IPv4Address("0.255.255.255") | Tins::IPv4Address("255.255.255.255")));
|
||||
REQUIRE(Tins::IPv4Address("255.0.0.0") == (Tins::IPv4Address("255.0.0.0") | Tins::IPv4Address("255.0.0.0")));
|
||||
REQUIRE(Tins::IPv4Address("255.255.255.255") == (Tins::IPv4Address("255.255.255.255") | Tins::IPv4Address("255.255.255.255")));
|
||||
REQUIRE(Tins::IPv4Address("1.255.255.255") == (Tins::IPv4Address("0.255.1.255") | Tins::IPv4Address("1.1.255.1")));
|
||||
REQUIRE(Tins::IPv4Address("0.255.0.0") == (Tins::IPv4Address("0.254.0.0") | Tins::IPv4Address("0.1.0.0")));
|
||||
|
||||
REQUIRE(Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") == (Tins::IPv6Address("::0") | Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")));
|
||||
REQUIRE(Tins::IPv6Address("FFFF:FFFF:0000:FFFF:00FF:FFFF:FFFF:FFFF") == (Tins::IPv6Address("FFFF:FFFF:0000:FFFF:00AA:FFFF:FFFF:FFFF") | Tins::IPv6Address("0000:FFFF:0000:FFFF:00FF:FFFF:FFFF:FFFF")));
|
||||
REQUIRE(Tins::IPv6Address("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF") == (Tins::IPv6Address("FFFF:0:FFFF:0:FFFF:0:FFFF:0") | Tins::IPv6Address("1:FFFF:1:FFFF:1:FFFF:1:FFFF")));
|
||||
|
||||
REQUIRE(Tins::EthernetII::address_type("FF:FF:FF:FF:FF:FF") == (Tins::EthernetII::address_type("00:00:00:00:00:00") | Tins::EthernetII::address_type("FF:FF:FF:FF:FF:FF")));
|
||||
REQUIRE(Tins::EthernetII::address_type("FF:FF:FF:FF:FF:05") == (Tins::EthernetII::address_type("00:00:00:FF:FF:00") | Tins::EthernetII::address_type("FF:FF:FF:00:00:05")));
|
||||
REQUIRE(Tins::EthernetII::address_type("FF:07:FF:00:00:05") == (Tins::EthernetII::address_type("00:06:00:00:00:00") | Tins::EthernetII::address_type("FF:01:FF:00:00:05")));
|
||||
}
|
||||
|
||||
TEST_CASE( "test toIpv4AddressBytes", "[IpAddressTranslator]" ) {
|
||||
REQUIRE(0 == IpAddressTranslator::toIpv4AddressBytes(Tins::IPv6Address()));
|
||||
REQUIRE(Tins::IPv4Address("0.0.0.1") == Tins::IPv4Address(IpAddressTranslator::toIpv4AddressBytes( "::1")));
|
||||
REQUIRE(Tins::IPv4Address("111.111.111.111") == Tins::IPv4Address(IpAddressTranslator::toIpv4AddressBytes("::111.111.111.111")));
|
||||
REQUIRE(Tins::IPv4Address("192.168.42.1") == Tins::IPv4Address(IpAddressTranslator::toIpv4AddressBytes("2011:0DB8::1111:192.168.42.1")));
|
||||
REQUIRE(Tins::IPv4Address("1.2.3.4") == Tins::IPv4Address(IpAddressTranslator::toIpv4AddressBytes("2011:0DB8::0102:0304")));
|
||||
}
|
||||
|
||||
TEST_CASE( "test toIpv6Address", "[IpAddressTranslator]" ) {
|
||||
Tins::IPv6Address ipv6;
|
||||
IpAddressTranslator::toIpv6Address(Tins::IPv4Address("0.0.0.1"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("::1") == ipv6);
|
||||
|
||||
IpAddressTranslator::toIpv6Address(Tins::IPv4Address("111.111.111.111"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("::111.111.111.111") == ipv6);
|
||||
|
||||
ipv6 = Tins::IPv6Address("2011:0DB8::");
|
||||
IpAddressTranslator::toIpv6Address(Tins::IPv4Address("192.168.42.1"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("2011:0DB8::192.168.42.1") == ipv6);
|
||||
|
||||
IpAddressTranslator::toIpv6Address(Tins::IPv4Address("1.2.3.4"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("2011:0DB8::0102:0304") == ipv6);
|
||||
|
||||
ipv6 = Tins::IPv6Address("2011:0DB8::ffff:ffff:ffff");
|
||||
IpAddressTranslator::toIpv6Address(Tins::IPv4Address("1.2.3.4"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("2011:0DB8::ffff:0102:0304") == ipv6);
|
||||
}
|
||||
|
||||
TEST_CASE( "test toSolicitedNodeAddress", "[IpAddressTranslator]" ) {
|
||||
Tins::IPv6Address ipv6;
|
||||
|
||||
IpAddressTranslator::toSolicitedNodeAddress(Tins::IPv6Address("2011:0DB8::"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("FF02::1:FF00:0") == ipv6);
|
||||
|
||||
IpAddressTranslator::toSolicitedNodeAddress(Tins::IPv6Address("2011:0DB8::ffff:ff12:3456"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("FF02::1:FF12:3456") == ipv6);
|
||||
|
||||
IpAddressTranslator::toSolicitedNodeAddress(Tins::IPv4Address("0.0.0.1"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("FF02::1:FF00:1") == ipv6);
|
||||
|
||||
IpAddressTranslator::toSolicitedNodeAddress(Tins::IPv4Address("1.2.3.4"), ipv6);
|
||||
REQUIRE(Tins::IPv6Address("FF02::1:FF02:0304") == ipv6);
|
||||
|
||||
|
||||
}
|
||||
26
test/src/testcase.cpp
Normal file
26
test/src/testcase.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
|
||||
#include "fakeit.hpp"
|
||||
using namespace fakeit;
|
||||
|
||||
struct SomeInterface {
|
||||
virtual int foo(int) = 0;
|
||||
virtual int bar(int,int) = 0;
|
||||
};
|
||||
|
||||
TEST_CASE( "test foo succss", "[footest]" ) {
|
||||
// Stub a method to return a value once
|
||||
Mock<SomeInterface> mock;
|
||||
|
||||
// Stub a method to return a value once
|
||||
When(Method(mock,foo)).AlwaysReturn(1);
|
||||
|
||||
SomeInterface & i = mock.get();
|
||||
// Production code:
|
||||
i.foo(1);
|
||||
i.foo(2);
|
||||
i.foo(3);
|
||||
|
||||
//c checks
|
||||
Verify(Method(mock,foo));
|
||||
REQUIRE(1 == 1);
|
||||
}
|
||||
Reference in New Issue
Block a user