From f64503c1c6086c04284d3c5037ecc21fe92e3537 Mon Sep 17 00:00:00 2001 From: hladu357 Date: Sun, 9 Jun 2024 15:09:04 +0200 Subject: [PATCH] new test generator --- tests/test-radix.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ tests/test-trie.py | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/test-radix.py diff --git a/tests/test-radix.py b/tests/test-radix.py new file mode 100644 index 0000000..f186e13 --- /dev/null +++ b/tests/test-radix.py @@ -0,0 +1,52 @@ +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() diff --git a/tests/test-trie.py b/tests/test-trie.py index 590d256..eadbdc7 100644 --- a/tests/test-trie.py +++ b/tests/test-trie.py @@ -14,8 +14,8 @@ def generate_random_test_cases(rules, n): rule_network = rule[0] rule_next_hop = rule[1] - network = rule_network for _ in range(n): + network = rule_network next_hop = rule_next_hop address = rule_network.network_address + random.randint(0, rule_network.num_addresses - 1)