BsPatch { public static void applyPatch( RandomAccessFile oldData, OutputStream newData, InputStream patchData) throws PatchFormatException, IOException { patchData = new BufferedInputStream(patchData, PATCH_STREAM_BUFFER_SIZE); newData = new BufferedOutputStream(newData, OUTPUT_STREAM_BUFFER_SIZE); try { applyPatchInternal(oldData, newData, patchData); } finally { newData.flush(); } }  static void applyPatch(
      RandomAccessFile oldData, OutputStream newData, InputStream patchData);  }
@Test public void testApplyPatch_BadSignature() throws Exception { createEmptyOldFile(10); String junkSignature = "WOOOOOO/BSDIFF43"; InputStream patchIn = makePatch( junkSignature, 10, 10, 0, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with bad signature"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("bad signature", actual); } }
@Test public void testApplyPatch_NewLengthNegative() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, -10, 10, 0, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with negative newLength"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("bad newSize", actual); } }
@Test public void testApplyPatch_NewLengthTooLarge() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, Integer.MAX_VALUE + 1, 10, 0, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch( new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with excessive newLength"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("bad newSize", actual); } }
@Test public void testApplyPatch_DiffSegmentLengthNegative() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, 10, -10, 0, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with negative diffSegmentLength"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("bad diffSegmentLength", actual); } }
@Test public void testApplyPatch_DiffSegmentLengthTooLarge() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, 10, Integer.MAX_VALUE + 1, 0, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with excessive diffSegmentLength"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("bad diffSegmentLength", actual); } }
@Test public void testApplyPatch_CopySegmentLengthNegative() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, 10, 10, -10, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with negative copySegmentLength"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("bad copySegmentLength", actual); } }
@Test public void testApplyPatch_CopySegmentLengthTooLarge() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, 10, 0, Integer.MAX_VALUE + 1, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with excessive copySegmentLength"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("bad copySegmentLength", actual); } }
@Test public void testApplyPatch_ExpectedFinalNewDataBytesWritten_PastEOF() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, 10, 10, 1, 0, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch that moves past EOF in new file"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("expectedFinalNewDataBytesWritten too large", actual); } }
@Test public void testApplyPatch_ExpectedFinalOldDataOffset_Negative() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, 10, 10, 0, -11, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with that moves to a negative offset in old file"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("expectedFinalOldDataOffset is negative", actual); } }
@Test public void testApplyPatch_ExpectedFinalOldDataOffset_PastEOF() throws Exception { createEmptyOldFile(10); InputStream patchIn = makePatch( SIGNATURE, 10, 10, 0, 1, new byte[10] ); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with that moves past EOF in old file"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("expectedFinalOldDataOffset too large", actual); } }
@Test public void testApplyPatch_TruncatedSignature() throws Exception { createEmptyOldFile(10); InputStream patchIn = new ByteArrayInputStream("X".getBytes("US-ASCII")); ByteArrayOutputStream newData = new ByteArrayOutputStream(); try { BsPatch.applyPatch(new RandomAccessFile(oldFile, "r"), newData, patchIn); Assert.fail("Read patch with truncated signature"); } catch (PatchFormatException expected) { String actual = expected.getMessage(); Assert.assertEquals("truncated signature", actual); } }