improve tests

This commit is contained in:
Paul Schaub 2018-06-20 12:45:05 +02:00
parent ffbfae9856
commit cdba5bdda9
7 changed files with 152 additions and 56 deletions

View File

@ -75,7 +75,7 @@ public class FileBasedPainlessOpenPgpStore extends AbstractPainlessOpenPgpStore
inputStream.close();
bytes = buffer.toByteArray();
} catch (FileNotFoundException e) {
LOGGER.log(Level.INFO, "Pubring of user " + owner.toString() + " does not exist.");
LOGGER.log(Level.FINE, "Pubring of user " + owner.toString() + " does not exist.");
return null;
} catch (IOException e) {
if (inputStream != null) {
@ -101,7 +101,7 @@ public class FileBasedPainlessOpenPgpStore extends AbstractPainlessOpenPgpStore
inputStream.close();
bytes = buffer.toByteArray();
} catch (FileNotFoundException e) {
LOGGER.log(Level.INFO, "Secring of user " + owner.toString() + " does not exist.");
LOGGER.log(Level.FINE, "Secring of user " + owner.toString() + " does not exist.");
return null;
} catch (IOException e) {
if (inputStream != null) {
@ -190,7 +190,7 @@ public class FileBasedPainlessOpenPgpStore extends AbstractPainlessOpenPgpStore
}
} catch (PGPException | IOException e) {
LOGGER.log(Level.WARNING, "Could not read public keys of contact " + contact.toString(), e);
throw new SmackOpenPgpException("Could not read public keys of contact " + contact.toString(), e);
}
return availableFingerprints;
}
@ -298,7 +298,7 @@ public class FileBasedPainlessOpenPgpStore extends AbstractPainlessOpenPgpStore
try {
File contactsDir = getContactsPath(false);
if (!contactsDir.exists()) {
LOGGER.log(Level.INFO, "Contacts directory does not exists yet.");
LOGGER.log(Level.FINE, "Contacts directory does not exists yet.");
return trustedFingerprints;
}
@ -419,7 +419,6 @@ public class FileBasedPainlessOpenPgpStore extends AbstractPainlessOpenPgpStore
return file;
}
private static void createDirectoryOrThrow(File dir) throws IOException {
if (!dir.mkdirs()) {
throw new IOException("Could not create directory \"" + dir.getAbsolutePath() + "\"");

View File

@ -24,6 +24,7 @@ import java.security.NoSuchProviderException;
import java.util.Collections;
import java.util.Date;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.ox.element.PubkeyElement;
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
@ -33,23 +34,22 @@ import org.jivesoftware.smackx.ox.util.KeyBytesAndFingerprint;
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
import org.bouncycastle.openpgp.PGPException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.JidTestUtil;
public class DryOxEncryptionTest extends OxTestSuite {
private static File getTempDir(String suffix) {
String temp = System.getProperty("java.io.tmpdir");
if (temp == null) {
temp = "tmp";
}
private final File alicePath = FileUtils.getTempDir("ox-alice");
private final File bobPath = FileUtils.getTempDir("ox-bob");
if (suffix == null) {
return new File(temp);
} else {
return new File(temp, suffix);
}
@Before
@After
public void deletePath() {
FileUtils.deleteDirectory(alicePath);
FileUtils.deleteDirectory(bobPath);
}
@Test
@ -59,8 +59,6 @@ public class DryOxEncryptionTest extends OxTestSuite {
BareJid alice = JidTestUtil.BARE_JID_1;
BareJid bob = JidTestUtil.BARE_JID_2;
File alicePath = getTempDir("ox-alice");
File bobPath = getTempDir("ox-bob");
FileBasedPainlessOpenPgpStore aliceStore = new FileBasedPainlessOpenPgpStore(alicePath, new UnprotectedKeysProtector());
FileBasedPainlessOpenPgpStore bobStore = new FileBasedPainlessOpenPgpStore(bobPath, new UnprotectedKeysProtector());
@ -85,7 +83,5 @@ public class DryOxEncryptionTest extends OxTestSuite {
aliceStore.setAnnouncedKeysFingerprints(bob, Collections.singletonMap(bobKey.getFingerprint(), new Date()));
bobStore.setAnnouncedKeysFingerprints(alice, Collections.singletonMap(aliceKey.getFingerprint(), new Date()));
}
}

View File

@ -39,37 +39,28 @@ import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.jid.JidTestUtil;
public class FileBasedPainlessOpenPgpStoreTest extends OxTestSuite {
private static final File basePath;
private static final BareJid alice;
private static final BareJid bob;
private final File basePath;
private final BareJid alice;
private final BareJid bob;
private FileBasedPainlessOpenPgpStore store;
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);
}
public FileBasedPainlessOpenPgpStoreTest() {
super();
this.basePath = FileUtils.getTempDir("ox-filebased-store-test");
this.alice = JidTestUtil.BARE_JID_1;
this.bob = JidTestUtil.BARE_JID_2;
}
@After
@Before
public void deleteStore() {
FileUtils.deleteDirectory(basePath);

View File

@ -0,0 +1,75 @@
/**
*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.ox.bouncycastle;
import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
import de.vanitasvitae.crypto.pgpainless.PGPainless;
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.JidTestUtil;
public class UserIdTest extends OxTestSuite {
private final File path;
public UserIdTest() {
this.path = FileUtils.getTempDir("ox-user-id-test");
}
@After
@Before
public void deleteDir() {
FileUtils.deleteDirectory(path);
}
@Test
public void requireUserIdOnImportTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException, SmackOpenPgpException, MissingUserIdOnKeyException {
BareJid owner = JidTestUtil.BARE_JID_1;
FileBasedPainlessOpenPgpStore store = new FileBasedPainlessOpenPgpStore(path, new UnprotectedKeysProtector());
PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(owner, store);
PGPSecretKeyRing ownerKey = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:" + owner.toString());
provider.importSecretKey(owner, ownerKey.getEncoded());
}
@Test(expected = MissingUserIdOnKeyException.class)
public void throwOnMissingUserIdTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException, SmackOpenPgpException, MissingUserIdOnKeyException {
BareJid owner = JidTestUtil.BARE_JID_1;
BareJid stranger = JidTestUtil.BARE_JID_2;
FileBasedPainlessOpenPgpStore store = new FileBasedPainlessOpenPgpStore(path, new UnprotectedKeysProtector());
PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(owner, store);
PGPSecretKeyRing strangerKey = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:" + stranger.toString());
provider.importSecretKey(owner, strangerKey.getEncoded());
}
}

View File

@ -99,7 +99,7 @@ public final class OXInstantMessagingManager extends Manager implements Signcryp
public boolean contactSupportsOxInstantMessaging(OpenPgpContact contact)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException {
return contactSupportsOxInstantMessaging(contact.getJidOfChatPartner().asBareJid());
return contactSupportsOxInstantMessaging(contact.getJid());
}
public boolean addOxMessageListener(OxMessageListener listener) {

View File

@ -244,8 +244,7 @@ public final class OpenPgpManager extends Manager {
if (openPgpContact == null) {
OpenPgpFingerprints theirKeys = determineContactsKeys(jid);
OpenPgpFingerprints ourKeys = determineContactsKeys(connection().getUser().asBareJid());
Chat chat = ChatManager.getInstanceFor(connection()).chatWith(jid);
openPgpContact = new OpenPgpContact(getOpenPgpProvider(), chat, ourKeys, theirKeys);
openPgpContact = new OpenPgpContact(getOpenPgpProvider(), jid, ourKeys, theirKeys);
openPgpCapableContacts.put(jid, openPgpContact);
}

View File

@ -22,7 +22,8 @@ import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.MultiMap;
@ -40,44 +41,79 @@ import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
import org.jivesoftware.smackx.ox.util.DecryptedBytesAndMetadata;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.Jid;
import org.xmlpull.v1.XmlPullParserException;
public class OpenPgpContact {
private final Chat chat;
private final BareJid jid;
private final OpenPgpFingerprints contactsFingerprints;
private final OpenPgpFingerprints ourFingerprints;
private final OpenPgpProvider cryptoProvider;
private final OpenPgpV4Fingerprint singingKey;
public OpenPgpContact(OpenPgpProvider cryptoProvider,
Chat chat,
BareJid jid,
OpenPgpFingerprints ourFingerprints,
OpenPgpFingerprints contactsFingerprints) {
this.cryptoProvider = cryptoProvider;
this.chat = chat;
this.jid = jid;
this.singingKey = cryptoProvider.getStore().getPrimaryOpenPgpKeyPairFingerprint();
this.ourFingerprints = ourFingerprints;
this.contactsFingerprints = contactsFingerprints;
}
public EntityBareJid getJidOfChatPartner() {
return chat.getXmppAddressOfChatPartner();
public BareJid getJid() {
return jid;
}
public OpenPgpFingerprints getContactsFingerprints() {
public OpenPgpFingerprints getFingerprints() {
return contactsFingerprints;
}
public void send(Message message, List<ExtensionElement> payload)
public void addSignedEncryptedPayloadTo(Message message, List<ExtensionElement> payload)
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException {
MultiMap<BareJid, OpenPgpV4Fingerprint> fingerprints = oursAndRecipientFingerprints();
SigncryptElement preparedPayload = new SigncryptElement(
Collections.<Jid>singleton(getJid()),
payload);
OpenPgpElement encryptedPayload;
byte[] encryptedBytes;
// Encrypt the payload
try {
encryptedBytes = cryptoProvider.signAndEncrypt(
preparedPayload,
singingKey,
fingerprints);
} catch (MissingOpenPgpPublicKeyException e) {
throw new AssertionError("Missing OpenPGP public key, even though this should not happen here.", e);
}
encryptedPayload = new OpenPgpElement(Base64.encodeToString(encryptedBytes));
// Add encrypted payload to message
message.addExtension(encryptedPayload);
// Add additional information to the message
// STOPSHIP: 20.06.18 BELOW
// TODO: Check if message already contains EME element.
message.addExtension(new ExplicitMessageEncryptionElement(
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
StoreHint.set(message);
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
}
public void send(XMPPConnection connection, Message message, List<ExtensionElement> payload)
throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException,
SmackOpenPgpException, IOException {
MultiMap<BareJid, OpenPgpV4Fingerprint> fingerprints = oursAndRecipientFingerprints();
SigncryptElement preparedPayload = new SigncryptElement(
Collections.<Jid>singleton(chat.getXmppAddressOfChatPartner()),
Collections.<Jid>singleton(getJid()),
payload);
OpenPgpElement encryptedPayload;
@ -104,22 +140,22 @@ public class OpenPgpContact {
StoreHint.set(message);
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
chat.send(message);
ChatManager.getInstanceFor(connection).chatWith(getJid().asEntityBareJidIfPossible()).send(message);
}
public void send(Message message, CharSequence body)
public void send(XMPPConnection connection, Message message, CharSequence body)
throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException,
SmackOpenPgpException, IOException {
List<ExtensionElement> payload = new ArrayList<>();
payload.add(new Message.Body(null, body.toString()));
send(message, payload);
send(connection, message, payload);
}
public OpenPgpContentElement receive(OpenPgpElement element)
throws XmlPullParserException, MissingOpenPgpKeyPairException, SmackOpenPgpException, IOException {
byte[] decoded = Base64.decode(element.getEncryptedBase64MessageContent());
DecryptedBytesAndMetadata decryptedBytes = cryptoProvider.decrypt(decoded, getJidOfChatPartner(), null);
DecryptedBytesAndMetadata decryptedBytes = cryptoProvider.decrypt(decoded, getJid(), null);
OpenPgpMessage openPgpMessage = new OpenPgpMessage(decryptedBytes.getBytes(),
new OpenPgpMessage.Metadata(decryptedBytes.getDecryptionKey(),