def test_login_screen(self): """Test (de)compression of a login screen packet. The result is compared with data obtained from SAP GUI.""" from pysapcompress import compress, decompress, ALG_LZH login_screen_compressed = read_data_file('nw_703_login_screen_compressed.data') login_screen_decompressed = read_data_file('nw_703_login_screen_decompressed.data') status, out_length, decompressed = decompress(login_screen_compressed, len(login_screen_decompressed)) self.assertTrue(status) self.assertEqual(out_length, len(login_screen_decompressed)) self.assertEqual(decompressed, login_screen_decompressed) status, out_length, compressed = compress(decompressed, ALG_LZH) self.assertTrue(status) self.assertEqual(out_length, len(login_screen_compressed)) # As we can't compare with the compressed data (LZH randomness), # decompress again and check with the plain text status, out_length, decompressed = decompress(compressed, len(login_screen_decompressed)) self.assertTrue(status) self.assertEqual(out_length, len(login_screen_decompressed)) self.assertEqual(decompressed, login_screen_decompressed)
def test_login(self): """Test (de)compression of a login packet. The result is compared with data obtained from SAP GUI.""" from pysapcompress import compress, decompress, ALG_LZH login_compressed = read_data_file('sapgui_730_login_compressed.data') login_decompressed = read_data_file( 'sapgui_730_login_decompressed.data') status, out_length, decompressed = decompress(login_compressed, len(login_decompressed)) self.assertTrue(status) self.assertEqual(out_length, len(login_decompressed)) self.assertEqual(decompressed, login_decompressed) status, out_length, compressed = compress(decompressed, ALG_LZH) self.assertTrue(status) self.assertEqual(out_length, len(login_compressed)) # As we can't compare with the compressed data (LZH randomness), # decompress again and check with the plain text status, out_length, decompressed = decompress(compressed, len(login_decompressed)) self.assertTrue(status) self.assertEqual(out_length, len(login_decompressed)) self.assertEqual(decompressed, login_decompressed)
def from_file(cls, filename, version=SAPCAR_VERSION_201, archive_filename=None): """Populates the file format object from an actual file on the local file system. :param filename: filename to build the file format object from :type filename: string :param version: version of the file to construct :type version: string :param archive_filename: filename to use inside the archive file :type archive_filename: string :raise ValueError: if the version requested is invalid """ # Read the file properties and its content stat = os_stat(filename) with open(filename, "rb") as fd: data = fd.read() # Compress the file content and build the compressed string try: (_, out_length, out_buffer) = compress(data, ALG_LZH) except CompressError: return None out_buffer = pack("<I", out_length) + out_buffer # Check the version and grab the file format class if version not in sapcar_archive_file_versions: raise ValueError("Invalid version") ff = sapcar_archive_file_versions[version] # If an archive filename was not provided, use the actual filename if archive_filename is None: archive_filename = filename # Build the object and fill the fields archive_file = cls() archive_file._file_format = ff() archive_file._file_format.perm_mode = stat.st_mode archive_file._file_format.timestamp = stat.st_atime archive_file._file_format.file_length = stat.st_size archive_file._file_format.filename = archive_filename archive_file._file_format.filename_length = len(archive_filename) if archive_file._file_format.version == SAPCAR_VERSION_201: archive_file._file_format.filename_length += 1 # Put the compressed blob inside a last block and add it to the object block = SAPCARCompressedBlockFormat() block.type = SAPCAR_BLOCK_TYPE_COMPRESSED_LAST block.compressed = SAPCARCompressedBlobFormat(out_buffer) block.checksum = cls.calculate_checksum(data) archive_file._file_format.blocks.append(block) return archive_file
def test_compress_output_lzc(self): """ Test compress function output using LZC algorithm. """ from pysapcompress import compress, ALG_LZC status, out_length, out = compress(self.test_string_plain, ALG_LZC) self.assertTrue(status) self.assertEqual(out_length, len(out)) self.assertEqual(out_length, len(self.test_string_compr_lzc)) self.assertEqual(out, self.test_string_compr_lzc)
def from_file(cls, filename, version=SAPCAR_VERSION_201, archive_filename=None): """Populates the file format object from an actual file on the local file system. :param filename: filename to build the file format object from :type filename: string :param version: version of the file to construct :type version: string :param archive_filename: filename to use inside the archive file :type archive_filename: string :raise ValueError: if the version requested is invalid """ # Read the file properties and its content stat = os_stat(filename) with open(filename, "rb") as fd: data = fd.read() # Compress the file content and build the compressed string try: (_, out_length, out_buffer) = compress(data, ALG_LZH) except CompressError: return None out_buffer = pack("<I", out_length) + out_buffer # Check the version and grab the file format class if version not in sapcar_archive_file_versions: raise ValueError("Invalid version") ff = sapcar_archive_file_versions[version] # If an archive filename was not provided, use the actual filename if archive_filename is None: archive_filename = filename # Build the object and fill the fields archive_file = cls() archive_file._file_format = ff() archive_file._file_format.perm_mode = stat.st_mode archive_file._file_format.timestamp = stat.st_atime archive_file._file_format.file_length = stat.st_size archive_file._file_format.filename = archive_filename archive_file._file_format.filename_length = len(archive_filename) if archive_file._file_format.version == SAPCAR_VERSION_201: archive_file._file_format.filename_length += 1 # Put the compressed blob inside a end of data block and add it to the object block = SAPCARCompressedBlockFormat() block.type = SAPCAR_BLOCK_TYPE_COMPRESSED_LAST block.compressed = SAPCARCompressedBlobFormat(out_buffer) block.checksum = cls.calculate_checksum(data) archive_file._file_format.blocks.append(block) return archive_file
def do_compress(self, s): """Compress a string using SAP compression C++ extension. :param s: string to compress :type s: C{string} :return: string compression header plus the compressed string :rtype: C{string} :raise pysapcompress.Error: when a compression error is raised """ if len(s) > 0: # Compress the payload and return the output (_, _, outbuffer) = pysapcompress.compress(s, pysapcompress.ALG_LZH) return outbuffer
def test_lzc(self): """Test compression and decompression using LZC algorithm""" from pysapcompress import compress, decompress, ALG_LZC status, out_length_compressed, out_compressed = compress(self.test_string_plain, ALG_LZC) self.assertTrue(status) self.assertEqual(out_length_compressed, len(out_compressed)) self.assertEqual(out_length_compressed, len(self.test_string_compr_lzc)) self.assertEqual(out_compressed, self.test_string_compr_lzc) status, out_length_decompressed, out_decompressed = decompress(out_compressed, len(self.test_string_plain)) self.assertTrue(status) self.assertEqual(out_length_decompressed, len(out_decompressed)) self.assertEqual(out_length_decompressed, len(self.test_string_plain)) self.assertEqual(out_decompressed, self.test_string_plain)
def test_lzh(self): """Test compression and decompression using LZH algorithm""" from pysapcompress import compress, decompress, ALG_LZH status, out_length_compressed, out_compressed = compress(self.test_string_plain, ALG_LZH) self.assertTrue(status) self.assertEqual(out_length_compressed, len(out_compressed)) # LZH compression has a random component, so the only check here is # in regards to the length and to see if if decompress back to the # original string status, out_length_decompressed, out_decompressed = decompress(out_compressed, len(self.test_string_plain)) self.assertTrue(status) self.assertEqual(out_length_decompressed, len(out_decompressed)) self.assertEqual(out_length_decompressed, len(self.test_string_plain)) self.assertEqual(out_decompressed, self.test_string_plain)