1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-30 16:02:07 +01:00

Add first JET test

This commit is contained in:
vanitasvitae 2017-07-30 23:32:00 +02:00
parent b0b46ad167
commit 177ed6ea0d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 160 additions and 17 deletions

View file

@ -37,21 +37,22 @@ public abstract class AesGcmNoPadding {
protected final Cipher cipher; protected final Cipher cipher;
protected final byte[] key, iv, keyAndIv; protected final byte[] key, iv, keyAndIv;
public AesGcmNoPadding(int length) throws NoSuchAlgorithmException, NoSuchProviderException, public AesGcmNoPadding(int bits) throws NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException { NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
this.length = length; this.length = bits;
int bytes = bits / 8;
KeyGenerator keyGenerator = KeyGenerator.getInstance(keyType); KeyGenerator keyGenerator = KeyGenerator.getInstance(keyType);
keyGenerator.init(length); keyGenerator.init(bits);
key = keyGenerator.generateKey().getEncoded(); key = keyGenerator.generateKey().getEncoded();
SecureRandom secureRandom = new SecureRandom(); SecureRandom secureRandom = new SecureRandom();
iv = new byte[length]; iv = new byte[bytes];
secureRandom.nextBytes(iv); secureRandom.nextBytes(iv);
keyAndIv = new byte[2 * length]; keyAndIv = new byte[2 * bytes];
System.arraycopy(key, 0, keyAndIv, 0, length); System.arraycopy(key, 0, keyAndIv, 0, bytes);
System.arraycopy(iv, 0, keyAndIv, length, length); System.arraycopy(iv, 0, keyAndIv, bytes, bytes);
SecretKey secretKey = new SecretKeySpec(key, keyType); SecretKey secretKey = new SecretKeySpec(key, keyType);
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
@ -65,9 +66,11 @@ public abstract class AesGcmNoPadding {
this.key = key; this.key = key;
this.iv = iv; this.iv = iv;
keyAndIv = new byte[2 * length]; int bytes = length / 8;
System.arraycopy(key, 0, keyAndIv, 0, length);
System.arraycopy(iv, 0, keyAndIv, length, length); keyAndIv = new byte[2 * bytes];
System.arraycopy(key, 0, keyAndIv, 0, bytes);
System.arraycopy(iv, 0, keyAndIv, bytes, bytes);
cipher = Cipher.getInstance(cipherMode, "BC"); cipher = Cipher.getInstance(cipherMode, "BC");
SecretKeySpec keySpec = new SecretKeySpec(key, keyType); SecretKeySpec keySpec = new SecretKeySpec(key, keyType);

View file

@ -23,6 +23,7 @@ import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jet.internal.JetSecurity; import org.jivesoftware.smackx.jet.internal.JetSecurity;
import org.jivesoftware.smackx.jft.JingleFileTransferManager; import org.jivesoftware.smackx.jft.JingleFileTransferManager;
import org.jivesoftware.smackx.jft.controller.OutgoingFileOfferController; import org.jivesoftware.smackx.jft.controller.OutgoingFileOfferController;
@ -54,6 +55,8 @@ public final class JetManager extends Manager implements JingleDescriptionManage
private JetManager(XMPPConnection connection) { private JetManager(XMPPConnection connection) {
super(connection); super(connection);
this.jingleManager = JingleManager.getInstanceFor(connection); this.jingleManager = JingleManager.getInstanceFor(connection);
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(getNamespace());
jingleManager.addJingleDescriptionManager(this);
} }
public static JetManager getInstanceFor(XMPPConnection connection) { public static JetManager getInstanceFor(XMPPConnection connection) {

View file

@ -0,0 +1,137 @@
package org.jivesoftware.smackx.jet;
import static org.jivesoftware.smackx.jft.JingleFileTransferTest.prepareNewTestFile;
import static org.jivesoftware.smackx.omemo.OmemoIntegrationTestHelper.cleanServerSideTraces;
import static org.jivesoftware.smackx.omemo.OmemoIntegrationTestHelper.setUpOmemoManager;
import static org.jivesoftware.smackx.omemo.OmemoIntegrationTestHelper.subscribe;
import static org.jivesoftware.smackx.omemo.OmemoIntegrationTestHelper.unidirectionalTrust;
import static org.junit.Assert.assertArrayEquals;
import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.Future;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jft.JingleFileTransferManager;
import org.jivesoftware.smackx.jft.controller.IncomingFileOfferController;
import org.jivesoftware.smackx.jft.listener.IncomingFileOfferListener;
import org.jivesoftware.smackx.jft.listener.ProgressListener;
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.JingleIBBTransportManager;
import org.jivesoftware.smackx.omemo.AbstractOmemoIntegrationTest;
import org.jivesoftware.smackx.omemo.OmemoManager;
import org.jivesoftware.smackx.omemo.OmemoService;
import org.jivesoftware.smackx.omemo.OmemoStore;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.TestNotPossibleException;
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
public class JetTest extends AbstractOmemoIntegrationTest {
private OmemoManager oa, ob;
private JetManager ja, jb;
private JingleIBBTransportManager ia, ib;
private OmemoStore<?,?,?,?,?,?,?,?,?> store;
private static final File tempDir;
static {
String userHome = System.getProperty("user.home");
if (userHome != null) {
File f = new File(userHome);
tempDir = new File(f, ".config/smack-integration-test/");
} else {
tempDir = new File("int_test_jingle");
}
}
public JetTest(SmackIntegrationTestEnvironment environment)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException, TestNotPossibleException {
super(environment);
}
@Override
public void before() {
store = OmemoService.getInstance().getOmemoStoreBackend();
oa = OmemoManager.getInstanceFor(conOne, 666);
ob = OmemoManager.getInstanceFor(conTwo, 777);
ja = JetManager.getInstanceFor(conOne);
jb = JetManager.getInstanceFor(conTwo);
ia = JingleIBBTransportManager.getInstanceFor(conOne);
ib = JingleIBBTransportManager.getInstanceFor(conTwo);
}
@SmackIntegrationTest
public void JingleEncryptedFileTransferTest()
throws Exception {
final SimpleResultSyncPoint received = new SimpleResultSyncPoint();
//Setup OMEMO
subscribe(oa, ob, "Bob");
subscribe(ob, oa, "Alice");
setUpOmemoManager(oa);
setUpOmemoManager(ob);
unidirectionalTrust(oa, ob);
unidirectionalTrust(ob, oa);
ja.registerEncryptionMethod(oa);
jb.registerEncryptionMethod(ob);
File source = prepareNewTestFile("source");
final File target = new File(tempDir, "target");
JingleFileTransferManager.getInstanceFor(conTwo).addIncomingFileOfferListener(new IncomingFileOfferListener() {
@Override
public void onIncomingFileOffer(IncomingFileOfferController offer) {
try {
offer.addProgressListener(new ProgressListener() {
@Override
public void started() {
}
@Override
public void progress(float percent) {
}
@Override
public void finished() {
received.signal();
}
});
Future<Void> f = offer.accept(conTwo, target);
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
received.signal(e);
}
}
});
ja.sendEncryptedFile(conTwo.getUser().asFullJidOrThrow(), source, oa);
received.waitForResult(60 * 1000);
FileInputStream sIn = new FileInputStream(source);
FileInputStream tIn = new FileInputStream(target);
byte[] sB = new byte[(int) source.length()];
byte[] tB = new byte[(int) target.length()];
sIn.read(sB);
tIn.read(tB);
assertArrayEquals(sB, tB);
}
@Override
public void after() {
oa.shutdown();
ob.shutdown();
cleanServerSideTraces(oa);
cleanServerSideTraces(ob);
}
}

View file

@ -154,7 +154,7 @@ public class JingleFileTransferTest extends AbstractSmackIntegrationTest {
} }
private File prepareNewTestFile(String name) { public static File prepareNewTestFile(String name) {
File testFile = new File(tempDir, name); File testFile = new File(tempDir, name);
try { try {
if (!testFile.exists()) { if (!testFile.exists()) {

View file

@ -41,11 +41,11 @@ import org.jivesoftware.smackx.pubsub.PubSubManager;
/** /**
* Class containing some helper methods for OmemoIntegrationTests. * Class containing some helper methods for OmemoIntegrationTests.
*/ */
final class OmemoIntegrationTestHelper { public final class OmemoIntegrationTestHelper {
private static final Logger LOGGER = Logger.getLogger(OmemoIntegrationTestHelper.class.getSimpleName()); private static final Logger LOGGER = Logger.getLogger(OmemoIntegrationTestHelper.class.getSimpleName());
static void cleanServerSideTraces(OmemoManager omemoManager) { public static void cleanServerSideTraces(OmemoManager omemoManager) {
cleanUpPubSub(omemoManager); cleanUpPubSub(omemoManager);
cleanUpRoster(omemoManager); cleanUpRoster(omemoManager);
} }
@ -115,7 +115,7 @@ final class OmemoIntegrationTestHelper {
* @throws InterruptedException * @throws InterruptedException
* @throws SmackException.NoResponseException * @throws SmackException.NoResponseException
*/ */
static void subscribe(OmemoManager alice, OmemoManager bob, String nick) public static void subscribe(OmemoManager alice, OmemoManager bob, String nick)
throws SmackException.NotLoggedInException, XMPPException.XMPPErrorException, throws SmackException.NotLoggedInException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, InterruptedException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException { SmackException.NoResponseException {
@ -127,7 +127,7 @@ final class OmemoIntegrationTestHelper {
} }
static void unidirectionalTrust(OmemoManager alice, OmemoManager bob) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, CannotEstablishOmemoSessionException { public static void unidirectionalTrust(OmemoManager alice, OmemoManager bob) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, CannotEstablishOmemoSessionException {
//Fetch deviceList //Fetch deviceList
alice.requestDeviceListUpdateFor(bob.getOwnJid()); alice.requestDeviceListUpdateFor(bob.getOwnJid());
LOGGER.log(Level.INFO, "Current deviceList state: " + alice.getOwnDevice() + " knows " + bob.getOwnDevice() + ": " LOGGER.log(Level.INFO, "Current deviceList state: " + alice.getOwnDevice() + " knows " + bob.getOwnDevice() + ": "
@ -147,7 +147,7 @@ final class OmemoIntegrationTestHelper {
} }
static void setUpOmemoManager(OmemoManager omemoManager) throws CorruptedOmemoKeyException, InterruptedException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, PubSubException.NotALeafNodeException { public static void setUpOmemoManager(OmemoManager omemoManager) throws CorruptedOmemoKeyException, InterruptedException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, PubSubException.NotALeafNodeException {
omemoManager.initialize(); omemoManager.initialize();
OmemoBundleElement bundle = OmemoService.fetchBundle(omemoManager, omemoManager.getOwnDevice()); OmemoBundleElement bundle = OmemoService.fetchBundle(omemoManager, omemoManager.getOwnDevice());
assertNotNull("Bundle must not be null.", bundle); assertNotNull("Bundle must not be null.", bundle);

View file

@ -48,7 +48,7 @@ public class SmackOmemoSignalIntegrationTestFramework {
SignalOmemoService.acknowledgeLicense(); SignalOmemoService.acknowledgeLicense();
SignalOmemoService.setup(); SignalOmemoService.setup();
final String[] smackOmemoPackages = new String[] { "org.jivesoftware.smackx.omemo" }; final String[] smackOmemoPackages = new String[] { "org.jivesoftware.smackx.omemo", "org.jivesoftware.smackx.jet" };
SmackIntegrationTestFramework.main(smackOmemoPackages); SmackIntegrationTestFramework.main(smackOmemoPackages);
} }