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:
|
2024-06-10 11:00:30 +02:00
|
|
|
for i, rule in enumerate(rules):
|
2024-06-09 15:09:04 +02:00
|
|
|
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
|
|
|
|
|
2024-06-10 10:06:23 +02:00
|
|
|
f.write(f"{test_network.supernet(new_prefix=closest_rule[0].prefixlen)} {closest_rule[1]}\n")
|
|
|
|
# print(f"Generated test case: {test_network} {closest_rule[1]}")
|
2024-06-10 11:00:30 +02:00
|
|
|
percentage_completed = (i + 1) / len(rules) * 100
|
|
|
|
print(f"[{percentage_completed:.2f}%]Tests for rule {rule_network} {rule_next_hop} generated")
|
2024-06-09 15:09:04 +02:00
|
|
|
|
|
|
|
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()
|