Search in sources :

Example 46 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class OmemoDeviceTest method testEquals.

/**
 * Test, if the equals() method works as intended.
 */
@Test
public void testEquals() {
    BareJid romeo, juliet, guyUnderTheBalcony;
    try {
        romeo = JidCreate.bareFrom("romeo@shakespeare.lit");
        guyUnderTheBalcony = JidCreate.bareFrom("romeo@shakespeare.lit/underTheBalcony");
        juliet = JidCreate.bareFrom("juliet@shakespeare.lit");
    } catch (XmppStringprepException e) {
        Assert.fail(e.getMessage());
        return;
    }
    OmemoDevice r = new OmemoDevice(romeo, 1);
    OmemoDevice g = new OmemoDevice(guyUnderTheBalcony, 1);
    OmemoDevice r2 = new OmemoDevice(romeo, 2);
    OmemoDevice j = new OmemoDevice(juliet, 3);
    OmemoDevice j2 = new OmemoDevice(juliet, 1);
    assertTrue(r.equals(g));
    assertFalse(r.equals(r2));
    assertFalse(j.equals(j2));
    assertFalse(j2.equals(r2));
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) BareJid(org.jxmpp.jid.BareJid) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) Test(org.junit.Test)

Example 47 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class PacketWriterTest method shouldBlockAndUnblockTest.

/**
 * Make sure that stanza writer does block once the queue reaches
 * {@link PacketWriter#QUEUE_SIZE} and that
 * {@link PacketWriter#sendStanza(org.jivesoftware.smack.tcp.packet.Packet)} does unblock after the
 * interrupt.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws BrokenBarrierException in case of a broken barrier.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws XmppStringprepException if the provided string is invalid.
 * @throws SecurityException if there was a security violation.
 * @throws NoSuchFieldException if there is no such field.
 * @throws IllegalAccessException if there was an illegal access.
 * @throws IllegalArgumentException if an illegal argument was given.
 */
@Test
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException, XmppStringprepException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    XMPPTCPConnection connection = new XMPPTCPConnection("user", "pass", "example.org");
    // Get the protected "Reader reader" field from AbstractXMPPConnection and set it to a dummy Reader,
    // as otherwise it will be null when we perform the writer threads init() which will make the StAX parser
    // to throw an exception.
    Field readerField = AbstractXMPPConnection.class.getDeclaredField("reader");
    readerField.setAccessible(true);
    readerField.set(connection, DUMMY_READER);
    final PacketWriter pw = connection.packetWriter;
    BlockingStringWriter blockingStringWriter = new BlockingStringWriter();
    connection.setWriter(blockingStringWriter);
    connection.packetWriter.init();
    // blocking writer.
    for (int i = 0; i < XMPPTCPConnection.PacketWriter.QUEUE_SIZE + 1; i++) {
        pw.sendStreamElement(StanzaBuilder.buildMessage().build());
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final AtomicReference<Exception> unexpectedThreadExceptionReference = new AtomicReference<>();
    final AtomicReference<Exception> expectedThreadExceptionReference = new AtomicReference<>();
    shutdown = false;
    prematureUnblocked = false;
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                barrier.await();
                pw.sendStreamElement(StanzaBuilder.buildMessage().build());
                // should only return after the pw was interrupted
                if (!shutdown) {
                    prematureUnblocked = true;
                }
            } catch (InterruptedException | SmackException.NotConnectedException e) {
                // This is the exception we expect.
                expectedThreadExceptionReference.set(e);
            } catch (BrokenBarrierException e) {
                unexpectedThreadExceptionReference.set(e);
            }
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                unexpectedThreadExceptionReference.set(e);
            }
        }
    });
    t.start();
    // This barrier is not strictly necessary, but may increases the chances that the threat
    // will block before we call shutdown. Otherwise we may get false positives (which is still
    // better then false negatives).
    barrier.await();
    // Not really cool, but may increases the chances for 't' to block in sendStanza.
    Thread.sleep(250);
    // Shutdown the packetwriter, this will also interrupt the writer thread, which is what we hope to happen in the
    // thread created above.
    pw.shutdown(false);
    shutdown = true;
    barrier.await();
    t.join(60000);
    Exception unexpectedThreadException = unexpectedThreadExceptionReference.get();
    try {
        if (prematureUnblocked) {
            String failureMessage = "Should not unblock before the thread got shutdown.";
            if (unexpectedThreadException != null) {
                String stacktrace = ExceptionUtil.getStackTrace(unexpectedThreadException);
                failureMessage += " Unexpected thread exception thrown: " + unexpectedThreadException + "\n" + stacktrace;
            }
            fail(failureMessage);
        } else if (unexpectedThreadException != null) {
            String stacktrace = ExceptionUtil.getStackTrace(unexpectedThreadException);
            fail("Unexpected thread exception: " + unexpectedThreadException + "\n" + stacktrace);
        }
        assertNotNull(expectedThreadExceptionReference.get(), "Did not encounter expected exception on sendStreamElement()");
    } finally {
        blockingStringWriter.unblock();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) SmackException(org.jivesoftware.smack.SmackException) AtomicReference(java.util.concurrent.atomic.AtomicReference) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Field(java.lang.reflect.Field) PacketWriter(org.jivesoftware.smack.tcp.XMPPTCPConnection.PacketWriter) Test(org.junit.jupiter.api.Test)

Aggregations

XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)47 AccountJid (com.xabber.android.data.entity.AccountJid)12 UserJid (com.xabber.android.data.entity.UserJid)11 DomainBareJid (org.jxmpp.jid.DomainBareJid)11 Jid (org.jxmpp.jid.Jid)9 Localpart (org.jxmpp.jid.parts.Localpart)7 Resourcepart (org.jxmpp.jid.parts.Resourcepart)7 ArrayList (java.util.ArrayList)6 Cursor (android.database.Cursor)5 HashMap (java.util.HashMap)5 SmackException (org.jivesoftware.smack.SmackException)4 NetworkException (com.xabber.android.data.NetworkException)3 Realm (io.realm.Realm)3 IOException (java.io.IOException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 BareJid (org.jxmpp.jid.BareJid)3 EntityBareJid (org.jxmpp.jid.EntityBareJid)3 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 AccountRealm (com.xabber.android.data.database.realm.AccountRealm)2 ContactRealm (com.xabber.android.data.database.realm.ContactRealm)2