浏览代码

Added project filesystem-invariants-tests

* Implemented some tests to be run on multiple implementations to
demonstrate how such tests can be implemented
* Detected problems with CryptoFileSystem(NioFileSystem)
* Made CryptoFileSystem and CryptorImpl public / constructible from
other packages
Markus Kreusch 9 年之前
父节点
当前提交
3f44d9bb66

+ 1 - 1
main/filesystem-crypto/src/main/java/org/cryptomator/crypto/engine/impl/CryptorImpl.java

@@ -43,7 +43,7 @@ public class CryptorImpl implements Cryptor {
 	private final AtomicReference<FileContentCryptor> fileContentCryptor = new AtomicReference<>();
 	private final SecureRandom randomSource;
 
-	CryptorImpl(SecureRandom randomSource) {
+	public CryptorImpl(SecureRandom randomSource) {
 		this.randomSource = randomSource;
 	}
 

+ 1 - 1
main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoFileSystem.java

@@ -19,7 +19,7 @@ import org.cryptomator.filesystem.Folder;
 import org.cryptomator.filesystem.ReadableFile;
 import org.cryptomator.filesystem.WritableFile;
 
-class CryptoFileSystem extends CryptoFolder implements FileSystem {
+public class CryptoFileSystem extends CryptoFolder implements FileSystem {
 
 	private static final String DATA_ROOT_DIR = "d";
 	private static final String ROOT_DIR_FILE = "root";

+ 1 - 0
main/filesystem-invariants-tests/.gitignore

@@ -0,0 +1 @@
+/target/

+ 53 - 0
main/filesystem-invariants-tests/pom.xml

@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright (c) 2015 Markus Kreusch
+  This file is licensed under the terms of the MIT license.
+  See the LICENSE.txt file for more info.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+		<groupId>org.cryptomator</groupId>
+		<artifactId>main</artifactId>
+		<version>0.11.0-SNAPSHOT</version>
+	</parent>
+	<artifactId>filesystem-invariants-tests</artifactId>
+	<name>Cryptomator filesystem invariants tests</name>
+	<description>Test only project which checks invariants of FileSystem implementations</description>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.cryptomator</groupId>
+			<artifactId>filesystem-api</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.cryptomator</groupId>
+			<artifactId>filesystem-crypto</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.cryptomator</groupId>
+			<artifactId>filesystem-inmemory</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.cryptomator</groupId>
+			<artifactId>filesystem-nameshortening</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.cryptomator</groupId>
+			<artifactId>filesystem-nio</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.cryptomator</groupId>
+			<artifactId>commons-test</artifactId>
+		</dependency>
+	</dependencies>
+	
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.jacoco</groupId>
+				<artifactId>jacoco-maven-plugin</artifactId>
+			</plugin>
+		</plugins>
+	</build>
+</project>

+ 78 - 0
main/filesystem-invariants-tests/src/test/java/org/cryptomator/filesystem/invariants/FileSystemFactories.java

@@ -0,0 +1,78 @@
+package org.cryptomator.filesystem.invariants;
+
+import static java.nio.file.Files.createTempDirectory;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.cryptomator.crypto.engine.impl.CryptorImpl;
+import org.cryptomator.filesystem.FileSystem;
+import org.cryptomator.filesystem.crypto.CryptoFileSystem;
+import org.cryptomator.filesystem.inmem.InMemoryFileSystem;
+import org.cryptomator.filesystem.nio.NioFileSystem;
+
+import com.google.common.base.Supplier;
+
+class FileSystemFactories implements Iterable<Supplier<FileSystem>> {
+
+	private static final SecureRandom RANDOM_MOCK = new SecureRandom() {
+		@Override
+		public void nextBytes(byte[] bytes) {
+			Arrays.fill(bytes, (byte) 0x00);
+		}
+	};
+
+	private final List<Supplier<FileSystem>> factories = new ArrayList<>();
+
+	public FileSystemFactories() {
+		add("NioFileSystem", this::createNioFileSystem);
+		add("InMemoryFileSystem", this::createInMemoryFileSystem);
+		add("CryptoFileSystem(InMemoryFileSystem)", this::createCryptoFileSystemInMemory);
+		// FIXME fails add("CryptoFileSystem(NioFileSystem)", this::createCryptoFileSystemNio);
+	}
+
+	private FileSystem createNioFileSystem() {
+		try {
+			return NioFileSystem.rootedAt(createTempDirectory("fileSystemToTest"));
+		} catch (IOException e) {
+			throw new UncheckedIOException(e);
+		}
+	}
+
+	private FileSystem createInMemoryFileSystem() {
+		return new InMemoryFileSystem();
+	}
+
+	private FileSystem createCryptoFileSystemInMemory() {
+		return new CryptoFileSystem(createInMemoryFileSystem(), new CryptorImpl(RANDOM_MOCK), "aPassphrase");
+	}
+
+	private FileSystem createCryptoFileSystemNio() {
+		return new CryptoFileSystem(createNioFileSystem(), new CryptorImpl(RANDOM_MOCK), "aPassphrase");
+	}
+
+	private void add(String name, Supplier<FileSystem> fileSystemSupplier) {
+		factories.add(new Supplier<FileSystem>() {
+			@Override
+			public FileSystem get() {
+				return fileSystemSupplier.get();
+			}
+
+			@Override
+			public String toString() {
+				return name;
+			}
+		});
+	}
+
+	@Override
+	public Iterator<Supplier<FileSystem>> iterator() {
+		return factories.iterator();
+	}
+
+}

+ 91 - 0
main/filesystem-invariants-tests/src/test/java/org/cryptomator/filesystem/invariants/FileSystemTests.java

@@ -0,0 +1,91 @@
+package org.cryptomator.filesystem.invariants;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.Optional;
+
+import org.cryptomator.filesystem.FileSystem;
+import org.junit.Rule;
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import com.google.common.base.Supplier;
+
+@RunWith(Theories.class)
+public class FileSystemTests {
+
+	@DataPoints
+	public static final Iterable<Supplier<FileSystem>> FILE_SYSTEM_FACTORIES = new FileSystemFactories();
+
+	@Rule
+	public final ExpectedException thrown = ExpectedException.none();
+
+	@Theory
+	public void testFileSystemHasNoParent(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.parent(), is(Optional.empty()));
+	}
+
+	@Theory
+	public void testFileSystemExists(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.exists(), is(true));
+	}
+
+	@Theory
+	public void testFileSystemHasNoChildren(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.children().count(), is(0L));
+	}
+
+	@Theory
+	public void testFileSystemHasNoFiles(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.files().count(), is(0L));
+	}
+
+	@Theory
+	public void testFileSystemHasNoFolders(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.folders().count(), is(0L));
+	}
+
+	@Theory
+	public void testFileSystemsFileSystemIsItself(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.fileSystem(), is(inTest));
+	}
+
+	@Theory
+	public void testFileSystemBelongsToSameFilesystemWhenCheckingItself(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.belongsToSameFilesystem(inTest), is(true));
+	}
+
+	@Theory
+	public void testFileSystemDoesNotBelongToSameFilesystemWhenCheckingOtherInstance(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+		FileSystem otherInstance = factory.get();
+
+		assertThat(inTest.belongsToSameFilesystem(otherInstance), is(false));
+	}
+
+	@Theory
+	public void testFileSystemIsNoAncestorOfItself(Supplier<FileSystem> factory) {
+		FileSystem inTest = factory.get();
+
+		assertThat(inTest.isAncestorOf(inTest), is(false));
+	}
+
+}

+ 1 - 0
main/pom.xml

@@ -258,6 +258,7 @@
 		<module>filesystem-nio</module>
 		<module>filesystem-nameshortening</module>
 		<module>filesystem-crypto</module>
+		<module>filesystem-invariants-tests</module>
 		<module>crypto-api</module>
 		<module>crypto-aes</module>
 		<module>jackrabbit-filesystem-adapter</module>