Search in sources :

Example 6 with Directory

use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.

the class CryptoDirectoryStreamIntegrationTest method setup.

@Before
public void setup() throws IOException {
    fileSystem = Jimfs.newFileSystem();
    Path dir = fileSystem.getPath("crapDirDoNotUse");
    Files.createDirectory(dir);
    inTest = new CryptoDirectoryStream(new Directory("", dir), null, null, null, longFileNameProvider, null, null, null, null, null);
}
Also used : Path(java.nio.file.Path) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory) Before(org.junit.Before)

Example 7 with Directory

use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.

the class CryptoDirectoryStreamTest method testDirListingForEmptyDir.

@Test(expected = NoSuchElementException.class)
public void testDirListingForEmptyDir() throws IOException {
    Path cleartextPath = Paths.get("/foo/bar");
    Mockito.when(dirStream.spliterator()).thenReturn(Spliterators.emptySpliterator());
    try (CryptoDirectoryStream stream = new CryptoDirectoryStream(new Directory("foo", ciphertextDirPath), cleartextPath, filenameCryptor, cryptoPathMapper, longFileNameProvider, conflictResolver, ACCEPT_ALL, DO_NOTHING_ON_CLOSE, finallyUtil, encryptedNamePattern)) {
        Iterator<Path> iter = stream.iterator();
        Assert.assertFalse(iter.hasNext());
        iter.next();
    }
}
Also used : Path(java.nio.file.Path) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory) Test(org.junit.Test)

Example 8 with Directory

use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.

the class CryptoDirectoryStreamTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws IOException {
    filenameCryptor = CRYPTOR_PROVIDER.createNew().fileNameCryptor();
    ciphertextDirPath = Mockito.mock(Path.class);
    FileSystem fs = Mockito.mock(FileSystem.class);
    Mockito.when(ciphertextDirPath.getFileSystem()).thenReturn(fs);
    FileSystemProvider provider = Mockito.mock(FileSystemProvider.class);
    Mockito.when(fs.provider()).thenReturn(provider);
    dirStream = Mockito.mock(DirectoryStream.class);
    Mockito.when(provider.newDirectoryStream(Mockito.same(ciphertextDirPath), Mockito.any())).thenReturn(dirStream);
    longFileNameProvider = Mockito.mock(LongFileNameProvider.class);
    conflictResolver = Mockito.mock(ConflictResolver.class);
    finallyUtil = mock(FinallyUtil.class);
    Mockito.when(longFileNameProvider.inflate(Mockito.anyString())).then(invocation -> {
        String shortName = invocation.getArgument(0);
        if (shortName.contains("invalid")) {
            throw new IOException("invalid shortened name");
        } else {
            return StringUtils.removeEnd(shortName, ".lng");
        }
    });
    cryptoPathMapper = Mockito.mock(CryptoPathMapper.class);
    Mockito.when(cryptoPathMapper.resolveDirectory(Mockito.any())).then(invocation -> {
        Path dirFilePath = invocation.getArgument(0);
        if (dirFilePath.toString().contains("invalid")) {
            throw new IOException("Invalid directory.");
        }
        Path dirPath = Mockito.mock(Path.class);
        BasicFileAttributes attrs = Mockito.mock(BasicFileAttributes.class);
        Mockito.when(dirPath.getFileSystem()).thenReturn(fs);
        Mockito.when(provider.readAttributes(dirPath, BasicFileAttributes.class)).thenReturn(attrs);
        Mockito.when(attrs.isDirectory()).thenReturn(!dirFilePath.toString().contains("noDirectory"));
        return new Directory("asdf", dirPath);
    });
    Mockito.when(conflictResolver.resolveConflictsIfNecessary(Mockito.any(), Mockito.any())).then(returnsFirstArg());
    doAnswer(invocation -> {
        for (Object runnable : invocation.getArguments()) {
            ((RunnableThrowingException<?>) runnable).run();
        }
        return null;
    }).when(finallyUtil).guaranteeInvocationOf(any(RunnableThrowingException.class), any(RunnableThrowingException.class), any(RunnableThrowingException.class));
}
Also used : Path(java.nio.file.Path) DirectoryStream(java.nio.file.DirectoryStream) IOException(java.io.IOException) FileSystemProvider(java.nio.file.spi.FileSystemProvider) FileSystem(java.nio.file.FileSystem) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory) Before(org.junit.Before)

Example 9 with Directory

use of org.cryptomator.cryptofs.CryptoPathMapper.Directory in project cryptofs by cryptomator.

the class CryptoDirectoryStreamTest method testDirListing.

@Test
public void testDirListing() throws IOException {
    Path cleartextPath = Paths.get("/foo/bar");
    List<String> ciphertextFileNames = new ArrayList<>();
    ciphertextFileNames.add(filenameCryptor.encryptFilename("one", "foo".getBytes()));
    ciphertextFileNames.add(filenameCryptor.encryptFilename("two", "foo".getBytes()) + "_conflict");
    ciphertextFileNames.add("0" + filenameCryptor.encryptFilename("three", "foo".getBytes()));
    ciphertextFileNames.add("0invalidDirectory");
    ciphertextFileNames.add("0noDirectory");
    ciphertextFileNames.add("invalidLongName.lng");
    ciphertextFileNames.add(filenameCryptor.encryptFilename("four", "foo".getBytes()) + ".lng");
    ciphertextFileNames.add(filenameCryptor.encryptFilename("invalid", "bar".getBytes()));
    ciphertextFileNames.add("alsoInvalid");
    Mockito.when(dirStream.spliterator()).thenReturn(ciphertextFileNames.stream().map(cleartextPath::resolve).spliterator());
    try (CryptoDirectoryStream stream = new CryptoDirectoryStream(new Directory("foo", ciphertextDirPath), cleartextPath, filenameCryptor, cryptoPathMapper, longFileNameProvider, conflictResolver, ACCEPT_ALL, DO_NOTHING_ON_CLOSE, finallyUtil, encryptedNamePattern)) {
        Iterator<Path> iter = stream.iterator();
        Assert.assertTrue(iter.hasNext());
        Assert.assertEquals(cleartextPath.resolve("one"), iter.next());
        Assert.assertTrue(iter.hasNext());
        Assert.assertEquals(cleartextPath.resolve("two"), iter.next());
        Assert.assertTrue(iter.hasNext());
        Assert.assertEquals(cleartextPath.resolve("three"), iter.next());
        Assert.assertTrue(iter.hasNext());
        Assert.assertEquals(cleartextPath.resolve("four"), iter.next());
        Assert.assertFalse(iter.hasNext());
        Mockito.verify(dirStream, Mockito.never()).close();
    }
    Mockito.verify(dirStream).close();
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) Directory(org.cryptomator.cryptofs.CryptoPathMapper.Directory) Test(org.junit.Test)

Aggregations

Directory (org.cryptomator.cryptofs.CryptoPathMapper.Directory)9 Path (java.nio.file.Path)8 Test (org.junit.Test)5 IOException (java.io.IOException)2 DirectoryStream (java.nio.file.DirectoryStream)2 Before (org.junit.Before)2 UncheckedIOException (java.io.UncheckedIOException)1 FileChannel (java.nio.channels.FileChannel)1 ClosedFileSystemException (java.nio.file.ClosedFileSystemException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 FileSystem (java.nio.file.FileSystem)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 FileSystemProvider (java.nio.file.spi.FileSystemProvider)1 ArrayList (java.util.ArrayList)1