CDN_DNS/tests/test-radix.py

53 lines
1.7 KiB
Python
Raw Normal View History

2024-06-09 15:09:04 +02:00
import random
from ipaddress import IPv6Network
def parse_rule(rule):
parts = rule.split()
prefix, next_hop = parts
return IPv6Network(prefix), int(next_hop)
def generate_random_test_cases(rules, n):
with open('../data/test-data', 'w') as f:
for rule in rules:
rule_network = rule[0]
rule_next_hop = rule[1]
for _ in range(n):
ipaddress = rule_network.network_address + random.randint(0, rule_network.num_addresses - 1)
prefix_length = random.randint(rule_network.prefixlen, rule_network.max_prefixlen)
test_network = IPv6Network(ipaddress).supernet(new_prefix=prefix_length)
closest_rule = rule
for oth_rule in rules:
if oth_rule == rule:
continue
oth_network = oth_rule[0]
oth_next_hop = oth_rule[1]
if test_network.subnet_of(oth_network) and oth_network.subnet_of(closest_rule[0]):
print(f"{test_network} is closer to {oth_network} than {closest_rule[0]}")
closest_rule = oth_rule
f.write(f"{test_network} {closest_rule[1]}\n")
print(f"Generated test case: {test_network} {closest_rule[1]}")
print("Tests for rule", rule_network, "with next hop", rule_next_hop, "generated")
def main():
rules = []
with open('../data/routing-data', 'r') as f:
for line in f:
rules.append(line.strip())
parsed_rules = [parse_rule(rule) for rule in rules]
num_tests = 10
generate_random_test_cases(parsed_rules, num_tests)
if __name__ == "__main__":
main()