def test_serializer(self): ''' This test checks: * the serialization and storage of the network for later use. For each test network we write to a file (pickle), and then we read from that file, checking that it is identical to the original network * several other auxiliary functions related with storage and retrieval of network configuration files ''' net_1_conf = self.cman.get_network_conf_file(self.net_1) net_2_conf = self.cman.get_network_conf_file(self.net_2) net_3_conf = self.cman.get_network_conf_file(self.net_3) net_4_conf = self.cman.get_network_conf_file(self.net_4) net_5_conf = self.cman.get_network_conf_file(self.net_5) self.net_1_ser = util.build_net_serialization_filename(net_1_conf) self.net_2_ser = util.build_net_serialization_filename(net_2_conf) self.net_3_ser = util.build_net_serialization_filename(net_3_conf) self.net_4_ser = util.build_net_serialization_filename(net_4_conf) self.net_5_ser = util.build_net_serialization_filename(net_5_conf) self.assertTrue(util.is_net_serialization_filename(self.net_1_ser)) self.assertTrue(util.is_net_serialization_filename(self.net_2_ser)) self.assertTrue(util.is_net_serialization_filename(self.net_3_ser)) self.assertTrue(util.is_net_serialization_filename(self.net_4_ser)) self.assertTrue(util.is_net_serialization_filename(self.net_5_ser)) #calls to serialize_net_to_file have the side effect of creating a pickle file self.assertEqual( util.serialize_net_to_file(net_1_conf, self.net_1), self.net_1_ser ) self.assertEqual( util.serialize_net_to_file(net_2_conf, self.net_2), self.net_2_ser ) self.assertEqual( util.serialize_net_to_file(net_3_conf, self.net_3), self.net_3_ser ) self.assertEqual( util.serialize_net_to_file(net_4_conf, self.net_4), self.net_4_ser ) self.assertEqual( util.serialize_net_to_file(net_5_conf, self.net_5), self.net_5_ser ) #test that we have the same networks after deserialization self.assertEqual( util.deserialize_net_from_file(net_1_conf), self.net_1 ) self.assertEqual( util.deserialize_net_from_file(net_2_conf), self.net_2 ) self.assertEqual( util.deserialize_net_from_file(net_3_conf), self.net_3 ) self.assertEqual( util.deserialize_net_from_file(net_4_conf), self.net_4 ) self.assertEqual( util.deserialize_net_from_file(net_5_conf), self.net_5 ) for n in [self.net_1_ser, self.net_2_ser, self.net_3_ser, self.net_4_ser, self.net_5_ser]: try: remove(n) except OSError: pass
def connect(self, net_o, ask_reuse_file=True): ''' connect to a wireless network using a Network object. This function carries out all the necessary commands to build a connection. Depending on who the caller is we might want to deactivate the question to reuse the configuration file -if found- for a network ''' logger = logging.getLogger(Conf.LOGGER_NAME) strategy = self.decide_connection_strategy(net_o) conf_file_path = self.get_network_conf_file(net_o) valid_network_file = False #First, determine if the user wants to use the existing configuration file if path.exists(conf_file_path): if ask_reuse_file: initial_msg = 'I have found a configuration for this network, do you want to use it? (y/n)' error_msg = 'Answer y(es) or n(o)' answer_use_stored = util.ask_for_input(initial_msg, error_msg, ['y', 'n']) if answer_use_stored == 'y': valid_network_file = True else: valid_network_file = True #Create configuration file if needed and update the value #of valid_network_file for later use if not valid_network_file: if strategy == ConnectionManager.CONNECTION_STRATEGY_SUPPLICANT: if Conf.DEBUG_MODE: logger.info("Crear archivo de conf para supplicant") valid_network_file, conf_file_path = self.generate_supplicant_file(net_o) if valid_network_file: ''' Now we add the serialization for this network objet in order to use it for later connections ''' util.serialize_net_to_file(conf_file_path, net_o) elif strategy == ConnectionManager.CONNECTION_STRATEGY_KEY: if Conf.DEBUG_MODE: logger.info("Crear archivo de conf para key") valid_network_file, conf_file_path = self.generate_key_file(net_o) if valid_network_file: util.serialize_net_to_file(conf_file_path, net_o) elif strategy == ConnectionManager.CONNECTION_STRATEGY_OPEN: if Conf.DEBUG_MODE: logger.info("Crear archivo de conf para open") valid_network_file, conf_file_path = self.generate_open_file(net_o) if valid_network_file: util.serialize_net_to_file(conf_file_path, net_o) #Try to connect if it exists or it has been created a network configuration file success = False do_execute = True if Conf.DEBUG_MODE: do_execute = False if valid_network_file: if strategy == ConnectionManager.CONNECTION_STRATEGY_SUPPLICANT: success, commands = self.connect_wpa(conf_file_path, do_execute) elif strategy == ConnectionManager.CONNECTION_STRATEGY_KEY: success, commands = self.connect_key(conf_file_path, net_o, do_execute) elif strategy == ConnectionManager.CONNECTION_STRATEGY_OPEN: success, commands = self.connect_open(conf_file_path, net_o, do_execute) if Conf.DEBUG_MODE: logger.info(commands) del(commands) else: logger.error('No se puede conectar a {} sin un fichero de configuración supplicant'. format(net_o.essid)) return success