]> git.stg.codes - stg.git/blobdiff - tests/test_raw_ip.cpp
Non-virtual admin.
[stg.git] / tests / test_raw_ip.cpp
index 3b3a97fa4882c4e52a32ee14fcbefff4353c3bb8..90a2cdd5a44758ec193cdfa6a196bc2c8ebb770e 100644 (file)
@@ -3,6 +3,7 @@
 #include <arpa/inet.h>
 
 #include <cstdlib>
+#include <cstdint>
 #include <ctime>
 #include <iostream>
 
 
 void genVector(uint8_t * buf);
 
-std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p);
+namespace STG
+{
+std::ostream & operator<<(std::ostream & stream, const RawPacket & p);
+}
 
 namespace tut
 {
@@ -25,7 +29,7 @@ namespace tut
     };
 
     typedef test_group<rp_data> tg;
-    tg rp_test_group("RAW_PACKET tests group");
+    tg rp_test_group("STG::RawPacket tests group");
 
     typedef tg::object testobject;
 
@@ -35,22 +39,22 @@ namespace tut
     {
         set_test_name("Check structure consistency");
 
-        RAW_PACKET rp;
-        rp.header.ipHeader.ip_v = 4;
-        rp.header.ipHeader.ip_hl = 5;
-        rp.header.ipHeader.ip_tos = 0;
-        rp.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data
-        rp.header.ipHeader.ip_p = 6;
-        rp.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1");
-        rp.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101");
-        rp.header.sPort = htons(80);
-        rp.header.dPort = htons(38546);
-
-        ensure_equals("IP header size (explicitly)", sizeof(rp.header.ipHeader), 20);
+        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<size_t>(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(), 40);
+        ensure_equals("Packet length", rp.GetLen(), static_cast<uint32_t>(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);
@@ -64,15 +68,15 @@ namespace tut
         srand(time(NULL));
         for (size_t i = 0; i < ITERATIONS; ++i) {
             RAW_PACKET_OLD p1;
-            RAW_PACKET p2;
-            RAW_PACKET p3;
+            STG::RawPacket p2;
+            STG::RawPacket p3;
 
             uint8_t buf[68];
             genVector(buf);
 
             memcpy(p1.pckt, buf, 68);
-            memcpy(p2.pckt, buf, 68);
-            memcpy(p3.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());
@@ -97,12 +101,12 @@ void genVector(uint8_t * buf)
     buf[0] = (buf[0] & 0xF0) | 0x05; // Fix header length
 }
 
-std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p)
+std::ostream & STG::operator<<(std::ostream & stream, const RawPacket & p)
 {
     stream.unsetf(std::ios::dec);
     stream.setf(std::ios::hex);
-    for (size_t i = 0; i < sizeof(p.pckt); ++i) {
-        stream << static_cast<unsigned>(p.pckt[i]);
+    for (size_t i = 0; i < sizeof(p.rawPacket.data); ++i) {
+        stream << static_cast<unsigned>(p.rawPacket.data[i]);
     }
     stream.unsetf(std::ios::hex);
     stream.setf(std::ios::dec);