mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-08 03:51:09 +01:00
improve tests
This commit is contained in:
parent
ffbfae9856
commit
cdba5bdda9
7 changed files with 152 additions and 56 deletions
|
|
@ -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() + "\"");
|
||||
|
|
|
|||
|
|
@ -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()));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue