Skip to content

Commit

Permalink
Merge branch 'release/2.5.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Dec 5, 2022
2 parents 2f42f3c + 010b052 commit e1e07f3
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.cryptomator</groupId>
<artifactId>cryptofs</artifactId>
<version>2.5.1</version>
<version>2.5.2</version>
<name>Cryptomator Crypto Filesystem</name>
<description>This library provides the Java filesystem provider used by Cryptomator.</description>
<url>https://github.com/cryptomator/cryptofs</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Map<String, String> details() {
//visible for testing
void fix(Path pathToVault, Cryptor cryptor) throws IOException {
var dirIdHash = cryptor.fileNameCryptor().hashDirectoryId(dirId);
Path dirPath = pathToVault.resolve(Constants.DATA_DIR_NAME).resolve(dirIdHash.substring(0, 2)).resolve(dirIdHash.substring(2, 30));
Path dirPath = pathToVault.resolve(Constants.DATA_DIR_NAME).resolve(dirIdHash.substring(0, 2)).resolve(dirIdHash.substring(2, 32));
Files.createDirectories(dirPath);
DirectoryIdBackup.backupManually(cryptor, new CryptoPathMapper.CiphertextDirectory(dirId, dirPath));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void fix(Path pathToVault, VaultConfig config, Cryptor cryptor) throws I
try {
return decryptFileName(orphanedResource, isShortened, id, cryptor.fileNameCryptor());
} catch (IOException | AuthenticationFailedException e) {
LOG.warn("Unable to read and decrypt (long) file name of {}:", orphanedResource, e);
LOG.warn("Unable to read and decrypt file name of {}:", orphanedResource, e);
return null;
}})
.orElseGet(() ->
Expand Down Expand Up @@ -182,15 +182,7 @@ Optional<String> retrieveDirId(Path orphanedDir, Cryptor cryptor) {
return Optional.empty();
}

var allegedDirId = StandardCharsets.US_ASCII.decode(dirIdBuffer).toString();

var dirIdHash = orphanedDir.getParent().getFileName().toString() + orphanedDir.getFileName().toString();
if (dirIdHash.equals(cryptor.fileNameCryptor().hashDirectoryId(allegedDirId))) {
return Optional.of(allegedDirId);
} else {
LOG.info("Hash of read directory id {} does not match actual cipher dir hash {}.", allegedDirId, dirIdHash);
return Optional.empty();
}
return Optional.of(StandardCharsets.US_ASCII.decode(dirIdBuffer).toString());
}

//exists and visible for testability
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public void testGetFix() {
@DisplayName("After fix the content dir including dirId file exists ")
@Test
public void testFix() throws IOException {
var dirIdHash = "ridiculous-30-char-pseudo-hash";
var dirIdHash = "ridiculous-32-char-pseudo-hashhh";
Mockito.doReturn(dirIdHash).when(fileNameCryptor).hashDirectoryId(dirId);
try (var dirIdBackupMock = Mockito.mockStatic(DirectoryIdBackup.class)) {
dirIdBackupMock.when(() -> DirectoryIdBackup.backupManually(Mockito.any(), Mockito.any())).thenAnswer(Answers.RETURNS_SMART_NULLS);

result.fix(pathToVault, cryptor);

var expectedPath = pathToVault.resolve("d/ri/diculous-30-char-pseudo-hash");
var expectedPath = pathToVault.resolve("d/ri/diculous-32-char-pseudo-hashhh");
ArgumentMatcher<CryptoPathMapper.CiphertextDirectory> cipherDirMatcher = obj -> obj.dirId.equals(dirId) && obj.path.endsWith(expectedPath);
dirIdBackupMock.verify(() -> DirectoryIdBackup.backupManually(Mockito.eq(cryptor), Mockito.argThat(cipherDirMatcher)), Mockito.times(1));
var attr = Assertions.assertDoesNotThrow(() -> Files.readAttributes(expectedPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS));
Expand All @@ -70,7 +70,7 @@ public void testFix() throws IOException {
@DisplayName("If dirid.c9r creation fails, fix fails ")
@Test
public void testFixFailsOnFailingDirIdFile() throws IOException {
var dirIdHash = "ridiculous-30-char-pseudo-hash";
var dirIdHash = "ridiculous-32-char-pseudo-hashhh";
try (var dirIdBackupMock = Mockito.mockStatic(DirectoryIdBackup.class)) {
Mockito.doReturn(dirIdHash).when(fileNameCryptor).hashDirectoryId(dirId);
dirIdBackupMock.when(() -> DirectoryIdBackup.backupManually(Mockito.any(), Mockito.any())).thenThrow(new IOException("Access denied"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,28 +270,6 @@ public void testRetrieveDirIdIOExceptionReadingFile() throws IOException {
Assertions.assertTrue(notExistingResult.isEmpty());
}


@Test
@DisplayName("retrieveDirId returns empty optional if content of dirId.c9r does not match cipher dir hash")
public void testRetrieveDirIdWrongContent() throws IOException {
var dirIdFile = cipherOrphan.resolve(Constants.DIR_BACKUP_FILE_NAME);
var dirId = "anOverlyComplexAndCompletelyRandomExampleOfHowAnDirectoryIdIsTooLong";
Files.writeString(dirIdFile, dirId, StandardCharsets.US_ASCII, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
DecryptingReadableByteChannel dirIdReadChannel = Mockito.mock(DecryptingReadableByteChannel.class);

Mockito.doReturn(dirIdReadChannel).when(resultSpy).createDecryptingReadableByteChannel(Mockito.any(), Mockito.eq(cryptor));
Mockito.doAnswer(invocationOnMock -> {
try (ReadableByteChannel channel = Files.newByteChannel(dirIdFile, StandardOpenOption.READ)) {
return channel.read(invocationOnMock.getArgument(0));
}
}).when(dirIdReadChannel).read(Mockito.any());
Mockito.when(fileNameCryptor.hashDirectoryId(dirId.substring(0, 36))).thenReturn("123456");

var maybeDirId = resultSpy.retrieveDirId(cipherOrphan, cryptor);

Assertions.assertTrue(maybeDirId.isEmpty());
}

}


Expand Down

0 comments on commit e1e07f3

Please sign in to comment.