-
Notifications
You must be signed in to change notification settings - Fork 35
Store DirId for every cleartext dir encrypted inside the (cipher) directory #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fe3ac28
impl idea
infeo 60f518f
Merge branch 'develop' into feature/113-dirId-stored-in-dir
infeo d31e32c
fix unit tests and document backup class
infeo 210f248
Merge branch 'develop' into feature/113-dirId-stored-in-dir
infeo c86e5f1
add unit test for dirIdBackup class
infeo d31a661
add integration test for dirBackup
infeo 7653433
do not backup dirId implicitly
infeo 6e074df
remove mockito-junit runner
infeo fe9722e
Remove clearetext filter for dir.c9r
infeo aa8ced1
implement ciphertext dir filter and add unit test
infeo c6839d3
fix code smells
infeo 0338ea0
apply suggestions from code review
infeo c07e30d
Merge branch 'develop' into feature/113-dirId-stored-in-dir
infeo 65f0625
add/remove comment
infeo 1f72a65
Apply suggestions from code review
infeo 2c0b6d6
Improving integration test
infeo 310c038
Partially revert 2c0b6d6d1db43c696b4c6bd33f24468a575c41e0
infeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
src/main/java/org/cryptomator/cryptofs/DirectoryIdBackup.java
This file contains hidden or 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,46 @@ | ||
| package org.cryptomator.cryptofs; | ||
|
|
||
| import org.cryptomator.cryptofs.common.Constants; | ||
| import org.cryptomator.cryptolib.api.Cryptor; | ||
| import org.cryptomator.cryptolib.common.EncryptingWritableByteChannel; | ||
|
|
||
| import javax.inject.Inject; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.ByteChannel; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.StandardOpenOption; | ||
|
|
||
| /** | ||
| * Single purpose class to backup the directory id of an encrypted directory when it is created. | ||
| */ | ||
| @CryptoFileSystemScoped | ||
| public class DirectoryIdBackup { | ||
|
|
||
| private Cryptor cryptor; | ||
|
|
||
| @Inject | ||
| public DirectoryIdBackup(Cryptor cryptor) { | ||
| this.cryptor = cryptor; | ||
| } | ||
|
|
||
| /** | ||
| * Performs the backup operation for the given {@link CryptoPathMapper.CiphertextDirectory} object. | ||
| * <p> | ||
| * The directory id is written via an encrypting channel to the file {@link CryptoPathMapper.CiphertextDirectory#path}/{@value Constants#DIR_ID_FILE}. | ||
| * | ||
| * @param ciphertextDirectory The cipher dir object containing the dir id and the encrypted content root | ||
| * @throws IOException if an IOException is raised during the write operation | ||
| */ | ||
| public void execute(CryptoPathMapper.CiphertextDirectory ciphertextDirectory) throws IOException { | ||
| try (var channel = Files.newByteChannel(ciphertextDirectory.path.resolve(Constants.DIR_ID_FILE), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); // | ||
| var encryptingChannel = wrapEncryptionAround(channel, cryptor)) { | ||
| encryptingChannel.write(ByteBuffer.wrap(ciphertextDirectory.dirId.getBytes(StandardCharsets.UTF_8))); | ||
| } | ||
| } | ||
|
|
||
| static EncryptingWritableByteChannel wrapEncryptionAround(ByteChannel channel, Cryptor cryptor) { | ||
| return new EncryptingWritableByteChannel(channel, cryptor); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
67 changes: 67 additions & 0 deletions
67
src/test/java/org/cryptomator/cryptofs/DirectoryIdBackupTest.java
This file contains hidden or 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,67 @@ | ||
| package org.cryptomator.cryptofs; | ||
|
|
||
| import org.cryptomator.cryptofs.common.Constants; | ||
| import org.cryptomator.cryptolib.api.Cryptor; | ||
| import org.cryptomator.cryptolib.common.EncryptingWritableByteChannel; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class DirectoryIdBackupTest { | ||
|
|
||
| @TempDir | ||
| Path contentPath; | ||
|
|
||
| private String dirId = "12345678"; | ||
| private CryptoPathMapper.CiphertextDirectory cipherDirObject; | ||
| private EncryptingWritableByteChannel encChannel; | ||
| private Cryptor cryptor; | ||
|
|
||
| private DirectoryIdBackup dirIdBackup; | ||
|
|
||
|
|
||
| @BeforeEach | ||
| public void init() { | ||
| cipherDirObject = new CryptoPathMapper.CiphertextDirectory(dirId, contentPath); | ||
| cryptor = Mockito.mock(Cryptor.class); | ||
| encChannel = Mockito.mock(EncryptingWritableByteChannel.class); | ||
|
|
||
| dirIdBackup = new DirectoryIdBackup(cryptor); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIdFileCreated() throws IOException { | ||
| try (MockedStatic<DirectoryIdBackup> backupMock = Mockito.mockStatic(DirectoryIdBackup.class)) { | ||
| backupMock.when(() -> DirectoryIdBackup.wrapEncryptionAround(Mockito.any(), Mockito.eq(cryptor))).thenReturn(encChannel); | ||
| Mockito.when(encChannel.write(Mockito.any())).thenReturn(0); | ||
|
|
||
| dirIdBackup.execute(cipherDirObject); | ||
|
|
||
| Assertions.assertTrue(Files.exists(contentPath.resolve(Constants.DIR_ID_FILE))); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testContentIsWritten() throws IOException { | ||
| Mockito.when(encChannel.write(Mockito.any())).thenReturn(0); | ||
| var expectedWrittenContent = ByteBuffer.wrap(dirId.getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| try (MockedStatic<DirectoryIdBackup> backupMock = Mockito.mockStatic(DirectoryIdBackup.class)) { | ||
| backupMock.when(() -> DirectoryIdBackup.wrapEncryptionAround(Mockito.any(), Mockito.eq(cryptor))).thenReturn(encChannel); | ||
infeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| dirIdBackup.execute(cipherDirObject); | ||
|
|
||
| Mockito.verify(encChannel, Mockito.times(1)).write(Mockito.argThat(b -> b.equals(expectedWrittenContent))); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.