Updater extends Task<UpdateSummary> { @Override protected UpdateSummary call() throws Exception { highestLocalVersion = UpdateFX.extractVerFromFilename(UpdateFX.findBestJar(pathToOrigJar, localUpdatesDir)); newHighestVersion = highestLocalVersion; UFXProtocol.Updates updates = processSignedIndex(downloadSignedIndex()); return new UpdateSummary(newHighestVersion, updates); } Updater(URI indexURL, String userAgent, Path localUpdatesDir,
                   Path pathToOrigJar, List<ECPoint> pubkeys, int requiredSigningThreshold); void setOverrideURLs(boolean overrideURLs);  }
@Test(expected = FileNotFoundException.class) public void test404ForIndex() throws Exception { updater = new TestUpdater(indexURL, "UnitTest", dir, null); updater.call(); }
@Test public void test404ForUpdateFile() throws Exception { byte[] b = "ignored".getBytes(); configureIndex(b, b, b); try { updater = new TestUpdater(indexURL, "UnitTest", dir, null); updater.call(); fail(); } catch (FileNotFoundException e) { assertEquals("http: } }
@Test(expected = Updater.Ex.BadUpdateHash.class) public void patchFailsHashCheck() throws Exception { byte[] fakePatch = new byte[1024]; Arrays.fill(fakePatch, (byte) 0x42); paths.put("/2.jar.bpatch", fakePatch); byte[] b = "wrong".getBytes(); configureIndex(b, b, b); updater = new TestUpdater(indexURL, "UnitTest", dir, null); updater.call(); }
@Test(expected = Updater.Ex.InsufficientSigners.class) public void insufficientSigs() throws Exception { byte[] fakePatch = new byte[1024]; Arrays.fill(fakePatch, (byte) 0x42); paths.put("/2.jar.bpatch", fakePatch); UFXProtocol.SignedUpdates.Builder builder = makeWrongIndex(); builder.clearSignatures(); paths.put("/index", builder.build().toByteArray()); updater = new TestUpdater(indexURL, "UnitTest", dir, null); updater.call(); }
@Test(expected = Updater.Ex.InsufficientSigners.class) public void unknownSig() throws Exception { byte[] fakePatch = new byte[1024]; Arrays.fill(fakePatch, (byte) 0x42); paths.put("/2.jar.bpatch", fakePatch); UFXProtocol.SignedUpdates.Builder builder = makeWrongIndex(); BigInteger evilKey = new BigInteger(256, new SecureRandom()); builder.setSignatures(0, Crypto.signMessage("msg", evilKey)); paths.put("/index", builder.build().toByteArray()); updater = new TestUpdater(indexURL, "UnitTest", dir, null); updater.call(); }
@Test(expected = Updater.Ex.InsufficientSigners.class) public void replayedSig() throws Exception { byte[] fakePatch = new byte[1024]; Arrays.fill(fakePatch, (byte) 0x42); paths.put("/2.jar.bpatch", fakePatch); UFXProtocol.SignedUpdates.Builder builder = makeWrongIndex(); builder.setSignatures(0, Crypto.signMessage("hash from some other project", privKeys.get(0))); paths.put("/index", builder.build().toByteArray()); updater = new TestUpdater(indexURL, "UnitTest", dir, null); updater.call(); }
@Test(expected = SignatureException.class) public void badSig() throws Exception { byte[] fakePatch = new byte[1024]; Arrays.fill(fakePatch, (byte) 0x42); paths.put("/2.jar.bpatch", fakePatch); UFXProtocol.SignedUpdates.Builder builder = makeWrongIndex(); builder.setSignatures(0, "bzzzz"); paths.put("/index", builder.build().toByteArray()); updater = new TestUpdater(indexURL, "UnitTest", dir, null); updater.call(); }
@Test public void updateRun() throws Exception { Path working = dir.resolve("working"); createDirectory(working); byte[] baseFile = new byte[2048]; Arrays.fill(baseFile, (byte) 1); Path baseJar = working.resolve("1.jar"); write(baseJar, baseFile, CREATE_NEW); baseFile[0] = 2; Path jar2 = working.resolve("2.jar"); write(jar2, baseFile, CREATE_NEW); baseFile[0] = 3; Path jar3 = working.resolve("3.jar"); write(jar3, baseFile, CREATE_NEW); DeltaCalculator.process(working.toAbsolutePath(), working.toAbsolutePath(), -1); Path bpatch2 = working.resolve("2.jar.bpatch"); assertTrue(exists(bpatch2)); Path bpatch3 = working.resolve("3.jar.bpatch"); assertTrue(exists(bpatch3)); byte[] bpatch1bits = readAllBytes(bpatch2); byte[] bpatch2bits = readAllBytes(bpatch3); paths.put("/2.jar.bpatch", bpatch1bits); paths.put("/3.jar.bpatch", bpatch2bits); configureIndex(sha256(readAllBytes(baseJar)), sha256(bpatch1bits), sha256(readAllBytes(jar2)), sha256(readAllBytes(jar2)), sha256(bpatch2bits), sha256(readAllBytes(jar3))); updater = new TestUpdater(indexURL, "UnitTest", dir, baseJar); UpdateSummary summary = updater.call(); assertEquals(80, workDone); assertEquals(80, workMax); byte[] bits3 = Files.readAllBytes(dir.resolve("3.jar")); assertArrayEquals(baseFile, bits3); assertEquals(3, summary.highestVersion); }
@Test public void updateRun2() throws Exception { Path working = dir.resolve("working"); createDirectory(working); byte[] baseFile = new byte[2048]; Arrays.fill(baseFile, (byte) 1); Path jar1 = working.resolve("1.jar"); write(jar1, baseFile, CREATE_NEW); baseFile[0] = 2; Path baseJar = working.resolve("2.jar"); write(baseJar, baseFile, CREATE_NEW); baseFile[0] = 3; Path jar3 = working.resolve("3.jar"); write(jar3, baseFile, CREATE_NEW); DeltaCalculator.process(working.toAbsolutePath(), working.toAbsolutePath(), -1); Path bpatch2 = working.resolve("2.jar.bpatch"); assertTrue(exists(bpatch2)); Path bpatch3 = working.resolve("3.jar.bpatch"); assertTrue(exists(bpatch3)); byte[] bpatch1bits = readAllBytes(bpatch2); byte[] bpatch2bits = readAllBytes(bpatch3); paths.put("/2.jar.bpatch", bpatch1bits); paths.put("/3.jar.bpatch", bpatch2bits); configureIndex(sha256(readAllBytes(jar1)), sha256(bpatch1bits), sha256(readAllBytes(baseJar)), sha256(readAllBytes(baseJar)), sha256(bpatch2bits), sha256(readAllBytes(jar3))); updater = new TestUpdater(indexURL, "UnitTest", dir, baseJar); UpdateSummary summary = updater.call(); byte[] bits3 = Files.readAllBytes(dir.resolve("3.jar")); assertArrayEquals(baseFile, bits3); assertEquals(3, summary.highestVersion); }
@Test public void updateRunWithPinning() throws Exception { Path working = dir.resolve("working"); createDirectory(working); byte[] baseFile = new byte[2048]; Arrays.fill(baseFile, (byte) 1); Path jar1 = working.resolve("1.jar"); write(jar1, baseFile, CREATE_NEW); baseFile[0] = 2; Path baseJar = working.resolve("2.jar"); write(baseJar, baseFile, CREATE_NEW); baseFile[0] = 3; Path jar3 = working.resolve("3.jar"); write(jar3, baseFile, CREATE_NEW); DeltaCalculator.process(working.toAbsolutePath(), working.toAbsolutePath(), -1); Path bpatch2 = working.resolve("2.jar.bpatch"); assertTrue(exists(bpatch2)); Path bpatch3 = working.resolve("3.jar.bpatch"); assertTrue(exists(bpatch3)); byte[] bpatch1bits = readAllBytes(bpatch2); byte[] bpatch2bits = readAllBytes(bpatch3); paths.put("/2.jar.bpatch", bpatch1bits); paths.put("/3.jar.bpatch", bpatch2bits); configureIndex(sha256(readAllBytes(jar1)), sha256(bpatch1bits), sha256(readAllBytes(baseJar)), sha256(readAllBytes(baseJar)), sha256(bpatch2bits), sha256(readAllBytes(jar3))); UpdateFX.pinToVersion(dir, 2); UpdateFX.pinToVersion(dir, 1); updater = new TestUpdater(indexURL, "UnitTest", dir, baseJar); UpdateSummary summary = updater.call(); byte[] bits3 = Files.readAllBytes(dir.resolve("3.jar")); assertArrayEquals(baseFile, bits3); assertEquals(3, summary.highestVersion); UpdateFX.unpin(dir); }