def test_change_setting(self, _: Any) -> None: settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True) self.assert_se("Error: Invalid setting value 'Falsee'.", settings.change_setting, 'serial_baudrate', 'Falsee') self.assert_se( "Error: Invalid setting value '1.1'.", settings.change_setting, 'serial_baudrate', '1.1', ) self.assert_se("Error: Invalid setting value '18446744073709551616'.", settings.change_setting, 'serial_baudrate', str(2**64)) self.assert_se("Error: Invalid setting value 'Falsee'.", settings.change_setting, 'use_serial_usb_adapter', 'Falsee') self.assertIsNone(settings.change_setting('serial_baudrate', '9600')) self.assertEqual( GatewaySettings(operation=TX, local_test=True, dd_sockets=True).serial_baudrate, 9600) settings.serial_baudrate = b'bytestring' with self.assertRaises(SystemExit): settings.change_setting('serial_baudrate', '9600')
def test_store_and_load_of_settings(self) -> None: settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=False) self.assertTrue(os.path.isfile(f'{DIR_USER_DATA}/{TX}_serial_settings.json')) self.assertEqual(settings.serial_baudrate, 19200) self.assertEqual(settings.use_serial_usb_adapter, True) settings.serial_baudrate = 115200 settings.use_serial_usb_adapter = False self.assertIsNone(settings.store_settings()) settings2 = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=False) self.assertEqual(settings2.serial_baudrate, 115200) self.assertEqual(settings.use_serial_usb_adapter, False)
def test_too_narrow_terminal_raises_fr_when_printing_settings( self, _: Any) -> None: settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True) self.assert_se("Error: Screen width is too small.", settings.print_settings)
def test_unknown_kv_pair_is_removed(self) -> None: # Setup ensure_dir(DIR_USER_DATA) with open(f"{DIR_USER_DATA}{TX}_serial_settings.json", 'w+') as f: f.write("""\ { "serial_baudrate": 19200, "serial_error_correction": 5, "use_serial_usb_adapter": true, "built_in_serial_interface": "ttyS0", "this_should_not_be_here": 1 }""") # Test settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True) self.assertEqual(settings.serial_baudrate, 19200) self.assertEqual(settings.serial_error_correction, 5) self.assertEqual(settings.use_serial_usb_adapter, True) self.assertEqual(settings.built_in_serial_interface, 'ttyS0') with open(settings.file_name) as f: data = f.read() self.assertEqual(data, self.default_serialized)
def test_invalid_type_is_replaced_with_default(self) -> None: # Setup ensure_dir(DIR_USER_DATA) with open(f"{DIR_USER_DATA}{TX}_serial_settings.json", 'w+') as f: f.write("""\ { "serial_baudrate": "115200", "serial_error_correction": "5", "use_serial_usb_adapter": "true", "built_in_serial_interface": true }""") # Test settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=False) self.assertEqual(settings.serial_baudrate, 19200) self.assertEqual(settings.serial_error_correction, 5) self.assertEqual(settings.use_serial_usb_adapter, True) self.assertEqual(settings.built_in_serial_interface, 'ttyS0') with open(settings.file_name) as f: data = f.read() self.assertEqual(data, self.default_serialized)
def test_setup(self, *_: Any) -> None: # Setup ensure_dir(DIR_USER_DATA) with open(f"{DIR_USER_DATA}{TX}_serial_settings.json", 'w+') as f: f.write(self.default_serialized) settings = GatewaySettings(operation=TX, local_test=False, dd_sockets=True, qubes=False) # Test self.assertIsNone(settings.setup()) self.assertIsNone(settings.setup())
def test_validate_key_value_pair(self) -> None: settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=False) self.assert_se("Error: The specified baud rate is not supported.", settings.validate_key_value_pair, 'serial_baudrate', 0) self.assert_se("Error: The specified baud rate is not supported.", settings.validate_key_value_pair, 'serial_baudrate', 10) self.assert_se("Error: The specified baud rate is not supported.", settings.validate_key_value_pair, 'serial_baudrate', 9601) self.assert_se("Error: Invalid value for error correction ratio.", settings.validate_key_value_pair, 'serial_error_correction', -1) self.assertIsNone(settings.validate_key_value_pair("serial_baudrate", 9600)) self.assertIsNone(settings.validate_key_value_pair("serial_error_correction", 20)) self.assertIsNone(settings.validate_key_value_pair("use_serial_usb_adapter", True))
def test_missing_values_are_set_to_default_and_database_is_overwritten(self) -> None: # Setup ensure_dir(DIR_USER_DATA) with open(f"{DIR_USER_DATA}{TX}_serial_settings.json", 'w+') as f: f.write("""\ { "serial_error_correction": 1, "use_serial_usb_adapter": false, "relay_usb_serial_adapter": false }""") # Test settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=False) self.assertEqual(settings.serial_baudrate, 19200) self.assertEqual(settings.serial_error_correction, 1) self.assertEqual(settings.use_serial_usb_adapter, False) self.assertEqual(settings.built_in_serial_interface, 'ttyS0')
def test_manually_edited_settings_are_loaded(self) -> None: # Setup ensure_dir(DIR_USER_DATA) with open(f"{DIR_USER_DATA}{TX}_serial_settings.json", 'w+') as f: f.write("""\ { "serial_baudrate": 9600, "serial_error_correction": 1, "use_serial_usb_adapter": false, "built_in_serial_interface": "ttyS0" }""") # Test settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=False) self.assertEqual(settings.serial_baudrate, 9600) self.assertEqual(settings.serial_error_correction, 1) self.assertEqual(settings.use_serial_usb_adapter, False) self.assertEqual(settings.built_in_serial_interface, 'ttyS0')
def test_print_settings(self) -> None: settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=False) self.assert_prints("""\ Serial interface setting Current value Default value Description ──────────────────────────────────────────────────────────────────────────────── serial_baudrate 19200 19200 The speed of serial interface in bauds per second serial_error_correction 5 5 Number of byte errors serial datagrams can recover from """, settings.print_settings)
def test_invalid_rx_udp_ip_type_is_replaced_with_user_input(self, _) -> None: # Setup ensure_dir(DIR_USER_DATA) with open(f"{DIR_USER_DATA}{TX}_serial_settings.json", 'w+') as f: f.write("""\ { "serial_baudrate": 19200, "serial_error_correction": 5, "use_serial_usb_adapter": true, "built_in_serial_interface": "ttyS0", "rx_udp_ip": 5 }""") # Test settings = GatewaySettings(operation=TX, local_test=True, dd_sockets=True, qubes=True) self.assertEqual(settings.serial_baudrate, 19200) self.assertEqual(settings.serial_error_correction, 5) self.assertEqual(settings.use_serial_usb_adapter, True) self.assertEqual(settings.built_in_serial_interface, 'ttyS0') self.assertEqual(settings.rx_udp_ip, '10.137.0.17')
def test_gateway_setup(self, *_: Any) -> None: settings = GatewaySettings(operation=TX, local_test=False, dd_sockets=True, qubes=False) self.assertIsNone(settings.setup())