mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 20:42:06 +01:00
Add test
This commit is contained in:
parent
63e98cb4d6
commit
e23cf88082
4 changed files with 84 additions and 7 deletions
|
@ -239,12 +239,11 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
|||
}
|
||||
}
|
||||
|
||||
return decryptImpl(bytes, secretKeyRings, protector, trustedKeyIds, trustedKeys);
|
||||
return decryptImpl(bytes, secretKeyRings, protector, trustedKeys);
|
||||
}
|
||||
|
||||
DecryptedBytesAndMetadata decryptImpl(byte[] bytes, PGPSecretKeyRingCollection decryptionKeys,
|
||||
SecretKeyRingProtector protector,
|
||||
Set<Long> trustedKeyIds,
|
||||
Set<PGPPublicKeyRing> verificationKeys)
|
||||
throws SmackOpenPgpException, IOException {
|
||||
|
||||
|
@ -255,7 +254,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
|||
fromEncrypted = PGPainless.createDecryptor()
|
||||
.onInputStream(encryptedBytes)
|
||||
.decryptWith(decryptionKeys, protector)
|
||||
.verifyWith(trustedKeyIds, verificationKeys)
|
||||
.verifyWith(verificationKeys)
|
||||
.ignoreMissingPublicKeys()
|
||||
.build();
|
||||
} catch (IOException | PGPException e) {
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package org.jivesoftware.smackx.ox.bouncycastle;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.Security;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
|
||||
import de.vanitasvitae.crypto.pgpainless.PGPainless;
|
||||
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
|
||||
import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.jid.BareJid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public class FileBasedPainlessOpenPgpStoreTest extends SmackTestSuite {
|
||||
|
||||
private static final File basePath;
|
||||
private static final BareJid alice;
|
||||
private static final BareJid bob;
|
||||
|
||||
static {
|
||||
String userHome = System.getProperty("user.home");
|
||||
if (userHome != null) {
|
||||
File f = new File(userHome);
|
||||
basePath = new File(f, ".config/smack-integration-test/ox/painless_ox_store");
|
||||
} else {
|
||||
basePath = new File("painless_ox_store");
|
||||
}
|
||||
|
||||
try {
|
||||
alice = JidCreate.bareFrom("alice@wonderland.lit");
|
||||
bob = JidCreate.bareFrom("bob@builder.tv");
|
||||
} catch (XmppStringprepException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeSecretKeyRingsTest()
|
||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
FileBasedPainlessOpenPgpStore store = new FileBasedPainlessOpenPgpStore(basePath, new UnprotectedKeysProtector());
|
||||
|
||||
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing().simpleRsaKeyRing("xmpp:" + alice.toString(), RsaLength._3072);
|
||||
PGPSecretKeyRingCollection saving = new PGPSecretKeyRingCollection(Collections.singleton(secretKey));
|
||||
store.storeSecretKeyRing(alice, saving);
|
||||
|
||||
PGPSecretKeyRingCollection restored = store.getSecretKeyRings(alice);
|
||||
|
||||
assertTrue(Arrays.equals(saving.getEncoded(), restored.getEncoded()));
|
||||
}
|
||||
}
|
|
@ -78,10 +78,12 @@ public final class OXInstantMessagingManager extends Manager {
|
|||
|
||||
public static OXInstantMessagingManager getInstanceFor(XMPPConnection connection) {
|
||||
OXInstantMessagingManager manager = INSTANCES.get(connection);
|
||||
|
||||
if (manager == null) {
|
||||
manager = new OXInstantMessagingManager(connection);
|
||||
INSTANCES.put(connection, manager);
|
||||
}
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
|
@ -123,10 +125,17 @@ public final class OXInstantMessagingManager extends Manager {
|
|||
throws SmackOpenPgpException, InterruptedException, XMPPException.XMPPErrorException,
|
||||
SmackException.NotConnectedException, SmackException.NoResponseException {
|
||||
|
||||
OpenPgpFingerprints theirKeys = openPgpManager.determineContactsKeys(jid);
|
||||
OpenPgpFingerprints ourKeys = openPgpManager.determineContactsKeys(connection().getUser().asBareJid());
|
||||
Chat chat = chatManager.chatWith(jid);
|
||||
return new OpenPgpEncryptedChat(openPgpManager.getOpenPgpProvider(), chat, ourKeys, theirKeys);
|
||||
OpenPgpEncryptedChat encryptedChat = chats.get(jid);
|
||||
|
||||
if (encryptedChat == null) {
|
||||
OpenPgpFingerprints theirKeys = openPgpManager.determineContactsKeys(jid);
|
||||
OpenPgpFingerprints ourKeys = openPgpManager.determineContactsKeys(connection().getUser().asBareJid());
|
||||
Chat chat = chatManager.chatWith(jid);
|
||||
encryptedChat = new OpenPgpEncryptedChat(openPgpManager.getOpenPgpProvider(), chat, ourKeys, theirKeys);
|
||||
chats.put(jid, encryptedChat);
|
||||
}
|
||||
|
||||
return encryptedChat;
|
||||
}
|
||||
|
||||
public boolean addOpenPgpEncryptedMessageListener(OpenPgpEncryptedMessageListener listener) {
|
||||
|
|
|
@ -53,6 +53,8 @@ public interface OpenPgpStore {
|
|||
*
|
||||
* @param owner owner.
|
||||
* @return set of fingerprints of available OpenPGP key pairs master keys.
|
||||
*
|
||||
* @throws SmackOpenPgpException
|
||||
*/
|
||||
Set<OpenPgpV4Fingerprint> getAvailableKeyPairFingerprints(BareJid owner) throws SmackOpenPgpException;
|
||||
|
||||
|
|
Loading…
Reference in a new issue