Debian Codegraph @

tests/src/hop.cxx

raw
1 #include <dublintraceroute/hop.h>
2 #include <dublintraceroute/exceptions.h>
3 #include <gtest/gtest.h>
4 #include <tins/tins.h>
5
6
7 using namespace Tins;
8
9
10 namespace {
11
12 class HopTest: public ::testing::Test {
13 };
14
15 TEST_F(HopTest, TestHopConstructor) {
16 Hop h = Hop();
17 ASSERT_EQ(h.is_last_hop(), false);
18 ASSERT_EQ(h.sent(), nullptr);
19 ASSERT_EQ(h.received(), nullptr);
20 ASSERT_EQ(h.name(), std::string());
21 ASSERT_EQ(h.sent_timestamp(), nullptr);
22 ASSERT_EQ(h.received_timestamp(), nullptr);
23 ASSERT_THROW(h.nat_id(), DublinTracerouteException);
24 ASSERT_THROW(h.zerottl_forwarding_bug(), DublinTracerouteException);
25 ASSERT_EQ(h.flowhash(), 0);
26 ASSERT_EQ(h.rtt(), 0);
27 ASSERT_EQ((bool)h, false);
28 }
29
30 TEST_F(HopTest, TestHopSentPacketIPOnly) {
31 Hop h = Hop();
32 IP ip = IP("8.8.8.8", "0.0.0.0");
33 ip.ttl(32);
34 h.sent(ip);
35 ASSERT_EQ(h.sent()->dst_addr(), "8.8.8.8");
36 ASSERT_EQ(h.sent()->src_addr(), "0.0.0.0");
37 ASSERT_EQ(h.sent()->ttl(), 32);
38 ASSERT_EQ(h.flowhash(), 0); // need UDP layer to have a flow hash
39 }
40
41 TEST_F(HopTest, TestHopSentPacketIPUDP) {
42 Hop h = Hop();
43 IP ip = IP("8.8.8.8", "0.0.0.0") / UDP(33435, 12344);
44 ip.ttl(16);
45 h.sent(ip);
46 ASSERT_EQ(h.sent()->dst_addr(), "8.8.8.8");
47 ASSERT_EQ(h.sent()->src_addr(), "0.0.0.0");
48 ASSERT_EQ(h.sent()->ttl(), 16);
49 ASSERT_EQ(h.flowhash(), 47835);
50 }
51
52 TEST_F(HopTest, TestHopReceivedPacketIPOnly) {
53 Hop h = Hop();
54 IP ip = IP("8.8.8.8", "0.0.0.0");
55 auto now = Tins::Timestamp::current_time();
56 h.sent_timestamp(now);
57
58 struct timeval tv;
59 tv.tv_sec = now.seconds() + 2;
60 tv.tv_usec = now.microseconds();
61 auto then = Tins::Timestamp(tv);
62 h.received(ip, then);
63 ASSERT_THROW(h.nat_id(), Tins::pdu_not_found);
64 ASSERT_THROW(h.zerottl_forwarding_bug(), Tins::pdu_not_found);
65 ASSERT_EQ(h.rtt(), 2000000);
66 }
67
68 // TODO test resolver, summary and JSON representation
69
70 }
71
72
73 int main(int argc, char **argv) {
74 ::testing::InitGoogleTest(&argc, argv);
75 std::exit(RUN_ALL_TESTS());
76 }
77