Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 2 | |
| 3 | import unittest |
| 4 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 5 | from framework import VppTestCase |
| 6 | from asfframework import VppTestRunner |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 7 | from ipaddress import * |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 8 | from config import config |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 9 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 10 | from scapy.layers.inet import IP, UDP |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 11 | from scapy.layers.l2 import Ether |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 12 | from scapy.layers.dns import DNS, DNSQR |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 13 | |
| 14 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 15 | @unittest.skipIf("dns" in config.excluded_plugins, "Exclude DNS plugin tests") |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 16 | class TestDns(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 17 | """Dns Test Cases""" |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 18 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 45 | 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 Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 51 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 52 | 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 Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 58 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 71 | self.assertEqual(dns.an[0].rdata, "1.2.3.4") |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 72 | |
| 73 | def test_dns_unittest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 74 | """DNS Name Resolver Basic Functional Test""" |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 75 | |
| 76 | # Set up an upstream name resolver. We won't actually go there |
| 77 | self.vapi.dns_name_server_add_del( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 78 | is_ip6=0, is_add=1, server_address=IPv4Address("8.8.8.8").packed |
| 79 | ) |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 80 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 88 | rv = self.vapi.dns_resolve_name(name=b"bozo.clown.org") |
| 89 | self.assertEqual(rv.ip4_address, IPv4Address("1.2.3.4").packed) |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 90 | |
| 91 | # Configure 127.0.0.1/8 on the pg interface |
| 92 | self.vapi.sw_interface_add_del_address( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 93 | sw_if_index=self.pg0.sw_if_index, prefix="127.0.0.1/8" |
| 94 | ) |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 95 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 109 | self.assertIn("1.2.3.4", str) |
| 110 | self.assertIn("[P] no.clown.org:", str) |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 111 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 112 | |
| 113 | if __name__ == "__main__": |
Dave Barach | 4bf8888 | 2019-08-21 09:11:41 -0400 | [diff] [blame] | 114 | unittest.main(testRunner=VppTestRunner) |