CDN_DNS/tests/test-trie.py

51 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-06-05 19:53:31 +02:00
import random
from ipaddress import IPv6Network
def parse_rule(rule):
parts = rule.split()
prefix, next_hop = parts
2024-06-06 14:37:35 +02:00
return IPv6Network(prefix), int(next_hop)
2024-06-05 19:53:31 +02:00
def generate_random_test_cases(rules, n):
with open('../data/test-data', 'w') as f:
for rule in rules:
2024-06-06 14:37:35 +02:00
rule_network = rule[0]
2024-06-05 19:53:31 +02:00
rule_next_hop = rule[1]
for _ in range(n):
2024-06-09 15:09:04 +02:00
network = rule_network
2024-06-05 19:53:31 +02:00
next_hop = rule_next_hop
address = rule_network.network_address + random.randint(0, rule_network.num_addresses - 1)
for oth_rule in rules:
if oth_rule == rule:
continue
2024-06-06 14:37:35 +02:00
oth_network = oth_rule[0]
2024-06-05 19:53:31 +02:00
oth_next_hop = oth_rule[1]
2024-06-08 18:22:26 +02:00
if address in oth_network and oth_network.subnet_of( network ):
2024-06-06 14:37:35 +02:00
print(f"address {address} is closer to {oth_network} {rule_next_hop} than {rule_network} {oth_next_hop}")
2024-06-05 19:53:31 +02:00
next_hop = oth_next_hop
2024-06-08 18:22:26 +02:00
network = oth_network
2024-06-05 19:53:31 +02:00
f.write(f"{address} {next_hop}\n")
2024-06-06 14:37:35 +02:00
print(f"Generated test case: {address} {next_hop}")
print("tests for rule ", rule_network, " with next hop ", rule_next_hop, " generated")
2024-06-05 19:53:31 +02:00
2024-06-06 14:37:35 +02:00
def main():
2024-06-05 19:53:31 +02:00
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]
2024-06-06 14:37:35 +02:00
num_tests = 10
2024-06-05 19:53:31 +02:00
generate_random_test_cases(parsed_rules, num_tests)
if __name__ == "__main__":
main()