def pyexe_test_single_open_close_file_object_with_dereference( filename, mode): """Tests single file-like object open and close with dereference.""" description = ( "Testing single open close of file-like object with dereference of: " "{0:s} with access: {1:s}\t").format( get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: file_object = open(filename, "rb") exe_file = pyexe.file() exe_file.open_file_object(file_object, mode) del file_object exe_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def test_open_close(self): """Tests the open and close functions.""" if not unittest.source: return exe_file = pyexe.file() # Test open and close. exe_file.open(unittest.source) exe_file.close() # Test open and close a second time to validate clean up on close. exe_file.open(unittest.source) exe_file.close() file_object = open(unittest.source, "rb") # Test open_file_object and close. exe_file.open_file_object(file_object) exe_file.close() # Test open_file_object and close a second time to validate clean up on close. exe_file.open_file_object(file_object) exe_file.close() # Test open_file_object and close and dereferencing file_object. exe_file.open_file_object(file_object) del file_object exe_file.close()
def pyexe_test_multi_open_close_file(filename, mode): """Tests multiple open and close.""" description = ( "Testing multi open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: exe_file = pyexe.file() exe_file.open(filename, mode) exe_file.close() exe_file.open(filename, mode) exe_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def test_open_close(self): """Tests the open and close functions.""" if not unittest.source: return exe_file = pyexe.file() # Test open and close. exe_file.open(unittest.source) exe_file.close() # Test open and close a second time to validate clean up on close. exe_file.open(unittest.source) exe_file.close() file_object = open(unittest.source, "rb") # Test open_file_object and close. exe_file.open_file_object(file_object) exe_file.close() # Test open_file_object and close a second time to validate clean up on close. exe_file.open_file_object(file_object) exe_file.close() # Test open_file_object and close and dereferencing file_object. exe_file.open_file_object(file_object) del file_object exe_file.close()
def __init__(self, windows_path, ascii_codepage=u'cp1252', preferred_language_identifier=0x0409): """Initializes the Windows Message Resource file. Args: windows_path: normalized version of the Windows path. ascii_codepage: optional ASCII string codepage. The default is cp1252 (or windows-1252). preferred_language_identifier: optional preferred language identifier (LCID). The default is 0x0409 (en-US). """ super(MessageResourceFile, self).__init__() self._ascii_codepage = ascii_codepage self._exe_file = pyexe.file() self._exe_file.set_ascii_codepage(self._ascii_codepage) self._file_object = None self._file_version = None self._is_open = False self._preferred_language_identifier = preferred_language_identifier self._product_version = None self._wrc_stream = pywrc.stream() # TODO: wrc stream set codepage? self.windows_path = windows_path
def __init__(self, windows_path, ascii_codepage='cp1252', preferred_language_identifier=0x0409): """Initializes the Windows Message Resource file. Args: windows_path (str): normalized version of the Windows path. ascii_codepage (Optional[str]): ASCII string codepage. preferred_language_identifier (Optional[int]): preferred language identifier (LCID). """ super(MessageResourceFile, self).__init__() self._ascii_codepage = ascii_codepage self._exe_file = pyexe.file() self._exe_file.set_ascii_codepage(self._ascii_codepage) self._exe_section = None self._file_object = None self._file_version = None self._is_open = False self._preferred_language_identifier = preferred_language_identifier self._product_version = None # TODO: wrc stream set codepage? self._wrc_stream = pywrc.stream() self.windows_path = windows_path
def test_open_close(self): """Tests the open and close functions.""" test_source = unittest.source if not test_source: return exe_file = pyexe.file() # Test open and close. exe_file.open(test_source) exe_file.close() # Test open and close a second time to validate clean up on close. exe_file.open(test_source) exe_file.close() if os.path.isfile(test_source): with open(test_source, "rb") as file_object: # Test open_file_object and close. exe_file.open_file_object(file_object) exe_file.close() # Test open_file_object and close a second time to validate clean up on close. exe_file.open_file_object(file_object) exe_file.close() # Test open_file_object and close and dereferencing file_object. exe_file.open_file_object(file_object) del file_object exe_file.close()
def test_open_file_object(self): """Tests the open_file_object function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") if not os.path.isfile(test_source): raise unittest.SkipTest("source not a regular file") exe_file = pyexe.file() with open(test_source, "rb") as file_object: exe_file.open_file_object(file_object) with self.assertRaises(IOError): exe_file.open_file_object(file_object) exe_file.close() with self.assertRaises(TypeError): exe_file.open_file_object(None) with self.assertRaises(ValueError): exe_file.open_file_object(file_object, mode="w")
def test_close(self): """Tests the close function.""" if not unittest.source: return exe_file = pyexe.file() with self.assertRaises(IOError): exe_file.close()
def test_close(self): """Tests the close function.""" if not unittest.source: return exe_file = pyexe.file() with self.assertRaises(IOError): exe_file.close()
def test_close(self): """Tests the close function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") exe_file = pyexe.file() with self.assertRaises(IOError): exe_file.close()
def pyexe_test_single_open_close_file(filename, mode): """Tests a single open and close.""" description = ( "Testing single open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: exe_file = pyexe.file() exe_file.open(filename, mode) exe_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pyexe_file_open") if not filename and str(exception) == expected_message: pass else: error_string = str(exception) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format( "pyexe_file_open") if mode != "w" or str(exception) != expected_message: error_string = str(exception) result = False except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def pyexe_test_seek_file(filename): exe_file = pyexe.file() exe_file.open(filename, "r") result = True for section_index in exe_file.number_of_sections: exe_section = exe_file.get_section(section_index) result = pyexe_test_read(exe_section) if not result: break exe_file.close() return result
def test_get_number_of_sections(self): """Tests the get_number_of_sections function and number_of_sections property.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") exe_file = pyexe.file() exe_file.open(test_source) number_of_sections = exe_file.get_number_of_sections() self.assertIsNotNone(number_of_sections) self.assertIsNotNone(exe_file.number_of_sections) exe_file.close()
def test_get_ascii_codepage(self): """Tests the get_ascii_codepage function and ascii_codepage property.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") exe_file = pyexe.file() exe_file.open(test_source) ascii_codepage = exe_file.get_ascii_codepage() self.assertIsNotNone(ascii_codepage) self.assertIsNotNone(exe_file.ascii_codepage) exe_file.close()
def pyexe_test_seek_file(filename): """Tests the seek function with a file.""" exe_file = pyexe.file() exe_file.open(filename, "r") result = True for section_index in range(0, exe_file.number_of_sections): exe_section = exe_file.get_section(section_index) result = pyexe_test_seek(exe_section) if not result: break exe_file.close() return result
def pyexe_test_read_file(filename): """Tests the read function with a file.""" exe_file = pyexe.file() exe_file.open(filename, "r") result = True for section_index in range(0, exe_file.number_of_sections): exe_section = exe_file.get_section(section_index) result = pyexe_test_read(exe_section) if not result: break exe_file.close() return result
def pyexe_test_single_open_close_file(filename, mode): if not filename: filename_string = "None" else: filename_string = filename print("Testing single open close of: {0:s} with access: {1:s}\t".format( filename_string, get_mode_string(mode))) result = True try: exe_file = pyexe.file() exe_file.open(filename, mode) exe_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pyexe_file_open") if not filename and str(exception) == expected_message: pass else: print(str(exception)) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format( "pyexe_file_open") if mode != "w" or str(exception) != expected_message: print(str(exception)) result = False except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def pyexe_test_seek_file_object(filename): """Tests the seek function with a file-like object.""" file_object = open(filename, "rb") exe_file = pyexe.file() exe_file.open_file_object(file_object, "r") result = True for section_index in range(0, exe_file.number_of_sections): exe_section = exe_file.get_section(section_index) result = pyexe_test_seek(exe_section) if not result: break exe_file.close() return result
def pyexe_test_read_file_object(filename): """Tests the read function with a file-like object.""" file_object = open(filename, "rb") exe_file = pyexe.file() exe_file.open_file_object(file_object, "r") result = True for section_index in range(0, exe_file.number_of_sections): exe_section = exe_file.get_section(section_index) result = pyexe_test_read(exe_section) if not result: break exe_file.close() return result
def test_open(self): """Tests the open function.""" if not unittest.source: return exe_file = pyexe.file() exe_file.open(unittest.source) with self.assertRaises(IOError): exe_file.open(unittest.source) exe_file.close() with self.assertRaises(TypeError): exe_file.open(None) with self.assertRaises(ValueError): exe_file.open(unittest.source, mode="w")
def test_open(self): """Tests the open function.""" if not unittest.source: return exe_file = pyexe.file() exe_file.open(unittest.source) with self.assertRaises(IOError): exe_file.open(unittest.source) exe_file.close() with self.assertRaises(TypeError): exe_file.open(None) with self.assertRaises(ValueError): exe_file.open(unittest.source, mode="w")
def test_open(self): """Tests the open function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") exe_file = pyexe.file() exe_file.open(test_source) with self.assertRaises(IOError): exe_file.open(test_source) exe_file.close() with self.assertRaises(TypeError): exe_file.open(None) with self.assertRaises(ValueError): exe_file.open(test_source, mode="w")
def test_set_ascii_codepage(self): """Tests the set_ascii_codepage function.""" supported_codepages = ( "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258") exe_file = pyexe.file() for codepage in supported_codepages: exe_file.set_ascii_codepage(codepage) unsupported_codepages = ( "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u") for codepage in unsupported_codepages: with self.assertRaises(RuntimeError): exe_file.set_ascii_codepage(codepage)
def pyexe_test_single_open_close_file_object(filename, mode): print(("Testing single open close of file-like object of: {0:s} " "with access: {1:s}\t").format(filename, get_mode_string(mode))) result = True try: file_object = open(filename, "rb") exe_file = pyexe.file() exe_file.open_file_object(file_object, mode) exe_file.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def test_open_file_object(self): """Tests the open_file_object function.""" if not unittest.source: return file_object = open(unittest.source, "rb") exe_file = pyexe.file() exe_file.open_file_object(file_object) with self.assertRaises(IOError): exe_file.open_file_object(file_object) exe_file.close() # TODO: change IOError into TypeError with self.assertRaises(IOError): exe_file.open_file_object(None) with self.assertRaises(ValueError): exe_file.open_file_object(file_object, mode="w")
def test_set_ascii_codepage(self): """Tests the set_ascii_codepage function.""" supported_codepages = ("ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258") exe_file = pyexe.file() for codepage in supported_codepages: exe_file.set_ascii_codepage(codepage) unsupported_codepages = ("iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u") for codepage in unsupported_codepages: with self.assertRaises(RuntimeError): exe_file.set_ascii_codepage(codepage)
def test_open_file_object(self): """Tests the open_file_object function.""" if not unittest.source: return file_object = open(unittest.source, "rb") exe_file = pyexe.file() exe_file.open_file_object(file_object) with self.assertRaises(IOError): exe_file.open_file_object(file_object) exe_file.close() # TODO: change IOError into TypeError with self.assertRaises(IOError): exe_file.open_file_object(None) with self.assertRaises(ValueError): exe_file.open_file_object(file_object, mode="w")
def pyexe_test_multi_open_close_file(filename, mode): print("Testing multi open close of: {0:s} with access: {1:s}\t".format( filename, get_mode_string(mode))) result = True try: exe_file = pyexe.file() exe_file.open(filename, mode) exe_file.close() exe_file.open(filename, mode) exe_file.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def pyexe_test_seek_file_no_open(filename): """Tests the seek function with a file without open.""" description = "Testing seek of offset without open:\t" print(description, end="") exe_file = pyexe.file() error_string = None result = False try: exe_file.get_section(0) except Exception as exception: error_string = str(exception) result = True if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def pyexe_test_read_file_no_open(filename): """Tests the read function with a file without open.""" description = "Testing read of without open:\t" print(description, end="") exe_file = pyexe.file() error_string = None result = False try: exe_file.get_section(0) except Exception as exception: error_string = str(exception) result = True if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def main(): supported_codepages = [ "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258"] unsupported_codepages = [ "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u"] exe_file = pyexe.file() result = True for codepage in supported_codepages: print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: exe_file.ascii_codepage = codepage result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: exe_file.set_ascii_codepage(codepage) result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") for codepage in unsupported_codepages: print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: exe_file.ascii_codepage = codepage except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pyexe_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: exe_file.set_ascii_codepage(codepage) except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pyexe_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") return True
def test_signal_abort(self): """Tests the signal_abort function.""" exe_file = pyexe.file() exe_file.signal_abort()
def test_signal_abort(self): """Tests the signal_abort function.""" exe_file = pyexe.file() exe_file.signal_abort()
def main(): supported_codepages = [ "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258"] unsupported_codepages = [ "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u"] exe_file = pyexe.file() result = True for codepage in supported_codepages: print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: exe_file.ascii_codepage = codepage result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: exe_file.set_ascii_codepage(codepage) result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") for codepage in unsupported_codepages: print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: exe_file.ascii_codepage = codepage except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pyexe_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: exe_file.set_ascii_codepage(codepage) except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pyexe_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") return True