@Test
  public void testCompactionWithCorruptBlock() throws Exception {
    createStoreFile(r, Bytes.toString(FAMILY));
    createStoreFile(r, Bytes.toString(FAMILY));
    HStore store = r.getStore(FAMILY);

    Collection<HStoreFile> storeFiles = store.getStorefiles();
    DefaultCompactor tool = (DefaultCompactor) store.storeEngine.getCompactor();
    CompactionRequestImpl request = new CompactionRequestImpl(storeFiles);
    tool.compact(request, NoLimitThroughputController.INSTANCE, null);

    // insert the hfile with a corrupted data block into the region's tmp directory, where
    // compaction output is collected.
    FileSystem fs = store.getFileSystem();
    Path tmpPath = store.getRegionFileSystem().createTempName();
    try (
      InputStream inputStream =
        getClass().getResourceAsStream("TestCompaction_HFileWithCorruptBlock.gz");
      GZIPInputStream gzipInputStream = new GZIPInputStream(Objects.requireNonNull(inputStream));
      OutputStream outputStream = fs.create(tmpPath, null, true, 512, (short) 3, 1024L, null)) {
      assertThat(gzipInputStream, notNullValue());
      assertThat(outputStream, notNullValue());
      IOUtils.copyBytes(gzipInputStream, outputStream, 512);
    }
    LoggerFactory.getLogger(TestCompaction.class).info("Wrote corrupted HFile to {}", tmpPath);

    // The complete compaction should fail and the corrupt file should remain
    // in the 'tmp' directory;
    try {
      store.doCompaction(request, storeFiles, null, EnvironmentEdgeManager.currentTime(),
        Collections.singletonList(tmpPath));
    } catch (IOException e) {
      Throwable rootCause = e;
      while (rootCause.getCause() != null) {
        rootCause = rootCause.getCause();
      }
      assertThat(rootCause, allOf(instanceOf(IOException.class),
        hasProperty("message", containsString("not a gzip file"))));
      assertTrue(fs.exists(tmpPath));
      return;
    }
    fail("Compaction should have failed due to corrupt block");
  }
This test uses a hand-modified HFile, which is loaded in from the resources' path. That file
was generated from the test support code in this class and then edited to corrupt the
GZ-encoded block by zeroing-out the first two bytes of the GZip header, the "standard
declaration" of {@code 1f 8b}, found at offset 33 in the file. I'm not sure why, but it seems
that in this test context we do not enforce CRC checksums. Thus, this corruption manifests in
the Decompressor rather than in the reader when it loads the block bytes and compares vs. the
header.
hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompaction.java