def test_apply_patch_foo_short_none_compression(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/short-none.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "Early end of patch data.")
def test_apply_patch_foo_short(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/short.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "End of patch not found.")
def test_apply_patch_foo_bad_patch_type(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/bad-patch-type.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "Bad patch type 7.")
def test_apply_patch_short_to_size(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/short-to-size.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "Failed to read consecutive size byte.")
def test_apply_patch_one_byte(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/one-byte.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "Failed to read first size byte.")
def test_apply_patch_foo_long(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/bad-lzma-end.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "Patch decompression failed.")
def test_apply_patch_foo_empty(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/empty.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "Failed to read the patch header.")
def test_apply_patch_foo_extra_data_too_long(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/extra-data-too-long.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual(str(cm.exception), "Patch extra data too long.")
def test_apply_patch_foo_bad_compression(self): fnew = BytesIO() with open('tests/files/foo/old', 'rb') as fold: with open('tests/files/foo/bad-compression.patch', 'rb') as fpatch: with self.assertRaises(detools.Error) as cm: detools.apply_patch(fold, fpatch, fnew) self.assertEqual( str(cm.exception), "Expected compression none(0), lzma(1), crle(2), bz2(3), " "heatshrink(4), zstd(5) or lz4(6), but got 15.")
def assert_apply_patch(self, from_filename, to_filename, patch_filename, **kwargs): patch_type = kwargs.get('patch_type', 'sequential') if patch_type in ['sequential', 'hdiffpatch']: fto = BytesIO() with open(from_filename, 'rb') as ffrom: with open(patch_filename, 'rb') as fpatch: to_size = detools.apply_patch(ffrom, fpatch, fto) actual = fto.getvalue() elif patch_type == 'in-place': memory_size = kwargs['memory_size'] with open(from_filename, 'rb') as ffrom: data = ffrom.read() data += (memory_size - len(data)) * b'\xff' fmem = BytesIO(data) with open(patch_filename, 'rb') as fpatch: to_size = detools.apply_patch_in_place(fmem, fpatch) actual = fmem.getvalue()[:to_size] elif patch_type == 'bsdiff': fto = BytesIO() with open(from_filename, 'rb') as ffrom: with open(patch_filename, 'rb') as fpatch: to_size = detools.apply_patch_bsdiff(ffrom, fpatch, fto) actual = fto.getvalue() else: raise Exception(patch_type) with open(to_filename, 'rb') as fto: expected = fto.read() # open('actual-to.bin', 'wb').write(actual) self.assertEqual(to_size, len(expected)) self.assertEqual(len(actual), len(expected)) self.assertEqual(actual, expected)
def assert_apply_patch(self, from_filename, to_filename, patch_filename, **kwargs): patch_type = kwargs.get('patch_type', 'normal') if patch_type == 'normal': fnew = BytesIO() with open(from_filename, 'rb') as fold: with open(patch_filename, 'rb') as fpatch: to_size = detools.apply_patch(fold, fpatch, fnew) actual = fnew.getvalue() elif patch_type == 'in-place': memory_size = kwargs['memory_size'] with open(from_filename, 'rb') as fold: data = fold.read() data += (memory_size - len(data)) * b'\xff' fmem = BytesIO(data) with open(patch_filename, 'rb') as fpatch: to_size = detools.apply_patch_in_place(fmem, fpatch) actual = fmem.getvalue()[:to_size] elif patch_type == 'bsdiff': fnew = BytesIO() with open(from_filename, 'rb') as fold: with open(patch_filename, 'rb') as fpatch: to_size = detools.apply_patch_bsdiff(fold, fpatch, fnew) actual = fnew.getvalue() else: raise Exception(patch_type) with open(to_filename, 'rb') as fnew: expected = fnew.read() # open('actual-to.bin', 'wb').write(actual) self.assertEqual(to_size, len(expected)) self.assertEqual(actual, expected)