-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
117 lines (103 loc) · 2.89 KB
/
test.cpp
File metadata and controls
117 lines (103 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "network_switch.h"
#include "shared_storage.h"
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <memory>
#include <random>
#include <tins/pdu.h>
#include <tins/rawpdu.h>
#include <tins/tcp.h>
using std::cout, std::unique_ptr;
unique_ptr<Tins::PDU> generatePDU()
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distByte(0, 255);
vector<uint8_t> payload;
payload.reserve(distByte(rng) + 20);
uint8_t size = distByte(rng);
for (int i = 0; i < size; i++)
{
payload.push_back(distByte(rng));
}
unique_ptr<Tins::TCP> output(new Tins::TCP);
output->sport(distByte(rng) * 2);
output->dport(distByte(rng) * 2);
output->inner_pdu(new Tins::RawPDU(payload));
return output;
}
void printPayload(const vector<uint8_t> & payload)
{
cout << std::hex;
int i = 0;
for (const auto & byte : payload)
{
if (i % 8 == 7)
{
cout << "\n";
i = 0;
}
cout << std::setw(3) << (int)byte;
i++;
}
cout << "\n";
cout << std::dec;
}
#define HASH_COUNT 10
int main (int argc, char *argv[]) {
cout << "Testing packet hashing...\n";
vector<unique_ptr<Tins::PDU>> pdus;
vector<Packet> hashes;
pdus.reserve(HASH_COUNT);
hashes.reserve(HASH_COUNT);
for (int i = 0; i < HASH_COUNT; i++)
{
pdus.push_back(generatePDU());
hashes.push_back({pdus[i].get()});
}
bool ok = true;
for (int i = 0; i < HASH_COUNT; i++)
{
for (int j = 0; j < HASH_COUNT; j++)
{
if (i == j)
{
continue;
}
if (hashes[i] == hashes[j])
{
cout << "Critical! Packets are equal!\n";
printPayload(hashes[i].data);
printPayload(hashes[j].data);
ok = false;
}
if (Packet::Hash()(hashes[i]) == Packet::Hash()(hashes[j]))
{
cout << "Critical! Hashes are colliding!\n";
cout << "First:\n" << std::hex << Packet::Hash()(hashes[i]) << "\n";
printPayload(hashes[i].data);
cout << "Second:\n" << std::hex << Packet::Hash()(hashes[j]) << "\n";
printPayload(hashes[j].data);
ok = false;
}
}
if (Packet::Hash()(hashes[i]) != Packet::Hash()(hashes[i]))
{
cout << "Critical! Hashes are not equal!\n";
cout << std::hex << Packet::Hash()(hashes[i]) << "\n";
printPayload(hashes[i].data);
ok = false;
}
}
if (ok)
{
cout << "---TEST PASS---\n";
}
for (int i = 0; i < HASH_COUNT; i++)
{
cout << i + 1 << ". " << std::hex << Packet::Hash()(hashes[i]) << "\n";
printPayload(hashes[i].data);
}
return 0;
}