X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/e9ae1f101b5418c0ba2e6c9d86b23c12f0140982..8582d7dfe21e7225d4a715501d70c2649268228f:/tests/test_raw_ip.cpp diff --git a/tests/test_raw_ip.cpp b/tests/test_raw_ip.cpp index 90a2cdd5..4c7ce121 100644 --- a/tests/test_raw_ip.cpp +++ b/tests/test_raw_ip.cpp @@ -1,114 +1,108 @@ -#include -#include -#include +#define BOOST_TEST_MODULE STGRawIP + +#include "raw_ip_packet_old.h" +#include "stg/raw_ip_packet.h" + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wsign-compare" +#pragma GCC diagnostic ignored "-Wparentheses" +#include +#pragma GCC diagnostic pop #include #include #include #include -#include "tut/tut.hpp" - -#include "raw_ip_packet_old.h" -#include "stg/raw_ip_packet.h" +#include +#include +#include -#ifndef ITERATIONS -#define ITERATIONS 1000 -#endif +namespace +{ -void genVector(uint8_t * buf); +constexpr size_t ITERATIONS = 1000; -namespace STG +std::array genVector() { -std::ostream & operator<<(std::ostream & stream, const RawPacket & p); + std::array res; + for (size_t i = 0; i < res.size(); ++i) + res[i] = rand() % 256; + res[0] = (res[0] & 0xF0) | 0x05; // Fix header length + return res; } -namespace tut -{ - struct rp_data { - }; - - typedef test_group tg; - tg rp_test_group("STG::RawPacket tests group"); - - typedef tg::object testobject; - - template<> - template<> - void testobject::test<1>() - { - set_test_name("Check structure consistency"); - - STG::RawPacket rp; - rp.rawPacket.header.ipHeader.ip_v = 4; - rp.rawPacket.header.ipHeader.ip_hl = 5; - rp.rawPacket.header.ipHeader.ip_tos = 0; - rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data - rp.rawPacket.header.ipHeader.ip_p = 6; - rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1"); - rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101"); - rp.rawPacket.header.sPort = htons(80); - rp.rawPacket.header.dPort = htons(38546); - - ensure_equals("IP header size (explicitly)", sizeof(rp.rawPacket.header.ipHeader), static_cast(20)); - ensure_equals("IP version", rp.GetIPVersion(), 4); - ensure_equals("IP header size (with options)", rp.GetHeaderLen(), 20); - ensure_equals("Underlying protocol version", rp.GetProto(), 6); - ensure_equals("Packet length", rp.GetLen(), static_cast(40)); - ensure_equals("Source IP address", rp.GetSrcIP(), inet_addr("192.168.0.1")); - ensure_equals("Destination IP address", rp.GetDstIP(), inet_addr("192.168.0.101")); - ensure_equals("Source port number", rp.GetSrcPort(), 80); - ensure_equals("Destination port number", rp.GetDstPort(), 38546); - } - - template<> - template<> - void testobject::test<2>() - { - srand(time(NULL)); - for (size_t i = 0; i < ITERATIONS; ++i) { - RAW_PACKET_OLD p1; - STG::RawPacket p2; - STG::RawPacket p3; - - uint8_t buf[68]; - genVector(buf); - - memcpy(p1.pckt, buf, 68); - memcpy(p2.rawPacket.data, buf, 68); - memcpy(p3.rawPacket.data, buf, 68); - - ensure_equals("IP versions", p1.GetIPVersion(), p2.GetIPVersion()); - ensure_equals("IP headers length", p1.GetHeaderLen(), p2.GetHeaderLen()); - ensure_equals("Protocols", p1.GetProto(), p2.GetProto()); - ensure_equals("Source IPs", p1.GetSrcIP(), p2.GetSrcIP()); - ensure_equals("Destination IPs", p1.GetDstIP(), p2.GetDstIP()); - ensure_equals("Source ports", p1.GetSrcPort(), p2.GetSrcPort()); - ensure_equals("Destination ports", p1.GetDstPort(), p2.GetDstPort()); - - ensure_equals("Self equallity", p2, p3); - ensure_equals("Reverse self equallity", p3, p2); - } - } } -inline -void genVector(uint8_t * buf) +namespace std { - for (size_t i = 0; i < 68; ++i) { - buf[i] = rand() % 256; - } - buf[0] = (buf[0] & 0xF0) | 0x05; // Fix header length -} -std::ostream & STG::operator<<(std::ostream & stream, const RawPacket & p) +std::ostream & operator<<(std::ostream& stream, const STG::RawPacket& p) { - stream.unsetf(std::ios::dec); stream.setf(std::ios::hex); for (size_t i = 0; i < sizeof(p.rawPacket.data); ++i) { stream << static_cast(p.rawPacket.data[i]); } - stream.unsetf(std::ios::hex); stream.setf(std::ios::dec); return stream; } + +} + +BOOST_AUTO_TEST_SUITE(RawIP) + +BOOST_AUTO_TEST_CASE(StructureConsistency) +{ + STG::RawPacket rp; + rp.rawPacket.header.ipHeader.ip_v = 4; + rp.rawPacket.header.ipHeader.ip_hl = 5; + rp.rawPacket.header.ipHeader.ip_tos = 0; + rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data + rp.rawPacket.header.ipHeader.ip_p = 6; + rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1"); + rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101"); + rp.rawPacket.header.sPort = htons(80); + rp.rawPacket.header.dPort = htons(38546); + + BOOST_CHECK_EQUAL(sizeof(rp.rawPacket.header.ipHeader), static_cast(20)); + BOOST_CHECK_EQUAL(rp.GetIPVersion(), 4); + BOOST_CHECK_EQUAL(rp.GetHeaderLen(), 20); + BOOST_CHECK_EQUAL(rp.GetProto(), 6); + BOOST_CHECK_EQUAL(rp.GetLen(), static_cast(40)); + BOOST_CHECK_EQUAL(rp.GetSrcIP(), inet_addr("192.168.0.1")); + BOOST_CHECK_EQUAL(rp.GetDstIP(), inet_addr("192.168.0.101")); + BOOST_CHECK_EQUAL(rp.GetSrcPort(), 80); + BOOST_CHECK_EQUAL(rp.GetDstPort(), 38546); +} + +BOOST_AUTO_TEST_CASE(RandomTests) +{ + srand(time(NULL)); + for (size_t i = 0; i < ITERATIONS; ++i) + { + RawPacketOld p1; + STG::RawPacket p2; + STG::RawPacket p3; + + const auto buf = genVector(); + + memcpy(p1.pckt, buf.data(), 68); + memcpy(p2.rawPacket.data, buf.data(), 68); + memcpy(p3.rawPacket.data, buf.data(), 68); + + BOOST_CHECK_EQUAL(p1.GetIPVersion(), p2.GetIPVersion()); + BOOST_CHECK_EQUAL(p1.GetHeaderLen(), p2.GetHeaderLen()); + BOOST_CHECK_EQUAL(p1.GetProto(), p2.GetProto()); + BOOST_CHECK_EQUAL(p1.GetSrcIP(), p2.GetSrcIP()); + BOOST_CHECK_EQUAL(p1.GetDstIP(), p2.GetDstIP()); + BOOST_CHECK_EQUAL(p1.GetSrcPort(), p2.GetSrcPort()); + BOOST_CHECK_EQUAL(p1.GetDstPort(), p2.GetDstPort()); + + BOOST_CHECK_EQUAL(p2, p3); + BOOST_CHECK_EQUAL(p3, p2); + } +} + +BOOST_AUTO_TEST_SUITE_END()