|
| 1 | +package org.cryptomator.integrations.update; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.NullAndEmptySource; |
| 7 | +import org.junit.jupiter.params.provider.ValueSource; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | + |
| 13 | +public class DownloadUpdateMechanismTest { |
| 14 | + |
| 15 | + private final DownloadUpdateMechanism mechanism = new DownloadUpdateMechanism() { |
| 16 | + @Override |
| 17 | + public UpdateStep secondStep(Path workDir, Path assetPath, DownloadUpdateInfo updateInfo) { |
| 18 | + throw new UnsupportedOperationException(); |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + protected DownloadUpdateInfo checkForUpdate(String currentVersion, LatestVersionResponse response) { |
| 23 | + throw new UnsupportedOperationException(); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(strings = { |
| 29 | + "Cryptomator-1.18.0-x64.msi", |
| 30 | + "Cryptomator-1.18.0-x64.exe", |
| 31 | + "Cryptomator-1.18.0-arm64.dmg", |
| 32 | + "cryptomator-1.18.0-x86_64.AppImage", |
| 33 | + "Cryptomator-1.18.0-beta1+build.42.dmg", |
| 34 | + "a" |
| 35 | + }) |
| 36 | + public void testSafeAssetNames(String name) { |
| 37 | + Assertions.assertTrue(DownloadUpdateMechanism.isSafeAssetName(name)); |
| 38 | + } |
| 39 | + |
| 40 | + @ParameterizedTest |
| 41 | + @NullAndEmptySource |
| 42 | + @ValueSource(strings = { |
| 43 | + ".", |
| 44 | + "..", |
| 45 | + ".hidden.exe", |
| 46 | + "-rf.exe", |
| 47 | + "../Cryptomator-x64.exe", |
| 48 | + "..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe", |
| 49 | + "sub/Cryptomator-x64.exe", |
| 50 | + "/etc/Cryptomator.AppImage", |
| 51 | + "C:\\Cryptomator-x64.exe", |
| 52 | + "C:Cryptomator-x64.exe", |
| 53 | + "Cryptomator-x64.exe:stream", |
| 54 | + "Cryptomator x64.exe", |
| 55 | + "Cryptomator\t.exe", |
| 56 | + "Cryptomator\n.exe", |
| 57 | + "Cryptomator\u0000.exe", |
| 58 | + "Cryptomator&calc.exe", |
| 59 | + "Cryptomator%TEMP%.exe", |
| 60 | + "Cryptomator$(id).AppImage", |
| 61 | + "Cryptomator\".exe", |
| 62 | + "Cryptomätor.exe" |
| 63 | + }) |
| 64 | + public void testUnsafeAssetNames(String name) { |
| 65 | + Assertions.assertFalse(DownloadUpdateMechanism.isSafeAssetName(name)); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @NullAndEmptySource |
| 70 | + @ValueSource(strings = { |
| 71 | + "..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe", |
| 72 | + "../../.local/share/applications/cryptomator.desktop" |
| 73 | + }) |
| 74 | + public void testFirstStepRejectsUnsafeAssetName(String name) { |
| 75 | + var updateInfo = updateInfo(name, "sha256:" + "00".repeat(32)); |
| 76 | + |
| 77 | + Assertions.assertThrows(UpdateFailedException.class, () -> mechanism.firstStep(updateInfo)); |
| 78 | + } |
| 79 | + |
| 80 | + @ParameterizedTest |
| 81 | + @ValueSource(strings = { |
| 82 | + "sha256:0", |
| 83 | + "sha256:xyz", |
| 84 | + "sha256:-0123456789abcdef" |
| 85 | + }) |
| 86 | + public void testFirstStepRejectsMalformedDigest(String digest) { |
| 87 | + var updateInfo = updateInfo("Cryptomator-1.18.0-x64.msi", digest); |
| 88 | + |
| 89 | + Assertions.assertThrows(UpdateFailedException.class, () -> mechanism.firstStep(updateInfo)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testFirstStepAcceptsSafeAssetName() throws IOException { |
| 94 | + var updateInfo = updateInfo("Cryptomator-1.18.0-x64.msi", "sha256:" + "00".repeat(32)); |
| 95 | + |
| 96 | + var step = Assertions.assertInstanceOf(DownloadUpdateStep.class, mechanism.firstStep(updateInfo)); |
| 97 | + var workDir = step.destination.getParent(); |
| 98 | + try { |
| 99 | + Assertions.assertEquals("Cryptomator-1.18.0-x64.msi", step.destination.getFileName().toString()); |
| 100 | + Assertions.assertTrue(Files.isDirectory(workDir)); |
| 101 | + } finally { |
| 102 | + Files.deleteIfExists(workDir); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private DownloadUpdateInfo updateInfo(String assetName, String digest) { |
| 107 | + var asset = new DownloadUpdateMechanism.Asset(assetName, digest, 42L, "https://example.com/" + assetName); |
| 108 | + return new DownloadUpdateInfo(mechanism, "1.18.0", asset); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments