diff --git a/tests/data/base_file.csv b/tests/data/base_file.csv new file mode 100644 index 0000000..5339ea1 --- /dev/null +++ b/tests/data/base_file.csv @@ -0,0 +1,20 @@ +network,is_valid,is_subnet_of_extra +127.0.0.0/17,FALSE,FALSE +0.128.0.0/10,TRUE,FALSE +206.0.0.0/8,TRUE,FALSE +105.231.36.0/23,TRUE,FALSE +108.232.0.0/13,TRUE,FALSE +110.204.32.0/21,TRUE,FALSE +113.53.128.0/17,TRUE,FALSE +121.128.0.0/9,TRUE,FALSE +122.64.0.0/11,TRUE,FALSE +131.128.0.0/9,TRUE,FALSE +138.232.0.0/13,TRUE,FALSE +140.210.168.0/22,TRUE,FALSE +145.224.0.0/13,TRUE,FALSE +152.128.0.0/9,TRUE,FALSE +162.55.64.0/18,TRUE,FALSE +165.128.0.0/13,TRUE,FALSE +174.160.0.0/11,TRUE,FALSE +109.92.213.0/24,TRUE,FALSE +244.20.0.0/14,TRUE,FALSE diff --git a/tests/data/extra_file.csv b/tests/data/extra_file.csv new file mode 100644 index 0000000..0ff7399 --- /dev/null +++ b/tests/data/extra_file.csv @@ -0,0 +1,21 @@ +network,is_valid,is_subnet_of_base +2.11.76.0/22,TRUE,FALSE +115.7.192.0/19,TRUE,FALSE +12.132.80.0/20,TRUE,FALSE +82.64.0.0/11,TRUE,FALSE +194.48.0.0/17,TRUE,FALSE +197.216.136.0/21,TRUE,FALSE +249.0.0.0/9,TRUE,FALSE +32.17.64.0/20,TRUE,FALSE +38.66.0.0/17,TRUE,FALSE +40.240.124.0/22,TRUE,FALSE +44.26.0.0/19,TRUE,FALSE +46.99.64.0/20,TRUE,FALSE +55.48.0.0/15,TRUE,FALSE +74.0.0.0/10,TRUE,FALSE +77.120.136.0/22,TRUE,FALSE +131.128.58.0/23,TRUE,TRUE +244.20.240.0/20,TRUE,TRUE +108.232.0.0/16,TRUE,TRUE +122.68.0.0/14,TRUE,TRUE +121.128.0.0/10,TRUE,TRUE diff --git a/tests/merge_test.py b/tests/merge_test.py index 73254ca..796498e 100644 --- a/tests/merge_test.py +++ b/tests/merge_test.py @@ -1,6 +1,9 @@ +""" +Test merging multiple sets of networks, as if they were independent AS files. +""" from kartograf.merge import general_merge -from .generate_data import ( +from .util.generate_data import ( build_file_lines, generate_ip_file, generate_file_items, @@ -13,6 +16,45 @@ def __tmp_paths(tmp_path): return [tmp_path / p for p in ["rpki_final.txt", "irr_final.txt", "out.txt"]] +def __read_test_vectors(filepath): + """ + Fixtures for IP networks are under tests/data. + Read them and return the list of valid networks and the count of individual subnets expected in the result of the merge. + """ + networks = [] + subnet_count = 0 + with open(filepath, "r") as f: + lines = f.readlines()[1:] + for line in lines: + network, is_valid, is_subnet = line.split(',') + if is_valid == "TRUE": + networks.append(network) + if is_subnet.strip() == "TRUE": + subnet_count += 1 + return networks, subnet_count + +def test_merge_from_fixtures(tmp_path): + ''' + Assert that general_merge merges subnets correctly. + ''' + + base_nets, base_subnet_count = __read_test_vectors("tests/data/base_file.csv") + base_path = tmp_path / "base.txt" + extra_nets, extra_subnet_count = __read_test_vectors("tests/data/extra_file.csv") + extra_path = tmp_path / "extra.txt" + # write the networks to disk, generating ASNs for each network + generate_ip_file(base_path, build_file_lines(base_nets, generate_asns(len(base_nets)))) + generate_ip_file(extra_path, build_file_lines(extra_nets, generate_asns(len(extra_nets)))) + + outpath = tmp_path / "final_result.txt" + general_merge(base_path, extra_path, None, outpath) + with open(outpath, "r") as f: + l = f.readlines() + final_network_count = len(l) + expected_count = (len(base_nets) + len(extra_nets)) - (base_subnet_count + extra_subnet_count) + assert final_network_count == expected_count + + def test_merge(tmp_path): """ Assert that merging two identical files is a no-op. diff --git a/tests/generate_data.py b/tests/util/generate_data.py similarity index 100% rename from tests/generate_data.py rename to tests/util/generate_data.py diff --git a/tests/test_util.py b/tests/util_test.py similarity index 100% rename from tests/test_util.py rename to tests/util_test.py