1 #define BOOST_TEST_MODULE STGRawIP
3 #include "raw_ip_packet_old.h"
4 #include "stg/raw_ip_packet.h"
6 #pragma GCC diagnostic push
7 #pragma GCC diagnostic ignored "-Wold-style-cast"
8 #pragma GCC diagnostic ignored "-Wunused-parameter"
9 #pragma GCC diagnostic ignored "-Wsign-compare"
10 #pragma GCC diagnostic ignored "-Wparentheses"
11 #include <boost/test/unit_test.hpp>
12 #pragma GCC diagnostic pop
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
26 constexpr size_t ITERATIONS = 1000;
28 std::array<uint8_t, 68> genVector()
30 std::array<uint8_t, 68> res;
31 for (size_t i = 0; i < 68; ++i)
32 res[i] = rand() % 256;
33 res[0] = (res[0] & 0xF0) | 0x05; // Fix header length
42 std::ostream & operator<<(std::ostream& stream, const STG::RawPacket& p)
44 stream.setf(std::ios::hex);
45 for (size_t i = 0; i < sizeof(p.rawPacket.data); ++i) {
46 stream << static_cast<unsigned>(p.rawPacket.data[i]);
48 stream.setf(std::ios::dec);
54 BOOST_AUTO_TEST_SUITE(RawIP)
56 BOOST_AUTO_TEST_CASE(StructureConsistency)
59 rp.rawPacket.header.ipHeader.ip_v = 4;
60 rp.rawPacket.header.ipHeader.ip_hl = 5;
61 rp.rawPacket.header.ipHeader.ip_tos = 0;
62 rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data
63 rp.rawPacket.header.ipHeader.ip_p = 6;
64 rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1");
65 rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101");
66 rp.rawPacket.header.sPort = htons(80);
67 rp.rawPacket.header.dPort = htons(38546);
69 BOOST_CHECK_EQUAL(sizeof(rp.rawPacket.header.ipHeader), static_cast<size_t>(20));
70 BOOST_CHECK_EQUAL(rp.GetIPVersion(), 4);
71 BOOST_CHECK_EQUAL(rp.GetHeaderLen(), 20);
72 BOOST_CHECK_EQUAL(rp.GetProto(), 6);
73 BOOST_CHECK_EQUAL(rp.GetLen(), static_cast<uint32_t>(40));
74 BOOST_CHECK_EQUAL(rp.GetSrcIP(), inet_addr("192.168.0.1"));
75 BOOST_CHECK_EQUAL(rp.GetDstIP(), inet_addr("192.168.0.101"));
76 BOOST_CHECK_EQUAL(rp.GetSrcPort(), 80);
77 BOOST_CHECK_EQUAL(rp.GetDstPort(), 38546);
80 BOOST_AUTO_TEST_CASE(RandomTests)
83 for (size_t i = 0; i < ITERATIONS; ++i)
89 const auto buf = genVector();
91 memcpy(p1.pckt, buf.data(), 68);
92 memcpy(p2.rawPacket.data, buf.data(), 68);
93 memcpy(p3.rawPacket.data, buf.data(), 68);
95 BOOST_CHECK_EQUAL(p1.GetIPVersion(), p2.GetIPVersion());
96 BOOST_CHECK_EQUAL(p1.GetHeaderLen(), p2.GetHeaderLen());
97 BOOST_CHECK_EQUAL(p1.GetProto(), p2.GetProto());
98 BOOST_CHECK_EQUAL(p1.GetSrcIP(), p2.GetSrcIP());
99 BOOST_CHECK_EQUAL(p1.GetDstIP(), p2.GetDstIP());
100 BOOST_CHECK_EQUAL(p1.GetSrcPort(), p2.GetSrcPort());
101 BOOST_CHECK_EQUAL(p1.GetDstPort(), p2.GetDstPort());
103 BOOST_CHECK_EQUAL(p2, p3);
104 BOOST_CHECK_EQUAL(p3, p2);
108 BOOST_AUTO_TEST_SUITE_END()