blob: acc9bfe889cd0bafb5c4966b50cd2fb7e7c13847 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Dave Barach4bf88882019-08-21 09:11:41 -04002
3import unittest
4
5from framework import VppTestCase, VppTestRunner
6from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath
Dave Barach4bf88882019-08-21 09:11:41 -04007from ipaddress import *
8
9import scapy.compat
10from scapy.contrib.mpls import MPLS
11from scapy.layers.inet import IP, UDP, TCP, ICMP, icmptypes, icmpcodes
12from scapy.layers.l2 import Ether
13from scapy.packet import Raw
14from scapy.layers.dns import DNSRR, DNS, DNSQR
15
16
17class TestDns(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020018 """Dns Test Cases"""
Dave Barach4bf88882019-08-21 09:11:41 -040019
20 @classmethod
21 def setUpClass(cls):
22 super(TestDns, cls).setUpClass()
23
24 @classmethod
25 def tearDownClass(cls):
26 super(TestDns, cls).tearDownClass()
27
28 def setUp(self):
29 super(TestDns, self).setUp()
30
31 self.create_pg_interfaces(range(1))
32
33 for i in self.pg_interfaces:
34 i.admin_up()
35 i.config_ip4()
36 i.resolve_arp()
37
38 def tearDown(self):
39 super(TestDns, self).tearDown()
40
41 def create_stream(self, src_if):
42 """Create input packet stream for defined interface.
43
44 :param VppInterface src_if: Interface to create packet stream for.
45 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020046 good_request = (
47 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
48 / IP(src=src_if.remote_ip4)
49 / UDP(sport=1234, dport=53)
50 / DNS(rd=1, qd=DNSQR(qname="bozo.clown.org"))
51 )
Dave Barach4bf88882019-08-21 09:11:41 -040052
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020053 bad_request = (
54 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
55 / IP(src=src_if.remote_ip4)
56 / UDP(sport=1234, dport=53)
57 / DNS(rd=1, qd=DNSQR(qname="no.clown.org"))
58 )
Dave Barach4bf88882019-08-21 09:11:41 -040059 pkts = [good_request, bad_request]
60 return pkts
61
62 def verify_capture(self, dst_if, capture):
63 """Verify captured input packet stream for defined interface.
64
65 :param VppInterface dst_if: Interface to verify captured packet stream
66 for.
67 :param list capture: Captured packet stream.
68 """
69 self.logger.info("Verifying capture on interface %s" % dst_if.name)
70 for packet in capture:
71 dns = packet[DNS]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020072 self.assertEqual(dns.an[0].rdata, "1.2.3.4")
Dave Barach4bf88882019-08-21 09:11:41 -040073
74 def test_dns_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020075 """DNS Name Resolver Basic Functional Test"""
Dave Barach4bf88882019-08-21 09:11:41 -040076
77 # Set up an upstream name resolver. We won't actually go there
78 self.vapi.dns_name_server_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020079 is_ip6=0, is_add=1, server_address=IPv4Address("8.8.8.8").packed
80 )
Dave Barach4bf88882019-08-21 09:11:41 -040081
82 # Enable name resolution
83 self.vapi.dns_enable_disable(enable=1)
84
85 # Manually add a static dns cache entry
86 self.logger.info(self.vapi.cli("dns cache add bozo.clown.org 1.2.3.4"))
87
88 # Test the binary API
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020089 rv = self.vapi.dns_resolve_name(name=b"bozo.clown.org")
90 self.assertEqual(rv.ip4_address, IPv4Address("1.2.3.4").packed)
Dave Barach4bf88882019-08-21 09:11:41 -040091
92 # Configure 127.0.0.1/8 on the pg interface
93 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020094 sw_if_index=self.pg0.sw_if_index, prefix="127.0.0.1/8"
95 )
Dave Barach4bf88882019-08-21 09:11:41 -040096
97 # Send a couple of DNS request packets, one for bozo.clown.org
98 # and one for no.clown.org which won't resolve
99
100 pkts = self.create_stream(self.pg0)
101 self.pg0.add_stream(pkts)
102 self.pg_enable_capture(self.pg_interfaces)
103
104 self.pg_start()
105 pkts = self.pg0.get_capture(1)
106 self.verify_capture(self.pg0, pkts)
107
108 # Make sure that the cache contents are correct
109 str = self.vapi.cli("show dns cache verbose")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200110 self.assertIn("1.2.3.4", str)
111 self.assertIn("[P] no.clown.org:", str)
Dave Barach4bf88882019-08-21 09:11:41 -0400112
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200113
114if __name__ == "__main__":
Dave Barach4bf88882019-08-21 09:11:41 -0400115 unittest.main(testRunner=VppTestRunner)