1 #include <sys/socket.h>
2 #include <netinet/in.h>
11 #include "raw_ip_packet_old.h"
12 #include "stg/raw_ip_packet.h"
15 #define ITERATIONS 1000
18 void genVector(uint8_t * buf);
20 std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p);
27 typedef test_group<rp_data> tg;
28 tg rp_test_group("RAW_PACKET tests group");
30 typedef tg::object testobject;
34 void testobject::test<1>()
36 set_test_name("Check structure consistency");
39 rp.rawPacket.header.ipHeader.ip_v = 4;
40 rp.rawPacket.header.ipHeader.ip_hl = 5;
41 rp.rawPacket.header.ipHeader.ip_tos = 0;
42 rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data
43 rp.rawPacket.header.ipHeader.ip_p = 6;
44 rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1");
45 rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101");
46 rp.rawPacket.header.sPort = htons(80);
47 rp.rawPacket.header.dPort = htons(38546);
49 ensure_equals("IP header size (explicitly)", sizeof(rp.rawPacket.header.ipHeader), 20);
50 ensure_equals("IP version", rp.GetIPVersion(), 4);
51 ensure_equals("IP header size (with options)", rp.GetHeaderLen(), 20);
52 ensure_equals("Underlying protocol version", rp.GetProto(), 6);
53 ensure_equals("Packet length", rp.GetLen(), 40);
54 ensure_equals("Source IP address", rp.GetSrcIP(), inet_addr("192.168.0.1"));
55 ensure_equals("Destination IP address", rp.GetDstIP(), inet_addr("192.168.0.101"));
56 ensure_equals("Source port number", rp.GetSrcPort(), 80);
57 ensure_equals("Destination port number", rp.GetDstPort(), 38546);
62 void testobject::test<2>()
65 for (size_t i = 0; i < ITERATIONS; ++i) {
73 memcpy(p1.pckt, buf, 68);
74 memcpy(p2.rawPacket.pckt, buf, 68);
75 memcpy(p3.rawPacket.pckt, buf, 68);
77 ensure_equals("IP versions", p1.GetIPVersion(), p2.GetIPVersion());
78 ensure_equals("IP headers length", p1.GetHeaderLen(), p2.GetHeaderLen());
79 ensure_equals("Protocols", p1.GetProto(), p2.GetProto());
80 ensure_equals("Source IPs", p1.GetSrcIP(), p2.GetSrcIP());
81 ensure_equals("Destination IPs", p1.GetDstIP(), p2.GetDstIP());
82 ensure_equals("Source ports", p1.GetSrcPort(), p2.GetSrcPort());
83 ensure_equals("Destination ports", p1.GetDstPort(), p2.GetDstPort());
85 ensure_equals("Self equallity", p2, p3);
86 ensure_equals("Reverse self equallity", p3, p2);
92 void genVector(uint8_t * buf)
94 for (size_t i = 0; i < 68; ++i) {
95 buf[i] = rand() % 256;
97 buf[0] = (buf[0] & 0xF0) | 0x05; // Fix header length
100 std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p)
102 stream.unsetf(std::ios::dec);
103 stream.setf(std::ios::hex);
104 for (size_t i = 0; i < sizeof(p.rawPacket.pckt); ++i) {
105 stream << static_cast<unsigned>(p.rawPacket.pckt[i]);
107 stream.unsetf(std::ios::hex);
108 stream.setf(std::ios::dec);