From cc44b623a38c9c52f04c3c7aaeb834685bf642e1 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 10 Jul 2018 16:39:53 +0200 Subject: [PATCH] Parametrize OpenPgpStoreTest --- .../store/abstr/AbstractOpenPgpKeyStore.java | 26 ++ .../ox/store/abstr/AbstractOpenPgpStore.java | 13 +- .../ox/store/definition/OpenPgpKeyStore.java | 4 + .../filebased/FileBasedOpenPgpKeyStore.java | 22 ++ .../filebased/FileBasedOpenPgpTrustStore.java | 9 + .../smackx/ox/OpenPgpStoreTest.java | 255 +++++++++++------- 6 files changed, 232 insertions(+), 97 deletions(-) diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java index 41f07a415..de977a9d1 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java @@ -147,6 +147,32 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore { return null; } + @Override + public void deletePublicKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { + PGPPublicKeyRingCollection publicKeyRings = getPublicKeysOf(owner); + if (publicKeyRings.contains(fingerprint.getKeyId())) { + publicKeyRings = PGPPublicKeyRingCollection.removePublicKeyRing(publicKeyRings, publicKeyRings.getPublicKeyRing(fingerprint.getKeyId())); + if (!publicKeyRings.iterator().hasNext()) { + publicKeyRings = null; + } + this.publicKeyRingCollections.put(owner, publicKeyRings); + writePublicKeysOf(owner, publicKeyRings); + } + } + + @Override + public void deleteSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { + PGPSecretKeyRingCollection secretKeyRings = getSecretKeysOf(owner); + if (secretKeyRings.contains(fingerprint.getKeyId())) { + secretKeyRings = PGPSecretKeyRingCollection.removeSecretKeyRing(secretKeyRings, secretKeyRings.getSecretKeyRing(fingerprint.getKeyId())); + if (!secretKeyRings.iterator().hasNext()) { + secretKeyRings = null; + } + this.secretKeyRingCollections.put(owner, secretKeyRings); + writeSecretKeysOf(owner, secretKeyRings); + } + } + @Override public PGPSecretKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java index ed7299ef6..98c1b4b0c 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java @@ -42,6 +42,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.jxmpp.jid.BareJid; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector; +import org.pgpainless.pgpainless.key.protection.UnprotectedKeysProtector; public abstract class AbstractOpenPgpStore extends Observable implements OpenPgpStore { @@ -50,9 +51,19 @@ public abstract class AbstractOpenPgpStore extends Observable implements OpenPgp protected final OpenPgpTrustStore trustStore; protected SecretKeyPassphraseCallback secretKeyPassphraseCallback; - protected SecretKeyRingProtector unlocker; + protected SecretKeyRingProtector unlocker = new UnprotectedKeysProtector(); protected final Map contacts = new HashMap<>(); + @Override + public void deletePublicKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { + keyStore.deletePublicKeyRing(owner, fingerprint); + } + + @Override + public void deleteSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { + keyStore.deleteSecretKeyRing(owner, fingerprint); + } + protected AbstractOpenPgpStore(OpenPgpKeyStore keyStore, OpenPgpMetadataStore metadataStore, OpenPgpTrustStore trustStore) { diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java index eef7b87a9..27db42bdf 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java @@ -41,6 +41,10 @@ public interface OpenPgpKeyStore { PGPSecretKeyRing getSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException; + void deletePublicKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException; + + void deleteSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException; + PGPSecretKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException; void importSecretKey(BareJid owner, PGPSecretKeyRing secretKeys) throws IOException, PGPException, MissingUserIdOnKeyException; diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java index 2df771111..66a3ef827 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpKeyStore.java @@ -49,6 +49,17 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { @Override public void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys) throws IOException { File file = getPublicKeyRingPath(owner); + + if (publicKeys == null) { + if (!file.exists()) { + return; + } + if (!file.delete()) { + throw new IOException("Could not delete file " + file.getAbsolutePath()); + } + return; + } + OutputStream outputStream = null; try { outputStream = prepareFileOutputStream(file); @@ -69,6 +80,17 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore { @Override public void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys) throws IOException { File file = getSecretKeyRingPath(owner); + + if (secretKeys == null) { + if (!file.exists()) { + return; + } + if (!file.delete()) { + throw new IOException("Could not delete file " + file.getAbsolutePath()); + } + return; + } + OutputStream outputStream = null; try { outputStream = prepareFileOutputStream(file); diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java index 92907e3a8..66cb5597f 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java @@ -92,6 +92,15 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore { protected void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException { File file = getTrustPath(owner, fingerprint); + if (trust == null || trust == Trust.undecided) { + if (!file.exists()) { + return; + } + if (!file.delete()) { + throw new IOException("Could not delete file " + file.getAbsolutePath()); + } + } + File parent = file.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { throw new IOException("Cannot create directory " + parent.getAbsolutePath()); diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java index d623bf295..5e49d3638 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java @@ -29,6 +29,8 @@ import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.Security; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -45,15 +47,21 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.After; +import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.junit.runners.Parameterized; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.JidTestUtil; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.pgpainless.util.BCUtil; +@RunWith(Parameterized.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class OpenPgpStoreTest extends SmackTestSuite { private static File storagePath; @@ -65,14 +73,30 @@ public class OpenPgpStoreTest extends SmackTestSuite { private static final OpenPgpV4Fingerprint finger2 = new OpenPgpV4Fingerprint("ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD"); private static final OpenPgpV4Fingerprint finger3 = new OpenPgpV4Fingerprint("0123012301230123012301230123012301230123"); + private final OpenPgpStore store; + private final OpenPgpStore otherStore; + static { storagePath = FileUtils.getTempDir("storeTest"); Security.addProvider(new BouncyCastleProvider()); } - @BeforeClass - @AfterClass - public static void deletePath() { + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList( + new OpenPgpStore[][] { + {new FileBasedOpenPgpStore(storagePath), new FileBasedOpenPgpStore(storagePath)} + }); + } + + public OpenPgpStoreTest(OpenPgpStore store, OpenPgpStore otherStore) { + this.store = store; + this.otherStore = otherStore; + } + + @Before + @After + public void deletePath() { FileUtils.deleteDirectory(storagePath); } @@ -81,9 +105,7 @@ public class OpenPgpStoreTest extends SmackTestSuite { */ @Test - public void store_protectorGetSet() { - OpenPgpStore store = new FileBasedOpenPgpStore(new File(storagePath, "store_protector")); - assertNull(store.getKeyRingProtector()); + public void t00_store_protectorGetSet() { store.setKeyRingProtector(new UnprotectedKeysProtector()); assertNotNull(store.getKeyRingProtector()); // TODO: Test method below @@ -100,116 +122,152 @@ public class OpenPgpStoreTest extends SmackTestSuite { */ @Test - public void key_emptyStoreTest() throws IOException, PGPException { - FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_empty")); - assertNull(keyStore.getPublicKeysOf(alice)); - assertNull(keyStore.getSecretKeysOf(alice)); - assertNull(keyStore.getPublicKeyRing(alice, finger1)); - assertNull(keyStore.getSecretKeyRing(alice, finger1)); + public void t00_deleteTest() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException { + assertNull(store.getSecretKeysOf(alice)); + assertNull(store.getPublicKeysOf(alice)); + + PGPSecretKeyRing keys = store.generateKeyRing(alice); + store.importSecretKey(alice, keys); + + assertNotNull(store.getSecretKeysOf(alice)); + assertNotNull(store.getPublicKeysOf(alice)); + + store.deleteSecretKeyRing(alice, new OpenPgpV4Fingerprint(keys)); + store.deletePublicKeyRing(alice, new OpenPgpV4Fingerprint(keys)); + + assertNull(store.getPublicKeysOf(alice)); + assertNull(store.getSecretKeysOf(alice)); } @Test - public void key_importPublicKeyFirst() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException { - // Test for nullity of all possible values. - FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_publicFirst")); + public void t01_key_emptyStoreTest() throws IOException, PGPException { + assertNull(store.getPublicKeysOf(alice)); + assertNull(store.getSecretKeysOf(alice)); + assertNull(store.getPublicKeyRing(alice, finger1)); + assertNull(store.getSecretKeyRing(alice, finger1)); + } - PGPSecretKeyRing secretKeys = keyStore.generateKeyRing(alice); + @Test + public void t02_key_importPublicKeyFirst() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException { + // Test for nullity of all possible values. + + PGPSecretKeyRing secretKeys = store.generateKeyRing(alice); PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys); assertNotNull(secretKeys); assertNotNull(publicKeys); - OpenPgpContact cAlice = keyStore.getOpenPgpContact(alice); + OpenPgpContact cAlice = store.getOpenPgpContact(alice); assertNull(cAlice.getAnyPublicKeys()); - assertEquals(new OpenPgpV4Fingerprint(publicKeys), new OpenPgpV4Fingerprint(secretKeys)); + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(publicKeys); + assertEquals(fingerprint, new OpenPgpV4Fingerprint(secretKeys)); - assertNull(keyStore.getPublicKeysOf(alice)); - assertNull(keyStore.getSecretKeysOf(alice)); + assertNull(store.getPublicKeysOf(alice)); + assertNull(store.getSecretKeysOf(alice)); - keyStore.importPublicKey(alice, publicKeys); - assertTrue(Arrays.equals(publicKeys.getEncoded(), keyStore.getPublicKeysOf(alice).getEncoded())); - assertNotNull(keyStore.getPublicKeyRing(alice, new OpenPgpV4Fingerprint(publicKeys))); - assertNull(keyStore.getSecretKeysOf(alice)); + store.importPublicKey(alice, publicKeys); + assertTrue(Arrays.equals(publicKeys.getEncoded(), store.getPublicKeysOf(alice).getEncoded())); + assertNotNull(store.getPublicKeyRing(alice, fingerprint)); + assertNull(store.getSecretKeysOf(alice)); - cAlice = keyStore.getOpenPgpContact(alice); + cAlice = store.getOpenPgpContact(alice); assertNotNull(cAlice.getAnyPublicKeys()); // Import keys a second time -> No change expected. - keyStore.importPublicKey(alice, publicKeys); - assertTrue(Arrays.equals(publicKeys.getEncoded(), keyStore.getPublicKeysOf(alice).getEncoded())); - keyStore.importSecretKey(alice, secretKeys); - assertTrue(Arrays.equals(secretKeys.getEncoded(), keyStore.getSecretKeysOf(alice).getEncoded())); + store.importPublicKey(alice, publicKeys); + assertTrue(Arrays.equals(publicKeys.getEncoded(), store.getPublicKeysOf(alice).getEncoded())); + store.importSecretKey(alice, secretKeys); + assertTrue(Arrays.equals(secretKeys.getEncoded(), store.getSecretKeysOf(alice).getEncoded())); - keyStore.importSecretKey(alice, secretKeys); - assertNotNull(keyStore.getSecretKeysOf(alice)); - assertTrue(Arrays.equals(secretKeys.getEncoded(), keyStore.getSecretKeysOf(alice).getEncoded())); - assertNotNull(keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(secretKeys))); + store.importSecretKey(alice, secretKeys); + assertNotNull(store.getSecretKeysOf(alice)); + assertTrue(Arrays.equals(secretKeys.getEncoded(), store.getSecretKeysOf(alice).getEncoded())); + assertNotNull(store.getSecretKeyRing(alice, fingerprint)); - assertTrue(Arrays.equals(secretKeys.getEncoded(), keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(secretKeys)).getEncoded())); + assertTrue(Arrays.equals(secretKeys.getEncoded(), store.getSecretKeyRing(alice, fingerprint).getEncoded())); assertTrue(Arrays.equals(publicKeys.getEncoded(), - BCUtil.publicKeyRingFromSecretKeyRing(keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(secretKeys))).getEncoded())); + BCUtil.publicKeyRingFromSecretKeyRing(store.getSecretKeyRing(alice, fingerprint)).getEncoded())); + + // Clean up + store.deletePublicKeyRing(alice, fingerprint); + store.deleteSecretKeyRing(alice, fingerprint); } @Test - public void key_importSecretKeyFirst() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_secretFirst")); - PGPSecretKeyRing secretKeys = keyStore.generateKeyRing(alice); + public void t03_key_importSecretKeyFirst() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { + PGPSecretKeyRing secretKeys = store.generateKeyRing(alice); + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys); - assertNull(keyStore.getSecretKeysOf(alice)); - assertNull(keyStore.getPublicKeysOf(alice)); - keyStore.importSecretKey(alice, secretKeys); + assertNull(store.getSecretKeysOf(alice)); + assertNull(store.getPublicKeysOf(alice)); + store.importSecretKey(alice, secretKeys); - assertNotNull(keyStore.getSecretKeysOf(alice)); - assertNotNull(keyStore.getPublicKeysOf(alice)); + assertNotNull(store.getSecretKeysOf(alice)); + assertNotNull(store.getPublicKeysOf(alice)); + + // Clean up + store.deleteSecretKeyRing(alice, fingerprint); + store.deletePublicKeyRing(alice, fingerprint); } @Test(expected = MissingUserIdOnKeyException.class) - public void key_wrongBareJidOnSecretKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_wrongSecBareJid")); - PGPSecretKeyRing secretKeys = keyStore.generateKeyRing(alice); + public void t04_key_wrongBareJidOnSecretKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { + PGPSecretKeyRing secretKeys = store.generateKeyRing(alice); - keyStore.importSecretKey(bob, secretKeys); + store.importSecretKey(bob, secretKeys); } @Test(expected = MissingUserIdOnKeyException.class) - public void key_wrongBareJidOnPublicKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_wrongPubBareJid")); - PGPSecretKeyRing secretKeys = keyStore.generateKeyRing(alice); + public void t05_key_wrongBareJidOnPublicKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { + PGPSecretKeyRing secretKeys = store.generateKeyRing(alice); PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys); - keyStore.importPublicKey(bob, publicKeys); + store.importPublicKey(bob, publicKeys); } @Test - public void key_keyReloadTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - FileBasedOpenPgpStore one = new FileBasedOpenPgpStore(new File(storagePath, "keys_reload")); - PGPSecretKeyRing secretKeys = one.generateKeyRing(alice); + public void t06_key_keyReloadTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { + PGPSecretKeyRing secretKeys = store.generateKeyRing(alice); + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys); PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys); - one.importSecretKey(alice, secretKeys); - one.importPublicKey(alice, publicKeys); + store.importSecretKey(alice, secretKeys); + store.importPublicKey(alice, publicKeys); - FileBasedOpenPgpStore two = new FileBasedOpenPgpStore(new File(storagePath, "keys_reload")); - assertNotNull(two.getSecretKeysOf(alice)); - assertNotNull(two.getPublicKeysOf(alice)); + assertNotNull(otherStore.getSecretKeysOf(alice)); + assertNotNull(otherStore.getPublicKeysOf(alice)); + + // Clean up + store.deletePublicKeyRing(alice, fingerprint); + store.deleteSecretKeyRing(alice, fingerprint); + otherStore.deletePublicKeyRing(alice, fingerprint); + otherStore.deleteSecretKeyRing(alice, fingerprint); } @Test - public void multipleKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - OpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_multi")); - PGPSecretKeyRing one = keyStore.generateKeyRing(alice); - PGPSecretKeyRing two = keyStore.generateKeyRing(alice); + public void t07_multipleKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { + PGPSecretKeyRing one = store.generateKeyRing(alice); + PGPSecretKeyRing two = store.generateKeyRing(alice); - keyStore.importSecretKey(alice, one); - keyStore.importSecretKey(alice, two); + OpenPgpV4Fingerprint fingerprint1 = new OpenPgpV4Fingerprint(one); + OpenPgpV4Fingerprint fingerprint2 = new OpenPgpV4Fingerprint(two); - assertTrue(Arrays.equals(one.getEncoded(), keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(one)).getEncoded())); - assertTrue(Arrays.equals(two.getEncoded(), keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(two)).getEncoded())); + store.importSecretKey(alice, one); + store.importSecretKey(alice, two); - assertTrue(Arrays.equals(one.getEncoded(), keyStore.getSecretKeysOf(alice).getSecretKeyRing(one.getPublicKey().getKeyID()).getEncoded())); + assertTrue(Arrays.equals(one.getEncoded(), store.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(one)).getEncoded())); + assertTrue(Arrays.equals(two.getEncoded(), store.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(two)).getEncoded())); + + assertTrue(Arrays.equals(one.getEncoded(), store.getSecretKeysOf(alice).getSecretKeyRing(one.getPublicKey().getKeyID()).getEncoded())); assertTrue(Arrays.equals(BCUtil.publicKeyRingFromSecretKeyRing(one).getEncoded(), - keyStore.getPublicKeyRing(alice, new OpenPgpV4Fingerprint(one)).getEncoded())); + store.getPublicKeyRing(alice, new OpenPgpV4Fingerprint(one)).getEncoded())); + + // Cleanup + store.deletePublicKeyRing(alice, fingerprint1); + store.deletePublicKeyRing(alice, fingerprint2); + store.deleteSecretKeyRing(alice, fingerprint1); + store.deleteSecretKeyRing(alice, fingerprint2); } /* @@ -217,30 +275,34 @@ public class OpenPgpStoreTest extends SmackTestSuite { */ @Test - public void trust_emptyStoreTest() throws IOException { + public void t08_trust_emptyStoreTest() throws IOException { - FileBasedOpenPgpStore trustStore = new FileBasedOpenPgpStore(new File(storagePath, "trust_empty")); - assertEquals(OpenPgpTrustStore.Trust.undecided, trustStore.getTrust(alice, finger2)); - trustStore.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted); - assertEquals(OpenPgpTrustStore.Trust.trusted, trustStore.getTrust(alice, finger2)); + assertEquals(OpenPgpTrustStore.Trust.undecided, store.getTrust(alice, finger2)); + store.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted); + assertEquals(OpenPgpTrustStore.Trust.trusted, store.getTrust(alice, finger2)); // Set trust a second time -> no change - trustStore.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted); - assertEquals(OpenPgpTrustStore.Trust.trusted, trustStore.getTrust(alice, finger2)); + store.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted); + assertEquals(OpenPgpTrustStore.Trust.trusted, store.getTrust(alice, finger2)); - assertEquals(OpenPgpTrustStore.Trust.undecided, trustStore.getTrust(alice, finger3)); + assertEquals(OpenPgpTrustStore.Trust.undecided, store.getTrust(alice, finger3)); - trustStore.setTrust(bob, finger2, OpenPgpTrustStore.Trust.untrusted); - assertEquals(OpenPgpTrustStore.Trust.untrusted, trustStore.getTrust(bob, finger2)); - assertEquals(OpenPgpTrustStore.Trust.trusted, trustStore.getTrust(alice, finger2)); + store.setTrust(bob, finger2, OpenPgpTrustStore.Trust.untrusted); + assertEquals(OpenPgpTrustStore.Trust.untrusted, store.getTrust(bob, finger2)); + assertEquals(OpenPgpTrustStore.Trust.trusted, store.getTrust(alice, finger2)); + + // clean up + store.setTrust(alice, finger2, OpenPgpTrustStore.Trust.undecided); + store.setTrust(bob, finger2, OpenPgpTrustStore.Trust.undecided); } @Test - public void trust_reloadTest() throws IOException { - OpenPgpStore trustStore = new FileBasedOpenPgpStore(new File(storagePath, "trust_reload")); - trustStore.setTrust(alice, finger1, OpenPgpTrustStore.Trust.trusted); + public void t09_trust_reloadTest() throws IOException { + store.setTrust(alice, finger1, OpenPgpTrustStore.Trust.trusted); + assertEquals(OpenPgpTrustStore.Trust.trusted, otherStore.getTrust(alice, finger1)); - OpenPgpStore secondStore = new FileBasedOpenPgpStore(new File(storagePath, "trust_reload")); - assertEquals(OpenPgpTrustStore.Trust.trusted, secondStore.getTrust(alice, finger1)); + // cleanup + store.setTrust(alice, finger1, OpenPgpTrustStore.Trust.undecided); + otherStore.setTrust(alice, finger1, OpenPgpTrustStore.Trust.undecided); } /* @@ -248,10 +310,9 @@ public class OpenPgpStoreTest extends SmackTestSuite { */ @Test - public void meta_emptyStoreTest() throws IOException { - OpenPgpStore metaStore = new FileBasedOpenPgpStore(new File(storagePath, "meta_empty")); - assertNotNull(metaStore.getAnnouncedFingerprintsOf(alice)); - assertTrue(metaStore.getAnnouncedFingerprintsOf(alice).isEmpty()); + public void t10_meta_emptyStoreTest() throws IOException { + assertNotNull(store.getAnnouncedFingerprintsOf(alice)); + assertTrue(store.getAnnouncedFingerprintsOf(alice).isEmpty()); Map map = new HashMap<>(); Date date1 = new Date(12354563423L); @@ -259,14 +320,16 @@ public class OpenPgpStoreTest extends SmackTestSuite { map.put(finger1, date1); map.put(finger2, date2); - metaStore.setAnnouncedFingerprintsOf(alice, map); - assertFalse(metaStore.getAnnouncedFingerprintsOf(alice).isEmpty()); - assertEquals(map, metaStore.getAnnouncedFingerprintsOf(alice)); + store.setAnnouncedFingerprintsOf(alice, map); + assertFalse(store.getAnnouncedFingerprintsOf(alice).isEmpty()); + assertEquals(map, store.getAnnouncedFingerprintsOf(alice)); - assertTrue(metaStore.getAnnouncedFingerprintsOf(bob).isEmpty()); + assertTrue(store.getAnnouncedFingerprintsOf(bob).isEmpty()); - OpenPgpStore otherStore = new FileBasedOpenPgpStore(new File(storagePath, "meta_empty")); assertFalse(otherStore.getAnnouncedFingerprintsOf(alice).isEmpty()); assertEquals(map, otherStore.getAnnouncedFingerprintsOf(alice)); + + store.setAnnouncedFingerprintsOf(alice, Collections.emptyMap()); + otherStore.setAnnouncedFingerprintsOf(alice, Collections.emptyMap()); } }