1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-10-31 17:25:58 +01:00

Rework some tests

This commit is contained in:
Paul Schaub 2018-06-26 17:59:42 +02:00
parent a6fb30668e
commit b1dc138ae9
3 changed files with 69 additions and 44 deletions

View file

@ -22,6 +22,7 @@ import static junit.framework.TestCase.assertTrue;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException; import java.security.NoSuchProviderException;
@ -36,6 +37,7 @@ import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint; import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
import org.jivesoftware.smackx.ox.TestKeys;
import org.jivesoftware.smackx.ox.chat.OpenPgpContact; import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
import org.jivesoftware.smackx.ox.chat.OpenPgpFingerprints; import org.jivesoftware.smackx.ox.chat.OpenPgpFingerprints;
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement; import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
@ -46,31 +48,31 @@ import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException; import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException; import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException; import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
import org.jivesoftware.smackx.ox.util.KeyBytesAndFingerprint;
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector; import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
import de.vanitasvitae.crypto.pgpainless.util.BCUtil;
import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.encoders.Base64;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.JidTestUtil;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
public class DryOxEncryptionTest extends OxTestSuite { public class DryOxEncryptionTest extends OxTestSuite {
private static final Logger LOGGER = Logger.getLogger(DryOxEncryptionTest.class.getName()); private static final Logger LOGGER = Logger.getLogger(DryOxEncryptionTest.class.getName());
private static final Charset UTF8 = Charset.forName("UTF-8");
private static final File alicePath = FileUtils.getTempDir("ox-alice"); private static final File julietPath = FileUtils.getTempDir("ox-juliet");
private static final File bobPath = FileUtils.getTempDir("ox-bob"); private static final File romeoPath = FileUtils.getTempDir("ox-romeo");
@BeforeClass @BeforeClass
@AfterClass @AfterClass
public static void deletePath() { public static void deletePath() {
LOGGER.log(Level.INFO, "Delete paths " + alicePath.getAbsolutePath() + " " + bobPath.getAbsolutePath()); LOGGER.log(Level.INFO, "Delete paths " + julietPath.getAbsolutePath() + " " + romeoPath.getAbsolutePath());
FileUtils.deleteDirectory(alicePath); FileUtils.deleteDirectory(julietPath);
FileUtils.deleteDirectory(bobPath); FileUtils.deleteDirectory(romeoPath);
} }
@Test @Test
@ -78,64 +80,65 @@ public class DryOxEncryptionTest extends OxTestSuite {
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException, SmackOpenPgpException, MissingUserIdOnKeyException, MissingOpenPgpPublicKeyException, IOException, SmackOpenPgpException, MissingUserIdOnKeyException, MissingOpenPgpPublicKeyException,
MissingOpenPgpKeyPairException, XmlPullParserException { MissingOpenPgpKeyPairException, XmlPullParserException {
BareJid alice = JidTestUtil.BARE_JID_1; BareJid juliet = TestKeys.JULIET_JID;
BareJid bob = JidTestUtil.BARE_JID_2; BareJid romemo = TestKeys.ROMEO_JID;
FileBasedPainlessOpenPgpStore aliceStore = new FileBasedPainlessOpenPgpStore(alicePath, new UnprotectedKeysProtector()); FileBasedPainlessOpenPgpStore julietStore = new FileBasedPainlessOpenPgpStore(julietPath, new UnprotectedKeysProtector());
FileBasedPainlessOpenPgpStore bobStore = new FileBasedPainlessOpenPgpStore(bobPath, new UnprotectedKeysProtector()); FileBasedPainlessOpenPgpStore romeoStore = new FileBasedPainlessOpenPgpStore(romeoPath, new UnprotectedKeysProtector());
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(alice, aliceStore); PainlessOpenPgpProvider julietProvider = new PainlessOpenPgpProvider(juliet, julietStore);
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bob, bobStore); PainlessOpenPgpProvider romeoProvider = new PainlessOpenPgpProvider(romemo, romeoStore);
KeyBytesAndFingerprint aliceKey = aliceProvider.generateOpenPgpKeyPair(alice); OpenPgpV4Fingerprint julietFinger = julietProvider.importSecretKey(juliet,
KeyBytesAndFingerprint bobKey = bobProvider.generateOpenPgpKeyPair(bob); BCUtil.getDecodedBytes(TestKeys.JULIET_PRIV.getBytes(UTF8)));
OpenPgpV4Fingerprint romeoFinger = romeoProvider.importSecretKey(romemo,
BCUtil.getDecodedBytes(TestKeys.ROMEO_PRIV.getBytes(UTF8)));
aliceProvider.importSecretKey(alice, aliceKey.getBytes()); julietStore.setPrimaryOpenPgpKeyPairFingerprint(julietFinger);
bobProvider.importSecretKey(bob, bobKey.getBytes()); romeoStore.setPrimaryOpenPgpKeyPairFingerprint(romeoFinger);
aliceStore.setPrimaryOpenPgpKeyPairFingerprint(aliceKey.getFingerprint()); byte[] julietPubBytes = julietStore.getPublicKeyRingBytes(juliet, julietFinger);
bobStore.setPrimaryOpenPgpKeyPairFingerprint(bobKey.getFingerprint()); byte[] romeoPubBytes = romeoStore.getPublicKeyRingBytes(romemo, romeoFinger);
byte[] alicePubBytes = aliceStore.getPublicKeyRingBytes(alice, aliceKey.getFingerprint()); assertNotNull(julietPubBytes);
byte[] bobPubBytes = bobStore.getPublicKeyRingBytes(bob, bobKey.getFingerprint()); assertNotNull(romeoPubBytes);
assertTrue(julietPubBytes.length != 0);
assertTrue(romeoPubBytes.length != 0);
assertNotNull(alicePubBytes); PubkeyElement julietPub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
assertNotNull(bobPubBytes); Base64.encode(julietStore.getPublicKeyRingBytes(juliet, julietFinger))),
assertTrue(alicePubBytes.length != 0);
assertTrue(bobPubBytes.length != 0);
PubkeyElement alicePub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
Base64.encode(aliceStore.getPublicKeyRingBytes(alice, aliceKey.getFingerprint()))),
new Date()); new Date());
PubkeyElement bobPub = new PubkeyElement(new PubkeyElement.PubkeyDataElement( PubkeyElement romeoPub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
Base64.encode(bobStore.getPublicKeyRingBytes(bob, bobKey.getFingerprint()))), Base64.encode(romeoStore.getPublicKeyRingBytes(romemo, romeoFinger))),
new Date()); new Date());
aliceProvider.importPublicKey(bob, Base64.decode(bobPub.getDataElement().getB64Data())); julietProvider.importPublicKey(romemo, Base64.decode(romeoPub.getDataElement().getB64Data()));
bobProvider.importPublicKey(alice, Base64.decode(alicePub.getDataElement().getB64Data())); romeoProvider.importPublicKey(juliet, Base64.decode(julietPub.getDataElement().getB64Data()));
aliceStore.setAnnouncedKeysFingerprints(bob, Collections.singletonMap(bobKey.getFingerprint(), new Date())); julietStore.setAnnouncedKeysFingerprints(romemo, Collections.singletonMap(romeoFinger, new Date()));
bobStore.setAnnouncedKeysFingerprints(alice, Collections.singletonMap(aliceKey.getFingerprint(), new Date())); romeoStore.setAnnouncedKeysFingerprints(juliet, Collections.singletonMap(julietFinger, new Date()));
OpenPgpFingerprints aliceFingerprints = new OpenPgpFingerprints(alice, OpenPgpFingerprints julietFingerprints = new OpenPgpFingerprints(juliet,
Collections.singleton(aliceKey.getFingerprint()), Collections.singleton(julietFinger),
Collections.singleton(aliceKey.getFingerprint()), Collections.singleton(julietFinger),
new HashMap<OpenPgpV4Fingerprint, Throwable>()); new HashMap<OpenPgpV4Fingerprint, Throwable>());
OpenPgpFingerprints bobFingerprints = new OpenPgpFingerprints(bob, OpenPgpFingerprints romeoFingerprints = new OpenPgpFingerprints(romemo,
Collections.singleton(bobKey.getFingerprint()), Collections.singleton(romeoFinger),
Collections.singleton(bobKey.getFingerprint()), Collections.singleton(romeoFinger),
new HashMap<OpenPgpV4Fingerprint, Throwable>()); new HashMap<OpenPgpV4Fingerprint, Throwable>());
OpenPgpContact aliceForBob = new OpenPgpContact(bobProvider, alice, bobFingerprints, aliceFingerprints); OpenPgpContact julietForRomeo = new OpenPgpContact(romeoProvider, juliet, romeoFingerprints, julietFingerprints);
OpenPgpContact bobForAlice = new OpenPgpContact(aliceProvider, bob, aliceFingerprints, bobFingerprints); OpenPgpContact romeoForJuliet = new OpenPgpContact(julietProvider, romemo, julietFingerprints, romeoFingerprints);
String bodyText = "Finden wir eine Kompromisslösung machen wir es so, wie ich es sage."; String bodyText = "Finden wir eine Kompromisslösung machen wir es so, wie ich es sage.";
List<ExtensionElement> payload = Collections.<ExtensionElement>singletonList(new Message.Body("de", List<ExtensionElement> payload = Collections.<ExtensionElement>singletonList(new Message.Body("de",
bodyText)); bodyText));
OpenPgpElement encrypted = bobForAlice.encryptAndSign(payload); OpenPgpElement encrypted = romeoForJuliet.encryptAndSign(payload);
OpenPgpContentElement decrypted = aliceForBob.receive(encrypted); LOGGER.log(Level.INFO, encrypted.toXML(null).toString());
OpenPgpContentElement decrypted = julietForRomeo.receive(encrypted);
assertTrue(decrypted instanceof SigncryptElement); assertTrue(decrypted instanceof SigncryptElement);
assertEquals(1, decrypted.getExtensions().size()); assertEquals(1, decrypted.getExtensions().size());

View file

@ -169,6 +169,8 @@ public class OpenPgpElementTest extends SmackTestSuite {
assertEquals(element.getTimestamp(), parsed.getTimestamp()); assertEquals(element.getTimestamp(), parsed.getTimestamp());
assertEquals(element.getTo(), parsed.getTo()); assertEquals(element.getTo(), parsed.getTo());
assertEquals(element.getExtensions(), parsed.getExtensions()); assertEquals(element.getExtensions(), parsed.getExtensions());
assertEquals(payload.get(0), element.getExtension(Message.Body.NAMESPACE));
assertEquals(payload.get(0), element.getExtension(Message.Body.ELEMENT, Message.Body.NAMESPACE));
} }

View file

@ -16,8 +16,28 @@
*/ */
package org.jivesoftware.smackx.ox; package org.jivesoftware.smackx.ox;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public class TestKeys { public class TestKeys {
public TestKeys() {
}
public static final BareJid JULIET_JID;
public static final BareJid ROMEO_JID;
static {
try {
JULIET_JID = JidCreate.bareFrom("juliet@capulet.lit");
ROMEO_JID = JidCreate.bareFrom("romeo@montague.lit");
} catch (XmppStringprepException e) {
throw new AssertionError(e);
}
}
public static final String JULIET_UID = "xmpp:juliet@capulet.lit"; public static final String JULIET_UID = "xmpp:juliet@capulet.lit";
/** /**