import BSDiffPatch from '../../../src/models/patches/bsdiffPatch.js'; import { applyPatch, INPUT16, parsePatch } from './patchTestUtil.js'; const patchFrom = BSDiffPatch.patchFrom.bind(BSDiffPatch); const FILE_NAME = 'patch 00000000.bdf'; // "BSDIFF40", then 8-byte sign-magnitude LE control block length, diff block length, and new // size, then the bzip2-compressed control, diff, and extra blocks. Each control record is a // diff length, extra length, and seek length. // [diff 16, extra 4, seek 0], a zeroed diff, and the extra bytes "aabbccdd" const GROW = '42534449464634302b0000000000000025000000000000001400000000000000425a6839314159265359c7b1cf66000005e0004c08400020002186819a0c56c9b8bb9229c284863d8e7b30425a6839314159265359aad2dd3700000040004004200021008283177245385090aad2dd37425a6839314159265359db91780f000000001e00100008000400022000219a68334d133c5dc914e142436e45e03c'; describe('patchFrom', () => { it('should parse', async () => { await expect(parsePatch(patchFrom, FILE_NAME, GROW)).resolves.toEqual({ crcBefore: '00000000', crcAfter: undefined, sizeAfter: 20, }); }); test.each([ ['empty', ''], ['short header', GROW.slice(0, 62)], ['wrong magic', `4253444946463431${GROW.slice(16)}`], ['negative control block length', `${GROW.slice(0, 30)}80${GROW.slice(32)}`], ['negative new size', `${GROW.slice(0, 62)}80${GROW.slice(64)}`], ['blocks past the end', `${GROW.slice(0, 16)}ffffff0000000000${GROW.slice(32)}`], ])('should throw on %s', async (_name, patchHex) => { await expect(parsePatch(patchFrom, FILE_NAME, patchHex)).rejects.toThrow( /BSDiff patch header is invalid/, ); }); }); describe('createPatchedFile', () => { test.each([ [ 'modify', '425344494646343029000000000000002a000000000000001000000000000000425a6839314159265359796f338e000002600040004800200030cc0cf505ce2ee48a70a120f2de671c425a683931415926535930788f95000000c01044004000200021134198a909dc5dc914e14240c1e23e54425a683917724538509000000000', '00010203a4a5a6a708090a0b0c0d0e0f', ], ['grow', GROW, `${INPUT16}aabbccdd`], [ 'shrink', '4253444946463430290000000000000025000000000000000800000000000000425a6839314159265359b8ad553b000002600040400800200030cc0cf505ce2ee48a70a121715aaa76425a683931415926535996fb44a60000004000440020002100828317724538509096fb44a6425a683917724538509000000000', '0001020304050607', ], [ 'extra only', '425344494646343028000000000000000e000000000000000400000000000000425a6839314159265359ba8d7f2d00000040004408200030cc0932546538bb9229c28485d46bf968425a683917724538509000000000425a6839314159265359db91780f000000001e00100008000400022000219a68334d133c5dc914e142436e45e03c', 'aabbccdd', ], [ 'seek forward', '42534449464634302c0000000000000025000000000000000800000000000000425a68393141592653599d48a8b700000760004c08080020002120066822622ed4e2ee48a70a1213a91516e0425a683931415926535996fb44a60000004000440020002100828317724538509096fb44a6425a683917724538509000000000', '0001020308090a0b', ], [ 'seek backward', '42534449464634302f0000000000000025000000000000001000000000000000425a6839314159265359b0cfb31a00000760405048080040002000212460300f5c21cacb85dc914e14242c33ecc680425a6839314159265359aad2dd3700000040004004200021008283177245385090aad2dd37425a683917724538509000000000', '00010203040506070001020304050607', ], // Bytes past the end of the input are zero [ 'diff past the end of the input', '42534449464634302d0000000000000026000000000000000400000000000000425a68393141592653590ea20b7a000000e0004c050800200021a69b40c00a45c875e1772453850900ea20b7a0425a6839314159265359e360d83600000040007000200021981984cc2ee48a70a121c6c1b06c425a683917724538509000000000', '0e0f0102', ], // Bytes before the start of the input are zero [ 'diff before the start of the input', '425344494646343030000000000000002a000000000000000400000000000000425a6839314159265359a2660d3b000000e040540408004000200021a7a980c008df250a2b85dc914e14242899834ec0425a6839314159265359f761631a0000014018400000100008200021981984cc2ee48a70a121eec2c634425a683917724538509000000000', 'aabb0001', ], ])('should apply: %s', async (_name, patchHex, expectedHex) => { await expect(applyPatch(patchFrom, FILE_NAME, patchHex, INPUT16)).resolves.toEqual( Buffer.from(expectedHex, 'hex'), ); }); test.each([ [ 'diff past the new size', '4253444946463430290000000000000025000000000000001000000000000000425a68393141592653592fc0ae3c000002600040002800200030cc0cf505ce2ee48a70a1205f815c78425a6839314159265359bd8ff79300000040004002200021008283177245385090bd8ff793425a683917724538509000000000', ], [ 'extra past the new size', '42534449464634302b0000000000000025000000000000001000000000000000425a683931415926535956d88cb4000005e00068084000200030cd00901a41566e2ee48a70a120adb11968425a6839314159265359aad2dd3700000040004004200021008283177245385090aad2dd37425a6839314159265359b1f7404b00000040004000200021184682ee48a70a12163ee80960', ], [ 'negative diff length', '42534449464634302b000000000000000e000000000000001000000000000000425a6839314159265359902f7b9600000440407004400020002183419a0854c88e2ee48a70a121205ef72c425a683917724538509000000000425a683917724538509000000000', ], [ 'negative extra length', '42534449464634302d000000000000000e000000000000001000000000000000425a6839314159265359e9cce217000001404074004000200030cd00cd47a0bf640ae177245385090e9cce2170425a683917724538509000000000425a683917724538509000000000', ], [ 'short control block', '4253444946463430290000000000000025000000000000001000000000000000425a6839314159265359582b15f9000002e0004008400020002126419890b8bb9229c28482c158afc8425a6839314159265359aad2dd3700000040004004200021008283177245385090aad2dd37425a683917724538509000000000', ], [ 'short diff block', '4253444946463430290000000000000025000000000000001000000000000000425a6839314159265359796f338e000002600040004800200030cc0cf505ce2ee48a70a120f2de671c425a683931415926535996fb44a60000004000440020002100828317724538509096fb44a6425a683917724538509000000000', ], [ 'short extra block', '42534449464634302b000000000000000e000000000000001000000000000000425a6839314159265359f6aa4b0b000000600044084000200030cd34121a6700f177245385090f6aa4b0b0425a683917724538509000000000425a683931415926535996fb44a60000004000440020002100828317724538509096fb44a6', ], ])('should throw on %s', async (_name, patchHex) => { await expect(applyPatch(patchFrom, FILE_NAME, patchHex, INPUT16)).rejects.toThrow( /BSDiff patch is corrupt/, ); }); it('should throw on corrupt bzip2 data', async () => { // One byte flipped inside the control block's compressed data const patchHex = `${GROW.slice(0, 104)}f7${GROW.slice(106)}`; await expect(applyPatch(patchFrom, FILE_NAME, patchHex, INPUT16)).rejects.toThrow(); }); });