@Test
public void testCompareFileLengthsAndChecksums() throws Throwable {

  String base = "/tmp/verify-checksum/";
  long srcSeed = System.currentTimeMillis();
  long dstSeed = srcSeed + rand.nextLong();
  short replFactor = 2;
  FileSystem fs = FileSystem.get(config);
  Path basePath = new Path(base);
  fs.mkdirs(basePath);
  // empty lengths comparison
  Path srcWithLen0 = new Path(base + "srcLen0");
  Path dstWithLen0 = new Path(base + "dstLen0");
  fs.create(srcWithLen0).close();
  fs.create(dstWithLen0).close();
  DistCpUtils.compareFileLengthsAndChecksums(0, fs, srcWithLen0,
      null, fs, dstWithLen0, false, 0);
  // different lengths comparison
  Path srcWithLen1 = new Path(base + "srcLen1");
  Path dstWithLen2 = new Path(base + "dstLen2");
  DFSTestUtil.createFile(fs, srcWithLen1, 1, replFactor, srcSeed);
  DFSTestUtil.createFile(fs, dstWithLen2, 2, replFactor, srcSeed);
  intercept(IOException.class, DistCpConstants.LENGTH_MISMATCH_ERROR_MSG,
      () -> DistCpUtils.compareFileLengthsAndChecksums(1, fs,
              srcWithLen1, null, fs, dstWithLen2, false, 2));
  // checksums matched
  Path srcWithChecksum1 = new Path(base + "srcChecksum1");
  Path dstWithChecksum1 = new Path(base + "dstChecksum1");
  DFSTestUtil.createFile(fs, srcWithChecksum1, 1024,
      replFactor, srcSeed);
  DFSTestUtil.createFile(fs, dstWithChecksum1, 1024,
      replFactor, srcSeed);
  DistCpUtils.compareFileLengthsAndChecksums(1024, fs, srcWithChecksum1,
      null, fs, dstWithChecksum1, false, 1024);
  DistCpUtils.compareFileLengthsAndChecksums(1024, fs, srcWithChecksum1,
      fs.getFileChecksum(srcWithChecksum1), fs, dstWithChecksum1,
      false, 1024);
  // checksums mismatched
  Path dstWithChecksum2 = new Path(base + "dstChecksum2");
  DFSTestUtil.createFile(fs, dstWithChecksum2, 1024,
      replFactor, dstSeed);
  intercept(IOException.class, DistCpConstants.CHECKSUM_MISMATCH_ERROR_MSG,
      () -> DistCpUtils.compareFileLengthsAndChecksums(1024, fs,
             srcWithChecksum1, null, fs, dstWithChecksum2,
             false, 1024));
  // checksums mismatched but skipped
  DistCpUtils.compareFileLengthsAndChecksums(1024, fs, srcWithChecksum1,
      null, fs, dstWithChecksum2, true, 1024);
}