From c9265ce4df1ef475ded8ae57d81b1cb93c1bf759 Mon Sep 17 00:00:00 2001 From: snkas Date: Tue, 13 Oct 2020 21:44:10 +0200 Subject: [PATCH] Improve ISL reading checks --- .../dynamic_state/helper_dynamic_state.py | 2 +- satgenpy/satgen/isls/read_isls.py | 27 +++++++- satgenpy/satgen/post_analysis/analyze_rtt.py | 2 +- .../print_graphical_routes_and_rtt.py | 2 +- .../post_analysis/print_routes_and_rtt.py | 2 +- satgenpy/tests/test_isls.py | 68 ++++++++++++++++++- 6 files changed, 94 insertions(+), 9 deletions(-) diff --git a/satgenpy/satgen/dynamic_state/helper_dynamic_state.py b/satgenpy/satgen/dynamic_state/helper_dynamic_state.py index 33d165d3e..a9f7f6710 100644 --- a/satgenpy/satgen/dynamic_state/helper_dynamic_state.py +++ b/satgenpy/satgen/dynamic_state/helper_dynamic_state.py @@ -103,8 +103,8 @@ def help_dynamic_state( # Variables (load in for each thread such that they don't interfere) ground_stations = read_ground_stations_extended(output_generated_data_dir + "/" + name + "/ground_stations.txt") tles = read_tles(output_generated_data_dir + "/" + name + "/tles.txt") - list_isls = read_isls(output_generated_data_dir + "/" + name + "/isls.txt") satellites = tles["satellites"] + list_isls = read_isls(output_generated_data_dir + "/" + name + "/isls.txt", len(satellites)) list_gsl_interfaces_info = read_gsl_interfaces_info( output_generated_data_dir + "/" + name + "/gsl_interfaces_info.txt", len(satellites), diff --git a/satgenpy/satgen/isls/read_isls.py b/satgenpy/satgen/isls/read_isls.py index 023821fe2..0fcb5a13a 100644 --- a/satgenpy/satgen/isls/read_isls.py +++ b/satgenpy/satgen/isls/read_isls.py @@ -20,17 +20,38 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from exputil import parse_positive_int -def read_isls(filename_isls): + +def read_isls(filename_isls, num_satellites): """ Read ISLs file into a list of undirected edges - :param filename_isls: Filename of ISLs (typically /path/to/isls.txt) + :param filename_isls: Filename of ISLs (typically /path/to/isls.txt) + :param num_satellites: Number of satellites (to verify indices) :return: List of all undirected ISL edges """ isls_list = [] with open(filename_isls, 'r') as f: + isls_set = set() for line in f: - isls_list.append((int(line.split()[0]), int(line.split()[1]))) + line_spl = line.split() + a = parse_positive_int(line_spl[0]) + b = parse_positive_int(line_spl[1]) + + # Verify the input + if a >= num_satellites: + raise ValueError("Satellite does not exist: %d" % a) + if b >= num_satellites: + raise ValueError("Satellite does not exist: %d" % b) + if b < a: + raise ValueError("The second satellite index must be higher than the first") + if (a, b) in isls_set: + raise ValueError("Duplicate ISL: (%d, %d)" % (a, b)) + isls_set.add((a, b)) + + # Finally add it to the list + isls_list.append((a, b)) + return isls_list diff --git a/satgenpy/satgen/post_analysis/analyze_rtt.py b/satgenpy/satgen/post_analysis/analyze_rtt.py index 8bbdbf9f1..8f11a2c61 100644 --- a/satgenpy/satgen/post_analysis/analyze_rtt.py +++ b/satgenpy/satgen/post_analysis/analyze_rtt.py @@ -62,8 +62,8 @@ def analyze_rtt( # Variables (load in for each thread such that they don't interfere) ground_stations = read_ground_stations_extended(satellite_network_dir + "/ground_stations.txt") tles = read_tles(satellite_network_dir + "/tles.txt") - list_isls = read_isls(satellite_network_dir + "/isls.txt") satellites = tles["satellites"] + list_isls = read_isls(satellite_network_dir + "/isls.txt", len(satellites)) epoch = tles["epoch"] description = exputil.PropertiesConfig(satellite_network_dir + "/description.txt") diff --git a/satgenpy/satgen/post_analysis/print_graphical_routes_and_rtt.py b/satgenpy/satgen/post_analysis/print_graphical_routes_and_rtt.py index 0951736a0..3ce739208 100644 --- a/satgenpy/satgen/post_analysis/print_graphical_routes_and_rtt.py +++ b/satgenpy/satgen/post_analysis/print_graphical_routes_and_rtt.py @@ -61,8 +61,8 @@ def print_graphical_routes_and_rtt( # Variables (load in for each thread such that they don't interfere) ground_stations = read_ground_stations_extended(satellite_network_dir + "/ground_stations.txt") tles = read_tles(satellite_network_dir + "/tles.txt") - list_isls = read_isls(satellite_network_dir + "/isls.txt") satellites = tles["satellites"] + list_isls = read_isls(satellite_network_dir + "/isls.txt", len(satellites)) epoch = tles["epoch"] description = exputil.PropertiesConfig(satellite_network_dir + "/description.txt") diff --git a/satgenpy/satgen/post_analysis/print_routes_and_rtt.py b/satgenpy/satgen/post_analysis/print_routes_and_rtt.py index fb278e7b2..fce2fa517 100644 --- a/satgenpy/satgen/post_analysis/print_routes_and_rtt.py +++ b/satgenpy/satgen/post_analysis/print_routes_and_rtt.py @@ -48,8 +48,8 @@ def print_routes_and_rtt(base_output_dir, satellite_network_dir, dynamic_state_u # Variables (load in for each thread such that they don't interfere) ground_stations = read_ground_stations_extended(satellite_network_dir + "/ground_stations.txt") tles = read_tles(satellite_network_dir + "/tles.txt") - list_isls = read_isls(satellite_network_dir + "/isls.txt") satellites = tles["satellites"] + list_isls = read_isls(satellite_network_dir + "/isls.txt", len(satellites)) epoch = tles["epoch"] description = exputil.PropertiesConfig(satellite_network_dir + "/description.txt") diff --git a/satgenpy/tests/test_isls.py b/satgenpy/tests/test_isls.py index 8a1750298..7f4f5c00c 100644 --- a/satgenpy/tests/test_isls.py +++ b/satgenpy/tests/test_isls.py @@ -24,13 +24,14 @@ import unittest from math import floor import os +import exputil class TestIsls(unittest.TestCase): def test_isls_empty(self): satgen.generate_empty_isls("isls_empty.txt.tmp") - isls_list = satgen.read_isls("isls_empty.txt.tmp") + isls_list = satgen.read_isls("isls_empty.txt.tmp", 0) self.assertEqual(0, len(isls_list)) os.remove("isls_empty.txt.tmp") @@ -40,7 +41,7 @@ def test_isls_plus_grid(self): num_sat_per_orbit = values[1] isl_shift = values[2] satgen.generate_plus_grid_isls("isls.txt.tmp", num_orbits, num_sat_per_orbit, isl_shift) - isls_list = satgen.read_isls("isls.txt.tmp") + isls_list = satgen.read_isls("isls.txt.tmp", num_orbits * num_sat_per_orbit) os.remove("isls.txt.tmp") self.assertEqual(len(isls_list), num_orbits * num_sat_per_orbit * 2) self.assertEqual(len(set(isls_list)), num_orbits * num_sat_per_orbit * 2) @@ -85,3 +86,66 @@ def test_isls_plus_grid_invalid(self): self.fail() except ValueError: self.assertTrue(True) + + def test_isls_invalid_file(self): + local_shell = exputil.LocalShell() + + # Invalid left index + local_shell.write_file( + "isls.txt.tmp", + "2 3\n5 6\n9 0" + ) + try: + satgen.read_isls("isls.txt.tmp", 9) + self.fail() + except ValueError: + self.assertTrue(True) + os.remove("isls.txt.tmp") + + # Invalid right index + local_shell.write_file( + "isls.txt.tmp", + "2 3\n5 6\n6 9\n3 99" + ) + try: + satgen.read_isls("isls.txt.tmp", 50) + self.fail() + except ValueError: + self.assertTrue(True) + os.remove("isls.txt.tmp") + + # Invalid left index + local_shell.write_file( + "isls.txt.tmp", + "2 3\n5 6\n6 8\n-3 3" + ) + try: + satgen.read_isls("isls.txt.tmp", 50) + self.fail() + except ValueError: + self.assertTrue(True) + os.remove("isls.txt.tmp") + + # Invalid right index + local_shell.write_file( + "isls.txt.tmp", + "2 3\n5 6\n1 -3\n6 8" + ) + try: + satgen.read_isls("isls.txt.tmp", 50) + self.fail() + except ValueError: + self.assertTrue(True) + os.remove("isls.txt.tmp") + + # Duplicate + local_shell.write_file( + "isls.txt.tmp", + "2 3\n5 6\n3 9\n5 6\n2 9" + ) + try: + satgen.read_isls("isls.txt.tmp", 10) + self.fail() + except ValueError: + self.assertTrue(True) + os.remove("isls.txt.tmp")