mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-02-18 14:49:23 +01:00
Parametrize OpenPgpStoreTest
This commit is contained in:
parent
eac247333e
commit
cc44b623a3
6 changed files with 232 additions and 97 deletions
|
@ -147,6 +147,32 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
|
||||||
return null;
|
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
|
@Override
|
||||||
public PGPSecretKeyRing generateKeyRing(BareJid owner)
|
public PGPSecretKeyRing generateKeyRing(BareJid owner)
|
||||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
||||||
|
|
|
@ -42,6 +42,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||||
import org.jxmpp.jid.BareJid;
|
import org.jxmpp.jid.BareJid;
|
||||||
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
|
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
|
||||||
import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector;
|
import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector;
|
||||||
|
import org.pgpainless.pgpainless.key.protection.UnprotectedKeysProtector;
|
||||||
|
|
||||||
public abstract class AbstractOpenPgpStore extends Observable implements OpenPgpStore {
|
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 final OpenPgpTrustStore trustStore;
|
||||||
|
|
||||||
protected SecretKeyPassphraseCallback secretKeyPassphraseCallback;
|
protected SecretKeyPassphraseCallback secretKeyPassphraseCallback;
|
||||||
protected SecretKeyRingProtector unlocker;
|
protected SecretKeyRingProtector unlocker = new UnprotectedKeysProtector();
|
||||||
protected final Map<BareJid, OpenPgpContact> contacts = new HashMap<>();
|
protected final Map<BareJid, OpenPgpContact> 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,
|
protected AbstractOpenPgpStore(OpenPgpKeyStore keyStore,
|
||||||
OpenPgpMetadataStore metadataStore,
|
OpenPgpMetadataStore metadataStore,
|
||||||
OpenPgpTrustStore trustStore) {
|
OpenPgpTrustStore trustStore) {
|
||||||
|
|
|
@ -41,6 +41,10 @@ public interface OpenPgpKeyStore {
|
||||||
|
|
||||||
PGPSecretKeyRing getSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException;
|
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;
|
PGPSecretKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException;
|
||||||
|
|
||||||
void importSecretKey(BareJid owner, PGPSecretKeyRing secretKeys) throws IOException, PGPException, MissingUserIdOnKeyException;
|
void importSecretKey(BareJid owner, PGPSecretKeyRing secretKeys) throws IOException, PGPException, MissingUserIdOnKeyException;
|
||||||
|
|
|
@ -49,6 +49,17 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
|
||||||
@Override
|
@Override
|
||||||
public void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys) throws IOException {
|
public void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys) throws IOException {
|
||||||
File file = getPublicKeyRingPath(owner);
|
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;
|
OutputStream outputStream = null;
|
||||||
try {
|
try {
|
||||||
outputStream = prepareFileOutputStream(file);
|
outputStream = prepareFileOutputStream(file);
|
||||||
|
@ -69,6 +80,17 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
|
||||||
@Override
|
@Override
|
||||||
public void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys) throws IOException {
|
public void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys) throws IOException {
|
||||||
File file = getSecretKeyRingPath(owner);
|
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;
|
OutputStream outputStream = null;
|
||||||
try {
|
try {
|
||||||
outputStream = prepareFileOutputStream(file);
|
outputStream = prepareFileOutputStream(file);
|
||||||
|
|
|
@ -92,6 +92,15 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore {
|
||||||
protected void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException {
|
protected void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException {
|
||||||
File file = getTrustPath(owner, fingerprint);
|
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();
|
File parent = file.getParentFile();
|
||||||
if (!parent.exists() && !parent.mkdirs()) {
|
if (!parent.exists() && !parent.mkdirs()) {
|
||||||
throw new IOException("Cannot create directory " + parent.getAbsolutePath());
|
throw new IOException("Cannot create directory " + parent.getAbsolutePath());
|
||||||
|
|
|
@ -29,6 +29,8 @@ import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.NoSuchProviderException;
|
import java.security.NoSuchProviderException;
|
||||||
import java.security.Security;
|
import java.security.Security;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -45,15 +47,21 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||||
import org.junit.AfterClass;
|
import org.junit.After;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.Before;
|
||||||
|
import org.junit.FixMethodOrder;
|
||||||
import org.junit.Test;
|
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.BareJid;
|
||||||
import org.jxmpp.jid.JidTestUtil;
|
import org.jxmpp.jid.JidTestUtil;
|
||||||
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
|
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
|
||||||
import org.pgpainless.pgpainless.key.protection.UnprotectedKeysProtector;
|
import org.pgpainless.pgpainless.key.protection.UnprotectedKeysProtector;
|
||||||
import org.pgpainless.pgpainless.util.BCUtil;
|
import org.pgpainless.pgpainless.util.BCUtil;
|
||||||
|
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
public class OpenPgpStoreTest extends SmackTestSuite {
|
public class OpenPgpStoreTest extends SmackTestSuite {
|
||||||
|
|
||||||
private static File storagePath;
|
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 finger2 = new OpenPgpV4Fingerprint("ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD");
|
||||||
private static final OpenPgpV4Fingerprint finger3 = new OpenPgpV4Fingerprint("0123012301230123012301230123012301230123");
|
private static final OpenPgpV4Fingerprint finger3 = new OpenPgpV4Fingerprint("0123012301230123012301230123012301230123");
|
||||||
|
|
||||||
|
private final OpenPgpStore store;
|
||||||
|
private final OpenPgpStore otherStore;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
storagePath = FileUtils.getTempDir("storeTest");
|
storagePath = FileUtils.getTempDir("storeTest");
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeClass
|
@Parameterized.Parameters
|
||||||
@AfterClass
|
public static Collection<OpenPgpStore[]> data() {
|
||||||
public static void deletePath() {
|
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);
|
FileUtils.deleteDirectory(storagePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,9 +105,7 @@ public class OpenPgpStoreTest extends SmackTestSuite {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void store_protectorGetSet() {
|
public void t00_store_protectorGetSet() {
|
||||||
OpenPgpStore store = new FileBasedOpenPgpStore(new File(storagePath, "store_protector"));
|
|
||||||
assertNull(store.getKeyRingProtector());
|
|
||||||
store.setKeyRingProtector(new UnprotectedKeysProtector());
|
store.setKeyRingProtector(new UnprotectedKeysProtector());
|
||||||
assertNotNull(store.getKeyRingProtector());
|
assertNotNull(store.getKeyRingProtector());
|
||||||
// TODO: Test method below
|
// TODO: Test method below
|
||||||
|
@ -100,116 +122,152 @@ public class OpenPgpStoreTest extends SmackTestSuite {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void key_emptyStoreTest() throws IOException, PGPException {
|
public void t00_deleteTest() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException {
|
||||||
FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_empty"));
|
assertNull(store.getSecretKeysOf(alice));
|
||||||
assertNull(keyStore.getPublicKeysOf(alice));
|
assertNull(store.getPublicKeysOf(alice));
|
||||||
assertNull(keyStore.getSecretKeysOf(alice));
|
|
||||||
assertNull(keyStore.getPublicKeyRing(alice, finger1));
|
PGPSecretKeyRing keys = store.generateKeyRing(alice);
|
||||||
assertNull(keyStore.getSecretKeyRing(alice, finger1));
|
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
|
@Test
|
||||||
public void key_importPublicKeyFirst() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException {
|
public void t01_key_emptyStoreTest() throws IOException, PGPException {
|
||||||
// Test for nullity of all possible values.
|
assertNull(store.getPublicKeysOf(alice));
|
||||||
FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_publicFirst"));
|
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);
|
PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys);
|
||||||
assertNotNull(secretKeys);
|
assertNotNull(secretKeys);
|
||||||
assertNotNull(publicKeys);
|
assertNotNull(publicKeys);
|
||||||
|
|
||||||
OpenPgpContact cAlice = keyStore.getOpenPgpContact(alice);
|
OpenPgpContact cAlice = store.getOpenPgpContact(alice);
|
||||||
assertNull(cAlice.getAnyPublicKeys());
|
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(store.getPublicKeysOf(alice));
|
||||||
assertNull(keyStore.getSecretKeysOf(alice));
|
assertNull(store.getSecretKeysOf(alice));
|
||||||
|
|
||||||
keyStore.importPublicKey(alice, publicKeys);
|
store.importPublicKey(alice, publicKeys);
|
||||||
assertTrue(Arrays.equals(publicKeys.getEncoded(), keyStore.getPublicKeysOf(alice).getEncoded()));
|
assertTrue(Arrays.equals(publicKeys.getEncoded(), store.getPublicKeysOf(alice).getEncoded()));
|
||||||
assertNotNull(keyStore.getPublicKeyRing(alice, new OpenPgpV4Fingerprint(publicKeys)));
|
assertNotNull(store.getPublicKeyRing(alice, fingerprint));
|
||||||
assertNull(keyStore.getSecretKeysOf(alice));
|
assertNull(store.getSecretKeysOf(alice));
|
||||||
|
|
||||||
cAlice = keyStore.getOpenPgpContact(alice);
|
cAlice = store.getOpenPgpContact(alice);
|
||||||
assertNotNull(cAlice.getAnyPublicKeys());
|
assertNotNull(cAlice.getAnyPublicKeys());
|
||||||
|
|
||||||
// Import keys a second time -> No change expected.
|
// Import keys a second time -> No change expected.
|
||||||
keyStore.importPublicKey(alice, publicKeys);
|
store.importPublicKey(alice, publicKeys);
|
||||||
assertTrue(Arrays.equals(publicKeys.getEncoded(), keyStore.getPublicKeysOf(alice).getEncoded()));
|
assertTrue(Arrays.equals(publicKeys.getEncoded(), store.getPublicKeysOf(alice).getEncoded()));
|
||||||
keyStore.importSecretKey(alice, secretKeys);
|
store.importSecretKey(alice, secretKeys);
|
||||||
assertTrue(Arrays.equals(secretKeys.getEncoded(), keyStore.getSecretKeysOf(alice).getEncoded()));
|
assertTrue(Arrays.equals(secretKeys.getEncoded(), store.getSecretKeysOf(alice).getEncoded()));
|
||||||
|
|
||||||
keyStore.importSecretKey(alice, secretKeys);
|
store.importSecretKey(alice, secretKeys);
|
||||||
assertNotNull(keyStore.getSecretKeysOf(alice));
|
assertNotNull(store.getSecretKeysOf(alice));
|
||||||
assertTrue(Arrays.equals(secretKeys.getEncoded(), keyStore.getSecretKeysOf(alice).getEncoded()));
|
assertTrue(Arrays.equals(secretKeys.getEncoded(), store.getSecretKeysOf(alice).getEncoded()));
|
||||||
assertNotNull(keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(secretKeys)));
|
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(),
|
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
|
@Test
|
||||||
public void key_importSecretKeyFirst() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
public void t03_key_importSecretKeyFirst() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
||||||
FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_secretFirst"));
|
PGPSecretKeyRing secretKeys = store.generateKeyRing(alice);
|
||||||
PGPSecretKeyRing secretKeys = keyStore.generateKeyRing(alice);
|
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys);
|
||||||
|
|
||||||
assertNull(keyStore.getSecretKeysOf(alice));
|
assertNull(store.getSecretKeysOf(alice));
|
||||||
assertNull(keyStore.getPublicKeysOf(alice));
|
assertNull(store.getPublicKeysOf(alice));
|
||||||
keyStore.importSecretKey(alice, secretKeys);
|
store.importSecretKey(alice, secretKeys);
|
||||||
|
|
||||||
assertNotNull(keyStore.getSecretKeysOf(alice));
|
assertNotNull(store.getSecretKeysOf(alice));
|
||||||
assertNotNull(keyStore.getPublicKeysOf(alice));
|
assertNotNull(store.getPublicKeysOf(alice));
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
store.deleteSecretKeyRing(alice, fingerprint);
|
||||||
|
store.deletePublicKeyRing(alice, fingerprint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = MissingUserIdOnKeyException.class)
|
@Test(expected = MissingUserIdOnKeyException.class)
|
||||||
public void key_wrongBareJidOnSecretKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
public void t04_key_wrongBareJidOnSecretKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
||||||
FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_wrongSecBareJid"));
|
PGPSecretKeyRing secretKeys = store.generateKeyRing(alice);
|
||||||
PGPSecretKeyRing secretKeys = keyStore.generateKeyRing(alice);
|
|
||||||
|
|
||||||
keyStore.importSecretKey(bob, secretKeys);
|
store.importSecretKey(bob, secretKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = MissingUserIdOnKeyException.class)
|
@Test(expected = MissingUserIdOnKeyException.class)
|
||||||
public void key_wrongBareJidOnPublicKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
public void t05_key_wrongBareJidOnPublicKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
||||||
FileBasedOpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_wrongPubBareJid"));
|
PGPSecretKeyRing secretKeys = store.generateKeyRing(alice);
|
||||||
PGPSecretKeyRing secretKeys = keyStore.generateKeyRing(alice);
|
|
||||||
PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys);
|
PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys);
|
||||||
|
|
||||||
keyStore.importPublicKey(bob, publicKeys);
|
store.importPublicKey(bob, publicKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void key_keyReloadTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
public void t06_key_keyReloadTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
||||||
FileBasedOpenPgpStore one = new FileBasedOpenPgpStore(new File(storagePath, "keys_reload"));
|
PGPSecretKeyRing secretKeys = store.generateKeyRing(alice);
|
||||||
PGPSecretKeyRing secretKeys = one.generateKeyRing(alice);
|
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys);
|
||||||
PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys);
|
PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys);
|
||||||
|
|
||||||
one.importSecretKey(alice, secretKeys);
|
store.importSecretKey(alice, secretKeys);
|
||||||
one.importPublicKey(alice, publicKeys);
|
store.importPublicKey(alice, publicKeys);
|
||||||
|
|
||||||
FileBasedOpenPgpStore two = new FileBasedOpenPgpStore(new File(storagePath, "keys_reload"));
|
assertNotNull(otherStore.getSecretKeysOf(alice));
|
||||||
assertNotNull(two.getSecretKeysOf(alice));
|
assertNotNull(otherStore.getPublicKeysOf(alice));
|
||||||
assertNotNull(two.getPublicKeysOf(alice));
|
|
||||||
|
// Clean up
|
||||||
|
store.deletePublicKeyRing(alice, fingerprint);
|
||||||
|
store.deleteSecretKeyRing(alice, fingerprint);
|
||||||
|
otherStore.deletePublicKeyRing(alice, fingerprint);
|
||||||
|
otherStore.deleteSecretKeyRing(alice, fingerprint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multipleKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
public void t07_multipleKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException {
|
||||||
OpenPgpStore keyStore = new FileBasedOpenPgpStore(new File(storagePath, "keys_multi"));
|
PGPSecretKeyRing one = store.generateKeyRing(alice);
|
||||||
PGPSecretKeyRing one = keyStore.generateKeyRing(alice);
|
PGPSecretKeyRing two = store.generateKeyRing(alice);
|
||||||
PGPSecretKeyRing two = keyStore.generateKeyRing(alice);
|
|
||||||
|
|
||||||
keyStore.importSecretKey(alice, one);
|
OpenPgpV4Fingerprint fingerprint1 = new OpenPgpV4Fingerprint(one);
|
||||||
keyStore.importSecretKey(alice, two);
|
OpenPgpV4Fingerprint fingerprint2 = new OpenPgpV4Fingerprint(two);
|
||||||
|
|
||||||
assertTrue(Arrays.equals(one.getEncoded(), keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(one)).getEncoded()));
|
store.importSecretKey(alice, one);
|
||||||
assertTrue(Arrays.equals(two.getEncoded(), keyStore.getSecretKeyRing(alice, new OpenPgpV4Fingerprint(two)).getEncoded()));
|
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(),
|
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
|
@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, store.getTrust(alice, finger2));
|
||||||
assertEquals(OpenPgpTrustStore.Trust.undecided, trustStore.getTrust(alice, finger2));
|
store.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted);
|
||||||
trustStore.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted);
|
assertEquals(OpenPgpTrustStore.Trust.trusted, store.getTrust(alice, finger2));
|
||||||
assertEquals(OpenPgpTrustStore.Trust.trusted, trustStore.getTrust(alice, finger2));
|
|
||||||
// Set trust a second time -> no change
|
// Set trust a second time -> no change
|
||||||
trustStore.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted);
|
store.setTrust(alice, finger2, OpenPgpTrustStore.Trust.trusted);
|
||||||
assertEquals(OpenPgpTrustStore.Trust.trusted, trustStore.getTrust(alice, finger2));
|
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);
|
store.setTrust(bob, finger2, OpenPgpTrustStore.Trust.untrusted);
|
||||||
assertEquals(OpenPgpTrustStore.Trust.untrusted, trustStore.getTrust(bob, finger2));
|
assertEquals(OpenPgpTrustStore.Trust.untrusted, store.getTrust(bob, finger2));
|
||||||
assertEquals(OpenPgpTrustStore.Trust.trusted, trustStore.getTrust(alice, 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
|
@Test
|
||||||
public void trust_reloadTest() throws IOException {
|
public void t09_trust_reloadTest() throws IOException {
|
||||||
OpenPgpStore trustStore = new FileBasedOpenPgpStore(new File(storagePath, "trust_reload"));
|
store.setTrust(alice, finger1, OpenPgpTrustStore.Trust.trusted);
|
||||||
trustStore.setTrust(alice, finger1, OpenPgpTrustStore.Trust.trusted);
|
assertEquals(OpenPgpTrustStore.Trust.trusted, otherStore.getTrust(alice, finger1));
|
||||||
|
|
||||||
OpenPgpStore secondStore = new FileBasedOpenPgpStore(new File(storagePath, "trust_reload"));
|
// cleanup
|
||||||
assertEquals(OpenPgpTrustStore.Trust.trusted, secondStore.getTrust(alice, finger1));
|
store.setTrust(alice, finger1, OpenPgpTrustStore.Trust.undecided);
|
||||||
|
otherStore.setTrust(alice, finger1, OpenPgpTrustStore.Trust.undecided);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -248,10 +310,9 @@ public class OpenPgpStoreTest extends SmackTestSuite {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void meta_emptyStoreTest() throws IOException {
|
public void t10_meta_emptyStoreTest() throws IOException {
|
||||||
OpenPgpStore metaStore = new FileBasedOpenPgpStore(new File(storagePath, "meta_empty"));
|
assertNotNull(store.getAnnouncedFingerprintsOf(alice));
|
||||||
assertNotNull(metaStore.getAnnouncedFingerprintsOf(alice));
|
assertTrue(store.getAnnouncedFingerprintsOf(alice).isEmpty());
|
||||||
assertTrue(metaStore.getAnnouncedFingerprintsOf(alice).isEmpty());
|
|
||||||
|
|
||||||
Map<OpenPgpV4Fingerprint, Date> map = new HashMap<>();
|
Map<OpenPgpV4Fingerprint, Date> map = new HashMap<>();
|
||||||
Date date1 = new Date(12354563423L);
|
Date date1 = new Date(12354563423L);
|
||||||
|
@ -259,14 +320,16 @@ public class OpenPgpStoreTest extends SmackTestSuite {
|
||||||
map.put(finger1, date1);
|
map.put(finger1, date1);
|
||||||
map.put(finger2, date2);
|
map.put(finger2, date2);
|
||||||
|
|
||||||
metaStore.setAnnouncedFingerprintsOf(alice, map);
|
store.setAnnouncedFingerprintsOf(alice, map);
|
||||||
assertFalse(metaStore.getAnnouncedFingerprintsOf(alice).isEmpty());
|
assertFalse(store.getAnnouncedFingerprintsOf(alice).isEmpty());
|
||||||
assertEquals(map, metaStore.getAnnouncedFingerprintsOf(alice));
|
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());
|
assertFalse(otherStore.getAnnouncedFingerprintsOf(alice).isEmpty());
|
||||||
assertEquals(map, otherStore.getAnnouncedFingerprintsOf(alice));
|
assertEquals(map, otherStore.getAnnouncedFingerprintsOf(alice));
|
||||||
|
|
||||||
|
store.setAnnouncedFingerprintsOf(alice, Collections.<OpenPgpV4Fingerprint, Date>emptyMap());
|
||||||
|
otherStore.setAnnouncedFingerprintsOf(alice, Collections.<OpenPgpV4Fingerprint, Date>emptyMap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue