@Test
public void testWriteV1Patch() throws IOException {

  // ---------------------------------------------------------------------------------------------
  // CAUTION - DO NOT CHANGE THIS FUNCTION WITHOUT DUE CONSIDERATION FOR BREAKING THE PATCH FORMAT
  // ---------------------------------------------------------------------------------------------
  // This test writes a simple patch with all the static data listed above and verifies it.
  // This code MUST be INDEPENDENT of the real patch parser code, even if it is partially
  // redundant; this guards against accidental changes to the patch writer that could alter the
  // format and otherwise escape detection.
  PatchWriter writer =
      new PatchWriter(
          PLAN, DELTA_FRIENDLY_OLD_FILE_SIZE, DELTA_FRIENDLY_NEW_FILE_SIZE, deltaFile);
  writer.writeV1Patch(buffer);
  DataInputStream patchIn = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  byte[] eightBytes = new byte[8];
  // Start by reading the signature and flags
  patchIn.readFully(eightBytes);
  Assert.assertArrayEquals(PatchConstants.IDENTIFIER.getBytes("US-ASCII"), eightBytes);
  Assert.assertEquals(0, patchIn.readInt()); // Flags, all reserved in v1
  Assert.assertEquals(DELTA_FRIENDLY_OLD_FILE_SIZE, patchIn.readLong());
  // Read the uncompression instructions
  Assert.assertEquals(1, patchIn.readInt()); // Number of old archive uncompression instructions
  Assert.assertEquals(OLD_DELTA_FRIENDLY_UNCOMPRESS_RANGE.getOffset(), patchIn.readLong());
  Assert.assertEquals(OLD_DELTA_FRIENDLY_UNCOMPRESS_RANGE.getLength(), patchIn.readLong());
  // Read the recompression instructions
  Assert.assertEquals(1, patchIn.readInt()); // Number of new archive recompression instructions
  Assert.assertEquals(NEW_DELTA_FRIENDLY_RECOMPRESS_RANGE.getOffset(), patchIn.readLong());
  Assert.assertEquals(NEW_DELTA_FRIENDLY_RECOMPRESS_RANGE.getLength(), patchIn.readLong());
  // Now the JreDeflateParameters for the record
  Assert.assertEquals(
      PatchConstants.CompatibilityWindowId.DEFAULT_DEFLATE.patchValue, patchIn.read());
  Assert.assertEquals(DEFLATE_PARAMS.level, patchIn.read());
  Assert.assertEquals(DEFLATE_PARAMS.strategy, patchIn.read());
  Assert.assertEquals(DEFLATE_PARAMS.nowrap ? 1 : 0, patchIn.read());
  // Delta section. V1 patches have exactly one delta entry and it is always mapped to the entire
  // file contents of the delta-friendly archives
  Assert.assertEquals(1, patchIn.readInt()); // Number of difference records
  Assert.assertEquals(PatchConstants.DeltaFormat.BSDIFF.patchValue, patchIn.read());
  Assert.assertEquals(0, patchIn.readLong()); // Old delta-friendly range start
  Assert.assertEquals(DELTA_FRIENDLY_OLD_FILE_SIZE, patchIn.readLong()); // old range length
  Assert.assertEquals(0, patchIn.readLong()); // New delta-friendly range start
  Assert.assertEquals(DELTA_FRIENDLY_NEW_FILE_SIZE, patchIn.readLong()); // new range length
  byte[] expectedDeltaContent = DELTA_CONTENT.getBytes("US-ASCII");
  Assert.assertEquals(expectedDeltaContent.length, patchIn.readLong());
  byte[] actualDeltaContent = new byte[expectedDeltaContent.length];
  patchIn.readFully(actualDeltaContent);
  Assert.assertArrayEquals(expectedDeltaContent, actualDeltaContent);
}