blob: 77f26d71eaa83bedd05f059c0420b27d2770d41d [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
Dave Wallace8800f732023-08-31 00:47:44 -04005from framework import VppTestCase
6from asfframework import VppTestRunner
Dave Barach4bf88882019-08-21 09:11:41 -04007from ipaddress import *
Dmitry Valter34fa0ce2024-03-11 10:38:46 +00008from config import config
Dave Barach4bf88882019-08-21 09:11:41 -04009
Dave Wallace8800f732023-08-31 00:47:44 -040010from scapy.layers.inet import IP, UDP
Dave Barach4bf88882019-08-21 09:11:41 -040011from scapy.layers.l2 import Ether
Dave Wallace8800f732023-08-31 00:47:44 -040012from scapy.layers.dns import DNS, DNSQR
Dave Barach4bf88882019-08-21 09:11:41 -040013
14
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000015@unittest.skipIf("dns" in config.excluded_plugins, "Exclude DNS plugin tests")
Dave Barach4bf88882019-08-21 09:11:41 -040016class TestDns(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020017 """Dns Test Cases"""
Dave Barach4bf88882019-08-21 09:11:41 -040018
19 @classmethod
20 def setUpClass(cls):
21 super(TestDns, cls).setUpClass()
22
23 @classmethod
24 def tearDownClass(cls):
25 super(TestDns, cls).tearDownClass()
26
27 def setUp(self):
28 super(TestDns, self).setUp()
29
30 self.create_pg_interfaces(range(1))
31
32 for i in self.pg_interfaces:
33 i.admin_up()
34 i.config_ip4()
35 i.resolve_arp()
36
37 def tearDown(self):
38 super(TestDns, self).tearDown()
39
40 def create_stream(self, src_if):
41 """Create input packet stream for defined interface.
42
43 :param VppInterface src_if: Interface to create packet stream for.
44 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020045 good_request = (
46 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
47 / IP(src=src_if.remote_ip4)
48 / UDP(sport=1234, dport=53)
49 / DNS(rd=1, qd=DNSQR(qname="bozo.clown.org"))
50 )
Dave Barach4bf88882019-08-21 09:11:41 -040051
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052 bad_request = (
53 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
54 / IP(src=src_if.remote_ip4)
55 / UDP(sport=1234, dport=53)
56 / DNS(rd=1, qd=DNSQR(qname="no.clown.org"))
57 )
Dave Barach4bf88882019-08-21 09:11:41 -040058 pkts = [good_request, bad_request]
59 return pkts
60
61 def verify_capture(self, dst_if, capture):
62 """Verify captured input packet stream for defined interface.
63
64 :param VppInterface dst_if: Interface to verify captured packet stream
65 for.
66 :param list capture: Captured packet stream.
67 """
68 self.logger.info("Verifying capture on interface %s" % dst_if.name)
69 for packet in capture:
70 dns = packet[DNS]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020071 self.assertEqual(dns.an[0].rdata, "1.2.3.4")
Dave Barach4bf88882019-08-21 09:11:41 -040072
73 def test_dns_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020074 """DNS Name Resolver Basic Functional Test"""
Dave Barach4bf88882019-08-21 09:11:41 -040075
76 # Set up an upstream name resolver. We won't actually go there
77 self.vapi.dns_name_server_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020078 is_ip6=0, is_add=1, server_address=IPv4Address("8.8.8.8").packed
79 )
Dave Barach4bf88882019-08-21 09:11:41 -040080
81 # Enable name resolution
82 self.vapi.dns_enable_disable(enable=1)
83
84 # Manually add a static dns cache entry
85 self.logger.info(self.vapi.cli("dns cache add bozo.clown.org 1.2.3.4"))
86
87 # Test the binary API
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 rv = self.vapi.dns_resolve_name(name=b"bozo.clown.org")
89 self.assertEqual(rv.ip4_address, IPv4Address("1.2.3.4").packed)
Dave Barach4bf88882019-08-21 09:11:41 -040090
91 # Configure 127.0.0.1/8 on the pg interface
92 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020093 sw_if_index=self.pg0.sw_if_index, prefix="127.0.0.1/8"
94 )
Dave Barach4bf88882019-08-21 09:11:41 -040095
96 # Send a couple of DNS request packets, one for bozo.clown.org
97 # and one for no.clown.org which won't resolve
98
99 pkts = self.create_stream(self.pg0)
100 self.pg0.add_stream(pkts)
101 self.pg_enable_capture(self.pg_interfaces)
102
103 self.pg_start()
104 pkts = self.pg0.get_capture(1)
105 self.verify_capture(self.pg0, pkts)
106
107 # Make sure that the cache contents are correct
108 str = self.vapi.cli("show dns cache verbose")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200109 self.assertIn("1.2.3.4", str)
110 self.assertIn("[P] no.clown.org:", str)
Dave Barach4bf88882019-08-21 09:11:41 -0400111
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200112
113if __name__ == "__main__":
Dave Barach4bf88882019-08-21 09:11:41 -0400114 unittest.main(testRunner=VppTestRunner)