-
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.
- Loading branch information
Showing
8 changed files
with
178 additions
and
17 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
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
90 changes: 90 additions & 0 deletions
90
src/test/java/org/cryptomator/cryptofs/DirectoryIdLoaderTest.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,90 @@ | ||
package org.cryptomator.cryptofs; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SeekableByteChannel; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Path; | ||
import java.nio.file.spi.FileSystemProvider; | ||
|
||
import org.cryptomator.cryptofs.mocks.SeekableByteChannelMock; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
public class DirectoryIdLoaderTest { | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
private FileSystemProvider provider = mock(FileSystemProvider.class); | ||
private FileSystem fileSystem = mock(FileSystem.class); | ||
private Path dirFilePath = mock(Path.class); | ||
private Path otherDirFilePath = mock(Path.class); | ||
|
||
private DirectoryIdLoader inTest = new DirectoryIdLoader(); | ||
|
||
@Before | ||
public void setup() { | ||
when(dirFilePath.getFileSystem()).thenReturn(fileSystem); | ||
when(otherDirFilePath.getFileSystem()).thenReturn(fileSystem); | ||
when(fileSystem.provider()).thenReturn(provider); | ||
} | ||
|
||
@Test | ||
public void testDirectoryIdsForTwoNonExistingFilesDiffer() throws IOException { | ||
doThrow(new IOException()).when(provider).checkAccess(dirFilePath); | ||
doThrow(new IOException()).when(provider).checkAccess(otherDirFilePath); | ||
|
||
String first = inTest.load(dirFilePath); | ||
String second = inTest.load(otherDirFilePath); | ||
|
||
assertThat(first, is(not(second))); | ||
} | ||
|
||
@Test | ||
public void testDirectoryIdForNonExistingFileIsNotEmpty() throws IOException { | ||
doThrow(new IOException()).when(provider).checkAccess(dirFilePath); | ||
|
||
String result = inTest.load(dirFilePath); | ||
|
||
assertThat(result, is(notNullValue())); | ||
assertThat(result, is(not(""))); | ||
} | ||
|
||
@Test | ||
public void testDirectoryIdIsReadFromExistingFile() throws IOException { | ||
String expectedId = "asdüßT°z¬╚‗"; | ||
byte[] expectedIdBytes = expectedId.getBytes(UTF_8); | ||
SeekableByteChannel channel = new SeekableByteChannelMock(ByteBuffer.wrap(expectedIdBytes)); | ||
when(provider.newByteChannel(eq(dirFilePath), any())).thenReturn(channel); | ||
|
||
String result = inTest.load(dirFilePath); | ||
|
||
assertThat(result, is(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testIOExceptionWhenExistingFileIsEmpty() throws IOException { | ||
SeekableByteChannel channel = new SeekableByteChannelMock(ByteBuffer.allocate(0)); | ||
when(provider.newByteChannel(eq(dirFilePath), any())).thenReturn(channel); | ||
|
||
thrown.expect(IOException.class); | ||
thrown.expectMessage("Invalid, empty directory file"); | ||
|
||
inTest.load(dirFilePath); | ||
} | ||
|
||
} |
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
59 changes: 59 additions & 0 deletions
59
src/test/java/org/cryptomator/cryptofs/WriteFileWhileReadonlyChannelIsOpenTest.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,59 @@ | ||
package org.cryptomator.cryptofs; | ||
|
||
import static java.nio.file.StandardOpenOption.READ; | ||
import static org.cryptomator.cryptofs.CryptoFileSystemProperties.cryptoFileSystemProperties; | ||
import static org.cryptomator.cryptofs.CryptoFileSystemUris.createUri; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.google.common.jimfs.Jimfs; | ||
|
||
/** | ||
* @see https://github.com/cryptomator/cryptofs/issues/10 | ||
*/ | ||
public class WriteFileWhileReadonlyChannelIsOpenTest { | ||
|
||
private FileSystem inMemoryFs; | ||
private FileSystem fileSystem; | ||
private Path root; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
inMemoryFs = Jimfs.newFileSystem(); | ||
Path pathToVault = inMemoryFs.getRootDirectories().iterator().next().resolve("vault"); | ||
fileSystem = new CryptoFileSystemProvider().newFileSystem(createUri(pathToVault), cryptoFileSystemProperties().withPassphrase("asd").build()); | ||
root = fileSystem.getPath("/"); | ||
} | ||
|
||
@After | ||
public void teardown() throws IOException { | ||
fileSystem.close(); | ||
inMemoryFs.close(); | ||
} | ||
|
||
@Test | ||
public void run() throws IOException { | ||
Path file = root.resolve("file"); | ||
Files.write(file, "foo".getBytes()); | ||
@SuppressWarnings("unused") | ||
FileChannel readOnlyChannel = FileChannel.open(file, READ); | ||
|
||
try (FileChannel writableChannel = FileChannel.open(file, StandardOpenOption.WRITE)) { | ||
ByteBuffer buffer = ByteBuffer.wrap("bar".getBytes()); | ||
while (buffer.hasRemaining()) { | ||
writableChannel.write(buffer); | ||
} | ||
} | ||
} | ||
|
||
} |
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