VCDiffFileBasedCoder { public static void main(String[] argv) throws Exception { List<String> newArgv = new LinkedList<String>(Arrays.asList(argv)); ListIterator<String> i = newArgv.listIterator(); while (i.hasNext()) { String arg = i.next(); if (arg.matches("--?allow_vcd_target") && i.hasNext()) { String val = i.next(); if (val.matches("(?i)true|yes|1")) { i.remove(); } else if (val.matches("(?i)false|no|0")) { i.remove(); i.previous(); i.remove(); } else { i.previous(); } } else if (arg.matches("--?allow_vcd_target(=.*)?")) { if (arg.matches("(?i)--?allow_vcd_target=(true|yes|1)")) { i.set("-allow_vcd_target"); } else if (arg.matches("(?i)--?allow_vcd_target=(false|no|0)")) { i.remove(); } } argv = newArgv.toArray(new String[newArgv.size()]); } EncodeCommand encodeCommand = new EncodeCommand(); DecodeCommand decodeCommand = new DecodeCommand(); DecodeAndCompareCommand decodeAndCompareCommand = new DecodeAndCompareCommand(); JCommander jCommander = new JCommander(); jCommander.addCommand("encode", encodeCommand, "delta"); jCommander.addCommand("decode", decodeCommand, "patch"); jCommander.addCommand("test", decodeAndCompareCommand); try { jCommander.parse(argv); } catch (ParameterException e) { System.err.println(e.getMessage()); System.exit(1); } String command_name = VCDiffFileBasedCoder.class.getCanonicalName(); String command_option = jCommander.getParsedCommand(); try { if ("encode".equals(command_option) || "delta".equals(command_option)) { encodeCommand.Encode(); } else if ("decode".equals(command_option) || "patch".equals(command_option)) { decodeCommand.Decode(); } else if ("test".equals(command_option)) { JCommander jCommander2 = new JCommander(); jCommander2.addObject(encodeCommand); jCommander2.parse(Arrays.copyOfRange(argv, 1, argv.length)); encodeCommand.Encode(); decodeAndCompareCommand.DecodeAndCompare(); } else { System.err.printf("%s: Unrecognized command option %s%n", command_name, command_option); jCommander.usage(); System.exit(1); } } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } } private  VCDiffFileBasedCoder(); static void main(String[] argv); static final int DEFAULT_MAX_TARGET_SIZE; }
@Test public void testVCDiffNoInterleavedAndChecksum() throws Exception { VCDiffFileBasedCoder.main(new String[] { "encode", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); }
@Test public void testVCDiffTargetMatches() throws Exception { VCDiffFileBasedCoder.main(new String[] { "encode", "-target_matches", "-stats", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); }
@Test public void testUnrecognizedCommand() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "dencode", "-target_matches", "-stats", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); }
@Test public void testTestWithoutDelta() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "test", "-target_matches", "-stats", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), }); }
@Test public void testTestWithoutTarget() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "test", "-target_matches", "-stats", "-delta", deltaFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), }); }
@Test public void testTestWithoutDictionary() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "test", "-target_matches", "-delta", deltaFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath() }); }
@Test public void testMaliciousEncodingWithMaxTargetFileSize() throws Exception { exit.expectSystemExit(); PrintStream outBackup = System.out; try { System.setOut(new PrintStream(nopOutputStream())); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", maliciousEncoding.getCanonicalPath(), "-max_target_file_size", "65536" }); } finally { System.setOut(outBackup); } }
@Test public void testMaliciousEncodingWithMaxTargetWindowSize() throws Exception { exit.expectSystemExit(); PrintStream outBackup = System.out; try { System.setOut(new PrintStream(nopOutputStream())); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", maliciousEncoding.getCanonicalPath(), "-max_target_window_size", "65536" }); } finally { System.setOut(outBackup); } }
@Test public void testWithMaxTargetFileSize() throws Exception { VCDiffFileBasedCoder.main(new String[] { "test", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-max_target_file_size", "65536" }); }
@Test public void testWithMaxTargetWindowSize() throws Exception { VCDiffFileBasedCoder.main(new String[] { "test", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-max_target_window_size", "65536" }); }
@Test public void testDisallowVCDTarget() throws Exception { VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); VCDiffFileBasedCoder.main(new String[] { "decode", "-allow_vcd_target=false", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); }
@Test public void testAllowVCDTarget() throws Exception { VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); VCDiffFileBasedCoder.main(new String[] { "decode", "-allow_vcd_target=true", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); }
@Test public void testDecodeReferenceDeltas() throws Exception { for (int i = 0; i < 6; i++) { String filename = String.format("configure.ac.vcdiff.%d", i); copyResourceToFile(deltaFile, filename); VCDiffFileBasedCoder.main(new String[] { "decode", "-allow_vcd_target=true", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); } }
@Test public void testDecodeXDelta3() throws Exception { copyResourceToFile(deltaFile, "xdelta3.vcdiff"); VCDiffFileBasedCoder.main(new String[] { "decode", "-allow_vcd_target=true", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); }
@Test public void testNoArguments() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[0]); }
@Test public void testNoCommand() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[]{ "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); }
@Test public void testVCDiff() throws Exception { VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); }
@Test public void testVCDiffWithStdio() throws Exception { InputStream inBackup; PrintStream outBackup; inBackup = System.in; try { outBackup = System.out; try (InputStream in = new FileInputStream(targetFile); OutputStream out = new FileOutputStream(deltaFile)) { System.setIn(in); System.setOut(new PrintStream(out)); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), }); } finally { System.setOut(outBackup); } } finally { System.setIn(inBackup); } inBackup = System.in; try { outBackup = System.out; try (InputStream in = new FileInputStream(deltaFile); OutputStream out = new FileOutputStream(outputTargetFile)) { System.setIn(in); System.setOut(new PrintStream(out)); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", dictionaryFile.getCanonicalPath(), }); } finally { System.setOut(outBackup); } } finally { System.setIn(inBackup); } assertFileEquals(targetFile, outputTargetFile); }
@Test public void testVCDiffWithStdout() throws Exception { PrintStream outBackup = System.out; try { try (OutputStream deltaOut = new FileOutputStream(deltaFile)) { System.setOut(new PrintStream(deltaOut)); VCDiffFileBasedCoder.main(new String[]{ "encode", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), }); } } finally { System.setOut(outBackup); } outBackup = System.out; try { try (OutputStream targetOut = new FileOutputStream(outputTargetFile)) { System.setOut(new PrintStream(targetOut)); VCDiffFileBasedCoder.main(new String[]{ "decode", "-delta", deltaFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), }); } } finally { System.setOut(outBackup); } assertFileEquals(targetFile, outputTargetFile); }
@Test public void testVCDiffWithStdin() throws Exception { InputStream inBackup; inBackup = System.in; try { try (InputStream in = new FileInputStream(targetFile)) { System.setIn(in); VCDiffFileBasedCoder.main(new String[]{ "encode", "-interleaved", "-checksum", "-delta", deltaFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), }); } } finally { System.setIn(inBackup); } inBackup = System.in; try { try (InputStream in = new FileInputStream(deltaFile)) { System.setIn(in); VCDiffFileBasedCoder.main(new String[]{ "decode", "-target", outputTargetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), }); } } finally { System.setIn(inBackup); } assertFileEquals(targetFile, outputTargetFile); }
@Test public void testWrongDictionary() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", targetFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); }
@Test public void testVCDiffTest() throws Exception { VCDiffFileBasedCoder.main(new String[] { "test", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath() }); }
@Test public void testDictionaryNotFound() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", tempFolder.getRoot().getCanonicalPath() + File.separator + "nonexistent_file", "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); }
@Test public void testTargetNotFound() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", tempFolder.getRoot().getCanonicalPath() + File.separator + "nonexistent_file", "-delta", deltaFile.getCanonicalPath() }); }
@Test public void testDeltaNotFound() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", tempFolder.getRoot().getCanonicalPath() + File.separator + "nonexistent_file", "-target", outputTargetFile.getCanonicalPath() }); }
@Test public void testStats() throws Exception { VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-stats", "-dictionary", dictionaryFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); VCDiffFileBasedCoder.main(new String[] { "decode", "-stats", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), }); assertFileEquals(targetFile, outputTargetFile); }
@Test public void testEmptyDictionary() throws Exception { File emptyDictionary = tempFolder.newFile("empty_dictionary"); VCDiffFileBasedCoder.main(new String[] { "test", "-interleaved", "-checksum", "-stats", "-dictionary", emptyDictionary.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath() }); }
@Test public void testEmptyUnreadableDictionary() throws Exception { exit.expectSystemExit(); File writeOnlyDictionary = tempFolder.newFile("write_only_dictionary"); assumeTrue("Couldn't create a read-only file", writeOnlyDictionary.setReadable(false)); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", writeOnlyDictionary.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath() }); }
@Test public void testEmptyUnreadableTarget() throws Exception { exit.expectSystemExit(); File writeOnlyTarget = tempFolder.newFile("write_only_target"); assumeTrue("Couldn't create a read-only file", writeOnlyTarget.setReadable(false)); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-target", writeOnlyTarget.getCanonicalPath() }); }
@Test public void testInvalidDelta() throws Exception { exit.expectSystemExit(); File invalidDeltaFile = tempFolder.newFile("invalid_delta"); Random random = new Random(42); try (FileOutputStream out = new FileOutputStream(invalidDeltaFile)) { for (int i = 0; i < 97; i++) { out.write(random.nextInt()); } } VCDiffFileBasedCoder.main(new String[] { "decode", "-dictionary", targetFile.getCanonicalPath(), "-target", outputTargetFile.getCanonicalPath(), "-delta", invalidDeltaFile.getCanonicalPath() }); }
@Test public void missingDictionary() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-delta", deltaFile.getCanonicalPath(), "-target", targetFile.getCanonicalPath(), "-dictionary" }); }
@Test public void testMissingTarget() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-delta", deltaFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), "-target" }); }
@Test public void testMissingDelta() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), "-delta" }); }
@Test public void testMissingBufferSize() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-buffersize" }); }
@Test public void testBufferSize() throws Exception { VCDiffFileBasedCoder.main(new String[] { "test", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-buffersize", "1", "-stats" }); }
@Test public void testBufferSizeWithStdio() throws Exception { InputStream inBackup; PrintStream outBackup; inBackup = System.in; try { outBackup = System.out; try (InputStream in = new FileInputStream(targetFile); OutputStream out = new FileOutputStream(deltaFile)) { System.setIn(in); System.setOut(new PrintStream(out)); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-buffersize", "1", "-stats", "-dictionary", dictionaryFile.getCanonicalPath(), }); } finally { System.setOut(outBackup); } } finally { System.setIn(inBackup); } inBackup = System.in; try { outBackup = System.out; try (InputStream in = new FileInputStream(deltaFile); OutputStream out = new FileOutputStream(outputTargetFile)) { System.setIn(in); System.setOut(new PrintStream(out)); VCDiffFileBasedCoder.main(new String[] { "decode", "-buffersize", "1", "-stats", "-dictionary", dictionaryFile.getCanonicalPath(), }); } finally { System.setOut(outBackup); } } finally { System.setIn(inBackup); } assertFileEquals(targetFile, outputTargetFile); }
@Test public void testBufferSizeZero() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "test", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-buffersize", "0", }); }
@Test public void testLargeBufferSize() throws Exception { VCDiffFileBasedCoder.main(new String[] { "test", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-buffersize", "134217728", }); }
@Test public void testUnrecognizedOption() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "test", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-dictionary", dictionaryFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath(), "-froobish" }); }
@Test public void testEncodeMissingDictionary() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "encode", "-interleaved", "-checksum", "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); }
@Test public void testDecodeMissingDictionary() throws Exception { exit.expectSystemExit(); VCDiffFileBasedCoder.main(new String[] { "decode", "-target", targetFile.getCanonicalPath(), "-delta", deltaFile.getCanonicalPath() }); }