-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/2.0.0' into release/2.1.0
- Loading branch information
Showing
6 changed files
with
171 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.cryptomator.cryptofs; | ||
|
||
import org.cryptomator.cryptofs.common.Constants; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.NotDirectoryException; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
|
||
/** | ||
* Enumeration of the vault directory structure resemblances. | ||
* <p> | ||
* A valid vault must contain a `d` directory. | ||
* Beginning with vault format 8, it must also contain a vault config file. | ||
* If the vault format is lower than 8, it must instead contain a masterkey file. | ||
* <p> | ||
* In the latter case, to distinguish between a damaged vault 8 directory and a legacy vault the masterkey file must be read. | ||
* For efficiency reasons, this class only checks for existence/readability of the above elements. | ||
* Hence, if the result of {@link #checkDirStructure(Path, String, String)} is {@link #MAYBE_LEGACY}, one needs to parse | ||
* the masterkey file and read out the vault version to determine this case. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public enum DirStructure { | ||
|
||
/** | ||
* Dir contains a <code>d</code> dir as well as a vault config file. | ||
*/ | ||
VAULT, | ||
|
||
/** | ||
* Dir contains a <code>d</code> dir and a masterkey file, but misses a vault config file. | ||
* Either needs migration to a newer format or damaged. | ||
*/ | ||
MAYBE_LEGACY, | ||
|
||
/** | ||
* Dir does not qualify as vault. | ||
*/ | ||
UNRELATED; | ||
|
||
|
||
/** | ||
* Analyzes the structure of the given directory under certain vault existence criteria. | ||
* | ||
* @param pathToVault A directory path | ||
* @param vaultConfigFilename Name of the vault config file | ||
* @param masterkeyFilename Name of the masterkey file | ||
* @return enum indicating what this directory might be | ||
* @throws IOException if the provided path is not a directory, does not exist or cannot be read | ||
*/ | ||
public static DirStructure checkDirStructure(Path pathToVault, String vaultConfigFilename, String masterkeyFilename) throws IOException { | ||
if(! Files.readAttributes(pathToVault, BasicFileAttributes.class).isDirectory()) { | ||
throw new NotDirectoryException(pathToVault.toString()); | ||
} | ||
Path vaultConfigPath = pathToVault.resolve(vaultConfigFilename); | ||
Path masterkeyPath = pathToVault.resolve(masterkeyFilename); | ||
Path dataDirPath = pathToVault.resolve(Constants.DATA_DIR_NAME); | ||
if (Files.isDirectory(dataDirPath)) { | ||
if (Files.isReadable(vaultConfigPath)) { | ||
return VAULT; | ||
} else if (Files.isReadable(masterkeyPath)) { | ||
return MAYBE_LEGACY; | ||
} | ||
} | ||
return UNRELATED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/test/java/org/cryptomator/cryptofs/DirStructureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.cryptomator.cryptofs; | ||
|
||
import org.cryptomator.cryptofs.common.Constants; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Assumptions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
public class DirStructureTest { | ||
|
||
private static final String KEY = "key"; | ||
private static final String CONFIG = "config"; | ||
|
||
@TempDir | ||
Path vaultPath; | ||
|
||
@Test | ||
public void testNonExistingVaultPathThrowsIOException() { | ||
Path vaultPath = Path.of("this/certainly/does/not/exist"); | ||
Assumptions.assumeTrue(Files.notExists(vaultPath)); | ||
|
||
Assertions.assertThrows(IOException.class, () -> DirStructure.checkDirStructure(vaultPath, CONFIG, KEY)); | ||
} | ||
|
||
@Test | ||
public void testNonDirectoryVaultPathThrowsIOException() throws IOException { | ||
Path tmp = vaultPath.resolve("this"); | ||
Files.createFile(tmp); | ||
Assumptions.assumeTrue(Files.exists(tmp)); | ||
|
||
Assertions.assertThrows(IOException.class, () -> DirStructure.checkDirStructure(tmp, CONFIG, KEY)); | ||
} | ||
|
||
@ParameterizedTest(name = "Testing all combinations of data dir, config and masterkey file existence.") | ||
@MethodSource("provideAllCases") | ||
public void testAllCombosOfDataAndConfigAndKey(boolean createDataDir, boolean createConfig, boolean createKey, DirStructure expectedResult) throws IOException { | ||
Path keyPath = vaultPath.resolve(KEY); | ||
Path configPath = vaultPath.resolve(CONFIG); | ||
Path dataDir = vaultPath.resolve(Constants.DATA_DIR_NAME); | ||
|
||
if (createDataDir) { | ||
Files.createDirectory(dataDir); | ||
} | ||
if (createConfig) { | ||
Files.createFile(configPath); | ||
} | ||
if (createKey) { | ||
Files.createFile(keyPath); | ||
} | ||
|
||
Assertions.assertEquals(expectedResult, DirStructure.checkDirStructure(vaultPath, CONFIG, KEY)); | ||
} | ||
|
||
private static Stream<Arguments> provideAllCases() { | ||
return Stream.of( | ||
Arguments.of(true, true, true, DirStructure.VAULT), | ||
Arguments.of(true, true, false, DirStructure.VAULT), | ||
Arguments.of(true, false, true, DirStructure.MAYBE_LEGACY), | ||
Arguments.of(true, false, false, DirStructure.UNRELATED), | ||
Arguments.of(false, false, false, DirStructure.UNRELATED), | ||
Arguments.of(false, false, true, DirStructure.UNRELATED), | ||
Arguments.of(false, true, false, DirStructure.UNRELATED), | ||
Arguments.of(false, true, true, DirStructure.UNRELATED) | ||
); | ||
} | ||
|
||
} |