3 * - server: 192.168.0.1
4 * - user A: 192.168.0.2
5 * - user B: 192.168.0.3
8 * - host 1: 216.239.59.104
9 * - host 2: 72.14.221.104
10 * - host 3: 66.249.93.104
11 * - host 4: 195.5.61.68
14 * - Local: ALL 192.168.0.0/24
15 * - DNS: TCP_UDP 195.5.61.68/32:53
16 * - FTP: TCP 129.22.8.159/32:20-21
17 * - World: ALL 0.0.0.0/0
27 #include <arpa/inet.h>
30 #include "rules_finder.h"
38 RULE MakeRule(const std::string & ip,
39 const std::string & mask,
47 rule.ip = inet_addr(ip.c_str());
48 rule.mask = inet_addr(mask.c_str());
60 RULE local(MakeRule("192.168.0.0",
66 RULE dns(MakeRule("195.5.61.68",
72 RULE ftp(MakeRule("129.22.8.159",
78 RULE world(MakeRule("0.0.0.0",
85 rules.push_back(local);
91 rules.push_back(world);
96 PENDING_PACKET MakePacket(const std::string & from,
97 const std::string & to,
101 PENDING_PACKET::DIRECTION direction,
109 hdr.tot_len = length;
113 hdr.protocol = proto;
115 hdr.saddr = inet_addr(from.c_str());
116 hdr.daddr = inet_addr(to.c_str());
118 PENDING_PACKET packet(hdr, sport, dport);
120 packet.direction = direction;
127 int actualDir; // Parser error status
128 bool stdException; // Parser throws an std execption
129 bool otherException; // Parser throws another exception
133 struct RF_TESTER : public std::unary_function<std::pair<PENDING_PACKET, int>, void>
136 RF_TESTER(RULES_FINDER & r)
149 void operator()(const std::pair<PENDING_PACKET, int> & entry)
152 info.wantedDir = entry.second;
154 info.stdException = false;
155 info.otherException = false;
159 info.actualDir = rf.GetDir(entry.first);
161 catch (std::exception & ex)
163 info.stdException = true;
168 info.otherException = true;
171 info.result &= (info.actualDir == info.wantedDir);
172 result &= info.result;
173 testLog.push_back(info);
179 std::cout << "RF_TESTER results:\n";
180 std::cout << "-----------------------------------------------------------------\n";
181 std::vector<TEST_INFO>::const_iterator it;
182 for (it = testLog.begin(); it != testLog.end(); ++it)
184 std::cout << "Test no.: " << testNumber++ << "\t"
185 << "Correct dir: " << it->wantedDir << "\t"
186 << "Actual dir:" << it->actualDir << "\t"
187 << "STD exceptions: " << it->stdException << "\t"
188 << "Other exceptions: " << it->otherException << "\t"
189 << "Result: " << it->result << "\n";
191 std::cout << "-----------------------------------------------------------------\n";
192 std::cout << "Final result: " << (result ? "passed" : "failed") << std::endl;
195 bool Result() const { return result; };
198 std::vector<TEST_INFO> testLog;
204 RULES rules(PrepareRules());
209 std::list<std::pair<PENDING_PACKET, int> > tests;
212 tests.push_back(make_pair(MakePacket("192.168.0.2", "192.168.0.1", 3214, 22, 6, PENDING_PACKET::OUTGOING, 0), 0));
213 tests.push_back(make_pair(MakePacket("192.168.0.1", "192.168.0.2", 22, 3214, 6, PENDING_PACKET::OUTGOING, 0), 0));
214 // Local, SSH, incorrect direction detection
215 tests.push_back(make_pair(MakePacket("192.168.0.2", "192.168.0.1", 3214, 22, 6, PENDING_PACKET::INCOMING, 0), 0));
216 tests.push_back(make_pair(MakePacket("192.168.0.1", "192.168.0.2", 22, 3214, 6, PENDING_PACKET::INCOMING, 0), 0));
218 tests.push_back(make_pair(MakePacket("192.168.0.2", "192.168.0.1", 3214, 20, 6, PENDING_PACKET::OUTGOING, 0), 0));
219 tests.push_back(make_pair(MakePacket("192.168.0.1", "192.168.0.2", 21, 3214, 6, PENDING_PACKET::OUTGOING, 0), 0));
221 tests.push_back(make_pair(MakePacket("192.168.0.2", "192.168.0.1", 3214, 53, 6, PENDING_PACKET::OUTGOING, 0), 0));
222 tests.push_back(make_pair(MakePacket("192.168.0.1", "192.168.0.2", 53, 3214, 6, PENDING_PACKET::OUTGOING, 0), 0));
224 tests.push_back(make_pair(MakePacket("192.168.0.2", "195.5.61.68", 3210, 53, 6, PENDING_PACKET::OUTGOING, 0), 1));
225 tests.push_back(make_pair(MakePacket("195.5.61.68", "192.168.0.2", 53, 3210, 6, PENDING_PACKET::INCOMING, 0), 1));
226 // Known DNS, invalid ports
227 tests.push_back(make_pair(MakePacket("192.168.0.2", "195.5.61.68", 3210, 54, 6, PENDING_PACKET::OUTGOING, 0), 3));
228 tests.push_back(make_pair(MakePacket("195.5.61.68", "192.168.0.2", 20, 3210, 6, PENDING_PACKET::INCOMING, 0), 3));
230 tests.push_back(make_pair(MakePacket("192.168.0.2", "129.22.8.159", 3241, 20, 6, PENDING_PACKET::OUTGOING, 0), 2));
231 tests.push_back(make_pair(MakePacket("129.22.8.159", "192.168.0.2", 21, 3241, 6, PENDING_PACKET::INCOMING, 0), 2));
232 // Known FTP, invalid ports
233 tests.push_back(make_pair(MakePacket("192.168.0.2", "129.22.8.159", 3241, 53, 6, PENDING_PACKET::OUTGOING, 0), 3));
234 tests.push_back(make_pair(MakePacket("129.22.8.159", "192.168.0.2", 22, 3241, 6, PENDING_PACKET::INCOMING, 0), 3));
236 std::for_each(tests.begin(),