浏览代码

added crypto file system tests

Sebastian Stenzel 9 年之前
父节点
当前提交
0dc30c27d9

+ 62 - 0
main/filesystem-crypto/src/test/java/org/cryptomator/filesystem/crypto/MasterkeysTest.java

@@ -0,0 +1,62 @@
+package org.cryptomator.filesystem.crypto;
+
+import java.nio.ByteBuffer;
+
+import org.cryptomator.crypto.engine.InvalidPassphraseException;
+import org.cryptomator.crypto.engine.impl.TestCryptorImplFactory;
+import org.cryptomator.filesystem.FileSystem;
+import org.cryptomator.filesystem.WritableFile;
+import org.cryptomator.filesystem.inmem.InMemoryFileSystem;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MasterkeysTest {
+
+	private FileSystem fs;
+	private Masterkeys m;
+
+	@Before
+	public void setup() {
+		fs = new InMemoryFileSystem();
+		m = new Masterkeys(TestCryptorImplFactory::insecureCryptorImpl);
+	}
+
+	@Test
+	public void testInitialize() {
+		m.initialize(fs, "asd");
+		Assert.assertTrue(fs.file("masterkey.cryptomator").exists());
+	}
+
+	@Test
+	public void testBackup() {
+		try (WritableFile w = fs.file("masterkey.cryptomator").openWritable()) {
+			w.write(ByteBuffer.wrap("asd".getBytes()));
+		}
+		m.backup(fs);
+		Assert.assertTrue(fs.file("masterkey.cryptomator.bkup").exists());
+	}
+
+	@Test
+	public void testRestoreBackup() {
+		try (WritableFile w = fs.file("masterkey.cryptomator.bkup").openWritable()) {
+			w.write(ByteBuffer.wrap("asd".getBytes()));
+		}
+		m.restoreBackup(fs);
+		Assert.assertTrue(fs.file("masterkey.cryptomator").exists());
+	}
+
+	@Test
+	public void testChangePassphraseWithCorrectPassword() {
+		m.initialize(fs, "foo");
+		m.changePassphrase(fs, "foo", "bar");
+		Assert.assertNotNull(m.decrypt(fs, "bar"));
+	}
+
+	@Test(expected = InvalidPassphraseException.class)
+	public void testChangePassphraseWithIncorrectPassword() {
+		m.initialize(fs, "foo");
+		m.changePassphrase(fs, "wrong", "bar");
+	}
+
+}