]> git.stg.codes - stg.git/blob - tests/test_raw_ip.cpp
Ticket 26. The 'set' replaced to 'rhs' in the SETTINGS_IMPL::operator=.
[stg.git] / tests / test_raw_ip.cpp
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <arpa/inet.h>
4
5 #include <cstdlib>
6 #include <ctime>
7 #include <iostream>
8
9 #include "tut/tut.hpp"
10
11 #include "stg/os_int.h"
12 #include "raw_ip_packet_old.h"
13 #include "stg/raw_ip_packet.h"
14
15 #ifndef ITERATIONS
16 #define ITERATIONS 1000
17 #endif
18
19 void genVector(uint8_t * buf);
20
21 std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p);
22
23 namespace tut
24 {
25     struct rp_data {
26     };
27
28     typedef test_group<rp_data> tg;
29     tg rp_test_group("RAW_PACKET tests group");
30
31     typedef tg::object testobject;
32
33     template<>
34     template<>
35     void testobject::test<1>()
36     {
37         set_test_name("Check structure consistency");
38
39         RAW_PACKET rp;
40         rp.rawPacket.header.ipHeader.ip_v = 4;
41         rp.rawPacket.header.ipHeader.ip_hl = 5;
42         rp.rawPacket.header.ipHeader.ip_tos = 0;
43         rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data
44         rp.rawPacket.header.ipHeader.ip_p = 6;
45         rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1");
46         rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101");
47         rp.rawPacket.header.sPort = htons(80);
48         rp.rawPacket.header.dPort = htons(38546);
49
50         ensure_equals("IP header size (explicitly)", sizeof(rp.rawPacket.header.ipHeader), static_cast<size_t>(20));
51         ensure_equals("IP version", rp.GetIPVersion(), 4);
52         ensure_equals("IP header size (with options)", rp.GetHeaderLen(), 20);
53         ensure_equals("Underlying protocol version", rp.GetProto(), 6);
54         ensure_equals("Packet length", rp.GetLen(), static_cast<uint32_t>(40));
55         ensure_equals("Source IP address", rp.GetSrcIP(), inet_addr("192.168.0.1"));
56         ensure_equals("Destination IP address", rp.GetDstIP(), inet_addr("192.168.0.101"));
57         ensure_equals("Source port number", rp.GetSrcPort(), 80);
58         ensure_equals("Destination port number", rp.GetDstPort(), 38546);
59     }
60
61     template<>
62     template<>
63     void testobject::test<2>()
64     {
65         srand(time(NULL));
66         for (size_t i = 0; i < ITERATIONS; ++i) {
67             RAW_PACKET_OLD p1;
68             RAW_PACKET p2;
69             RAW_PACKET p3;
70
71             uint8_t buf[68];
72             genVector(buf);
73
74             memcpy(p1.pckt, buf, 68);
75             memcpy(p2.rawPacket.pckt, buf, 68);
76             memcpy(p3.rawPacket.pckt, buf, 68);
77
78             ensure_equals("IP versions", p1.GetIPVersion(), p2.GetIPVersion());
79             ensure_equals("IP headers length", p1.GetHeaderLen(), p2.GetHeaderLen());
80             ensure_equals("Protocols", p1.GetProto(), p2.GetProto());
81             ensure_equals("Source IPs", p1.GetSrcIP(), p2.GetSrcIP());
82             ensure_equals("Destination IPs", p1.GetDstIP(), p2.GetDstIP());
83             ensure_equals("Source ports", p1.GetSrcPort(), p2.GetSrcPort());
84             ensure_equals("Destination ports", p1.GetDstPort(), p2.GetDstPort());
85
86             ensure_equals("Self equallity", p2, p3);
87             ensure_equals("Reverse self equallity", p3, p2);
88         }
89     }
90 }
91
92 inline
93 void genVector(uint8_t * buf)
94 {
95     for (size_t i = 0; i < 68; ++i) {
96         buf[i] = rand() % 256;
97     }
98     buf[0] = (buf[0] & 0xF0) | 0x05; // Fix header length
99 }
100
101 std::ostream & operator<<(std::ostream & stream, const RAW_PACKET & p)
102 {
103     stream.unsetf(std::ios::dec);
104     stream.setf(std::ios::hex);
105     for (size_t i = 0; i < sizeof(p.rawPacket.pckt); ++i) {
106         stream << static_cast<unsigned>(p.rawPacket.pckt[i]);
107     }
108     stream.unsetf(std::ios::hex);
109     stream.setf(std::ios::dec);
110     return stream;
111 }