Debian Codegraph @

tests/src/udpv4.cxx

raw
1 #include <dublintraceroute/udpv4probe.h>
2 #include <gtest/gtest.h>
3 #include <tins/tins.h>
4
5
6 using namespace Tins;
7
8
9 namespace {
10
11 class UDPv4Test: public ::testing::Test {
12 };
13
14 TEST_F(UDPv4Test, TestUDPv4Constructor) {
15 UDPv4Probe p = UDPv4Probe(IPv4Address("8.8.8.8"), 33434, 12345, 64, IPv4Address("127.0.0.2"));
16 ASSERT_EQ(p.local_port(), 12345);
17 ASSERT_EQ(p.remote_port(), 33434);
18 ASSERT_EQ(p.ttl(), 64);
19 ASSERT_EQ(p.remote_addr().to_string(), std::string("8.8.8.8"));
20 ASSERT_EQ(p.local_addr().to_string(), std::string("127.0.0.2"));
21 }
22
23 TEST_F(UDPv4Test, TestUDPv4ConstructorDefaultLocalAddr) {
24 UDPv4Probe p = UDPv4Probe(IPv4Address("8.8.8.8"), 33434, 12345, 64);
25 ASSERT_EQ(p.local_port(), 12345);
26 ASSERT_EQ(p.remote_port(), 33434);
27 ASSERT_EQ(p.ttl(), 64);
28 ASSERT_EQ(p.remote_addr().to_string(), std::string("8.8.8.8"));
29 ASSERT_EQ(p.local_addr().to_string(), std::string("0.0.0.0"));
30 }
31
32 TEST_F(UDPv4Test, TestUDPv4PacketForging) {
33 UDPv4Probe p = UDPv4Probe(IPv4Address("127.0.0.3"), 33434, 12345, 64, IPv4Address("127.0.0.2"));
34 IP* ip = p.forge();
35 ASSERT_EQ(ip->tos(), 0);
36 ASSERT_EQ(ip->id(), 60794);
37 ASSERT_EQ(ip->flags(), Tins::IP::Flags::DONT_FRAGMENT);
38 ASSERT_EQ(ip->ttl(), 64);
39 ASSERT_EQ(ip->dst_addr().to_string(), std::string("127.0.0.3"));
40 ASSERT_EQ(ip->src_addr().to_string(), std::string("127.0.0.2"));
41 delete ip;
42 }
43
44 TEST_F(UDPv4Test, TestUDPv4PacketForgingDefaultLocalAddr) {
45 UDPv4Probe p = UDPv4Probe(IPv4Address("8.8.8.8"), 33434, 12345, 64);
46 IP* ip = p.forge();
47 ASSERT_EQ(ip->tos(), 0);
48 ASSERT_EQ(ip->flags(), Tins::IP::Flags::DONT_FRAGMENT);
49 ASSERT_EQ(ip->ttl(), 64);
50 ASSERT_EQ(ip->dst_addr().to_string(), std::string("8.8.8.8"));
51 // not testing src_addr and IP ID because the default addr depends on
52 // the actual network interface's configuration
53 delete ip;
54 }
55
56 }
57
58
59 int main(int argc, char **argv) {
60 ::testing::InitGoogleTest(&argc, argv);
61 std::exit(RUN_ALL_TESTS());
62 }
63