Skip to content
Extraits de code Groupes Projets
Valider 3c582c88 rédigé par François De Keersmaeker's avatar François De Keersmaeker
Parcourir les fichiers

Added unit test with a sample DNS cache file

parent 34999d96
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
.vscode .vscode
.venv .venv
__pycache__ __pycache__
*.txt
test.py test.py
from .dns_unbound_cache_reader import read_dns_cache from .dns_unbound_cache_reader import (
DnsCacheSection,
DnsRtype,
DnsTableKeys,
read_dns_cache
)
...@@ -22,7 +22,7 @@ to_skip = ( ...@@ -22,7 +22,7 @@ to_skip = (
"EOF" "EOF"
) )
# Regex patterns # Regex patterns
pattern_line = r"^([a-zA-Z0-9.-]+)\s+(\d+)\s+IN\s+([A-Z]+)\s+(.+)$" # Generic DNS cache line pattern_line = r"^([a-zA-Z0-9._-]+)\s+(\d+)\s+IN\s+([A-Z]+)\s+(.+)$" # Generic DNS cache line
pattern_ipv4_byte = r"(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])" # Single byte from an IPv4 address pattern_ipv4_byte = r"(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])" # Single byte from an IPv4 address
pattern_ptr = (pattern_ipv4_byte + r"\.") * 3 + pattern_ipv4_byte + r".in-addr.arpa" # Reverse DNS lookup qname pattern_ptr = (pattern_ipv4_byte + r"\.") * 3 + pattern_ipv4_byte + r".in-addr.arpa" # Reverse DNS lookup qname
pattern_srv = r"^(\d+)\s+(\d+)\s+(\d+)\s+([a-zA-Z0-9.-]+)$" # SRV record target pattern_srv = r"^(\d+)\s+(\d+)\s+(\d+)\s+([a-zA-Z0-9.-]+)$" # SRV record target
...@@ -41,7 +41,6 @@ class DnsRtype(IntEnum): ...@@ -41,7 +41,6 @@ class DnsRtype(IntEnum):
Enum class for the DNS resource record types. Enum class for the DNS resource record types.
""" """
A = 1 # IPv4 address A = 1 # IPv4 address
NS = 2 # Name server
PTR = 12 # Domain name pointer PTR = 12 # Domain name pointer
AAAA = 28 # IPv6 address AAAA = 28 # IPv6 address
SRV = 33 # Service locator SRV = 33 # Service locator
......
# Exception: sample text files for unit testing
!*.txt
"""
Unit tests for the package `dns_unbound_cache_reader`.
"""
; unbound cache dump
START_RRSET_CACHE
example.com. 300 IN A 93.184.216.34
example.com. 300 IN AAAA 2606:2800:220:1:248:1893:25c8:1946
example.org. 600 IN MX 10 mail.example.org.
example.org. 600 IN NS ns1.example.org.
example.org. 600 IN NS ns2.example.org.
ns1.example.org. 3600 IN A 192.0.2.1
ns2.example.org. 3600 IN A 198.51.100.1
1.2.0.192.in-addr.arpa. 3600 IN PTR example.com
2.2.0.192.in-addr.arpa. 3600 IN PTR example.com
_tcp_.matter.example.com. 3600 IN SRV 10 60 5000 server1.example.com
_tcp_.matter.example.com. 3600 IN SRV 20 60 5000 server2.example.com
END_RRSET_CACHE
EOF
# Libraries
import os
# Package under test
import dns_unbound_cache_reader as dns_reader
from dns_unbound_cache_reader import DnsTableKeys
# Variables
self_path = os.path.abspath(__file__)
self_dir = os.path.dirname(self_path)
sample_cache_file = os.path.join(self_dir, "sample_dns_cache.txt")
### TEST FUNCTIONS ###
def test_read_sample_cache_file() -> None:
"""
Test reading a sample cache file.
"""
dns_table = dns_reader.read_dns_cache(file=sample_cache_file)
assert DnsTableKeys.IP.name in dns_table
assert DnsTableKeys.SERVICE.name in dns_table
dns_table_ip = dns_table[DnsTableKeys.IP.name]
assert len(dns_table_ip) == 5
assert dns_table_ip["93.184.216.34"] == "example.com"
assert dns_table_ip["2606:2800:220:1:248:1893:25c8:1946"] == "example.com"
assert dns_table_ip["192.0.2.1"] == "ns1.example.org"
assert dns_table_ip["198.51.100.1"] == "ns2.example.org"
assert dns_table_ip["192.0.2.2"] == "example.com"
dns_table_service = dns_table[DnsTableKeys.SERVICE.name]
assert len(dns_table_service) == 2
assert dns_table_service["server1.example.com"] == "_tcp_.matter.example.com"
assert dns_table_service["server2.example.com"] == "_tcp_.matter.example.com"
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter