Search in sources :

Example 31 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest in project Smack by igniterealtime.

the class UserTuneIntegrationTest method testNotification.

/**
 * Verifies that a notification is sent when a publication is received, assuming that notification filtering
 * has been adjusted to allow for the notification to be delivered.
 *
 * @throws Exception if the test fails
 */
@SmackIntegrationTest
public void testNotification() throws Exception {
    URI uri = new URI("http://www.yesworld.com/lyrics/Fragile.html#9");
    UserTuneElement.Builder builder = UserTuneElement.getBuilder();
    UserTuneElement data = builder.setArtist("Yes").setLength(686).setRating(8).setSource("Yessongs").setTitle("Heart of the Sunrise").setTrack("3").setUri(uri).build();
    IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
    final SimpleResultSyncPoint userTuneReceived = new SimpleResultSyncPoint();
    final PepEventListener<UserTuneElement> userTuneListener = (jid, userTune, id, message) -> {
        if (userTune.equals(data)) {
            userTuneReceived.signal();
        }
    };
    try {
        // Register ConTwo's interest in receiving user tune notifications, and wait for that interest to have been propagated.
        registerListenerAndWait(utm2, ServiceDiscoveryManager.getInstanceFor(conTwo), userTuneListener);
        // Publish the data.
        // for the purpose of this test, this needs not be blocking/use publishAndWait();
        utm1.publishUserTune(data);
        // Wait for the data to be received.
        Object result = userTuneReceived.waitForResult(timeout);
        // Explicitly assert the success case.
        Assertions.assertNotNull(result, "Expected to receive a PEP notification, but did not.");
    } finally {
        unregisterListener(utm2, userTuneListener);
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) SmackIntegrationTestEnvironment(org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment) AfterClass(org.igniterealtime.smack.inttest.annotations.AfterClass) EntityCapabilitiesChangedListener(org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) NotLoggedInException(org.jivesoftware.smack.SmackException.NotLoggedInException) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager) TimeoutException(java.util.concurrent.TimeoutException) UserTuneElement(org.jivesoftware.smackx.usertune.element.UserTuneElement) PepEventListener(org.jivesoftware.smackx.pep.PepEventListener) Assertions(org.junit.jupiter.api.Assertions) IntegrationTestRosterUtil(org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil) URI(java.net.URI) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) XMPPException(org.jivesoftware.smack.XMPPException) UserTuneElement(org.jivesoftware.smackx.usertune.element.UserTuneElement) URI(java.net.URI) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 32 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest in project Smack by igniterealtime.

the class SoftwareInfoIntegrationTest method test.

@SmackIntegrationTest
public void test() throws Exception {
    SoftwareInfoForm softwareInfoSent = createSoftwareInfoForm();
    performActionAndWaitForPresence(conTwo, conOne, new ThrowingRunnable() {

        @Override
        public void runOrThrow() throws Exception {
            sim1.publishSoftwareInformationForm(softwareInfoSent);
        }
    });
    SoftwareInfoForm softwareInfoFormReceived = sim2.fromJid(conOne.getUser());
    assertEquals(softwareInfoSent, softwareInfoFormReceived);
}
Also used : SoftwareInfoForm(org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException) ThrowingRunnable(org.jivesoftware.smack.util.Async.ThrowingRunnable) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 33 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest in project Smack by igniterealtime.

the class FormTest method testFilloutForm.

/**
 * 1. Create a form to fill out and send it to the other user
 * 2. Retrieve the form to fill out, complete it and return it to the requestor
 * 3. Retrieve the completed form and check that everything is OK
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws NotConnectedException if the XMPP connection is not connected.
 */
@SuppressWarnings("deprecation")
@SmackIntegrationTest
public void testFilloutForm() throws NotConnectedException, InterruptedException {
    DataForm.Builder formToSend = DataForm.builder(DataForm.Type.form);
    formToSend.setInstructions("Fill out this form to report your case.\nThe case will be created automatically.");
    formToSend.setTitle("Case configurations");
    formToSend.setFormType("https://igniterealtime.org/projects/smack/sinttest/form-test/1");
    // Add a hidden variable
    FormField field = FormField.hiddenBuilder("hidden_var").setValue("Some value for the hidden variable").build();
    formToSend.addField(field);
    // Add a fixed variable
    field = FormField.fixedBuilder().setValue("Section 1: Case description").build();
    formToSend.addField(field);
    // Add a text-single variable
    field = FormField.textSingleBuilder("name").setLabel("Enter a name for the case").build();
    formToSend.addField(field);
    // Add a text-multi variable
    field = FormField.textMultiBuilder("description").setLabel("Enter a description").build();
    formToSend.addField(field);
    // Add a boolean variable
    field = FormField.booleanBuilder("time").setLabel("Is this your first case?").build();
    formToSend.addField(field);
    // Add a text variable where an int value is expected
    field = FormField.textSingleBuilder("age").setLabel("How old are you?").build();
    formToSend.addField(field);
    // Create the chats between the two participants
    org.jivesoftware.smack.chat.Chat chat = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne).createChat(conTwo.getUser(), null);
    StanzaCollector collector = conOne.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    StanzaCollector collector2 = conTwo.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    Message msg = StanzaBuilder.buildMessage().setBody("To enter a case please fill out this form and send it back to me").addExtension(formToSend.build()).build();
    try {
        // Send the message with the form to fill out
        chat.sendMessage(msg);
        // Get the message with the form to fill out
        Message msg2 = collector2.nextResult();
        assertNotNull(msg2, "Message not found");
        // Retrieve the form to fill out
        Form formToRespond = Form.from(msg2);
        assertNotNull(formToRespond);
        assertNotNull(formToRespond.getField("name"));
        assertNotNull(formToRespond.getField("description"));
        // Obtain the form to send with the replies
        final FillableForm completedForm = formToRespond.getFillableForm();
        assertNotNull(completedForm.getField("hidden_var"));
        // Check that a field of type String does not accept booleans
        assertThrows(IllegalArgumentException.class, () -> completedForm.setAnswer("name", true));
        completedForm.setAnswer("name", "Credit card number invalid");
        completedForm.setAnswer("description", "The ATM says that my credit card number is invalid. What's going on?");
        completedForm.setAnswer("time", true);
        completedForm.setAnswer("age", 20);
        // Create a new message to send with the completed form
        msg2 = StanzaBuilder.buildMessage().to(conOne.getUser().asBareJid()).setThread(msg.getThread()).ofType(Message.Type.chat).setBody("To enter a case please fill out this form and send it back to me").addExtension(completedForm.getDataFormToSubmit()).build();
        // Send the message with the completed form
        conTwo.sendStanza(msg2);
        // Get the message with the completed form
        Message msg3 = collector.nextResult();
        assertNotNull(msg3, "Message not found");
        // Retrieve the completed form
        final DataForm completedForm2 = DataForm.from(msg3);
        assertNotNull(completedForm2);
        assertNotNull(completedForm2.getField("name"));
        assertNotNull(completedForm2.getField("description"));
        assertEquals(completedForm2.getField("name").getValues().get(0).toString(), "Credit card number invalid");
        assertNotNull(completedForm2.getField("time"));
        assertNotNull(completedForm2.getField("age"));
        assertEquals("20", completedForm2.getField("age").getValues().get(0).toString(), "The age is bad");
    } finally {
        collector.cancel();
        collector2.cancel();
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Form(org.jivesoftware.smackx.xdata.form.Form) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FillableForm(org.jivesoftware.smackx.xdata.form.FillableForm) StanzaCollector(org.jivesoftware.smack.StanzaCollector) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 34 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest in project Smack by igniterealtime.

the class SessionRenegotiationIntegrationTest method sessionRenegotiationTest.

@SuppressWarnings("SynchronizeOnNonFinalField")
@SmackIntegrationTest
public void sessionRenegotiationTest() throws Exception {
    boolean prevRepairProperty = OmemoConfiguration.getRepairBrokenSessionsWithPreKeyMessages();
    OmemoConfiguration.setRepairBrokenSessionsWithPrekeyMessages(true);
    boolean prevCompleteSessionProperty = OmemoConfiguration.getCompleteSessionWithEmptyMessage();
    OmemoConfiguration.setCompleteSessionWithEmptyMessage(false);
    // send PreKeyMessage -> Success
    final String body1 = "P = NP is true for all N,P from the set of complex numbers, where P is equal to 0";
    AbstractOmemoMessageListener.PreKeyMessageListener listener1 = new AbstractOmemoMessageListener.PreKeyMessageListener(body1);
    OmemoMessage.Sent e1 = alice.encrypt(bob.getOwnJid(), body1);
    bob.addOmemoMessageListener(listener1);
    XMPPConnection alicesConnection = alice.getConnection();
    MessageBuilder messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
    alicesConnection.sendStanza(e1.buildMessage(messageBuilder, bob.getOwnJid()));
    listener1.getSyncPoint().waitForResult(10 * 1000);
    bob.removeOmemoMessageListener(listener1);
    // Remove the session on Bobs side.
    synchronized (bob) {
        bob.getOmemoService().getOmemoStoreBackend().removeRawSession(bob.getOwnDevice(), alice.getOwnDevice());
    }
    // Send normal message -> fail, bob repairs session with preKeyMessage
    final String body2 = "P = NP is also true for all N,P from the set of complex numbers, where N is equal to 1.";
    AbstractOmemoMessageListener.PreKeyKeyTransportListener listener2 = new AbstractOmemoMessageListener.PreKeyKeyTransportListener();
    OmemoMessage.Sent e2 = alice.encrypt(bob.getOwnJid(), body2);
    alice.addOmemoMessageListener(listener2);
    messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
    alicesConnection.sendStanza(e2.buildMessage(messageBuilder, bob.getOwnJid()));
    listener2.getSyncPoint().waitForResult(10 * 1000);
    alice.removeOmemoMessageListener(listener2);
    // Send normal message -> success
    final String body3 = "P = NP would be a disaster for the world of cryptography.";
    AbstractOmemoMessageListener.MessageListener listener3 = new AbstractOmemoMessageListener.MessageListener(body3);
    OmemoMessage.Sent e3 = alice.encrypt(bob.getOwnJid(), body3);
    bob.addOmemoMessageListener(listener3);
    messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
    alicesConnection.sendStanza(e3.buildMessage(messageBuilder, bob.getOwnJid()));
    listener3.getSyncPoint().waitForResult(10 * 1000);
    bob.removeOmemoMessageListener(listener3);
    OmemoConfiguration.setRepairBrokenSessionsWithPrekeyMessages(prevRepairProperty);
    OmemoConfiguration.setCompleteSessionWithEmptyMessage(prevCompleteSessionProperty);
}
Also used : XMPPConnection(org.jivesoftware.smack.XMPPConnection) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 35 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest in project Smack by igniterealtime.

the class PingIntegrationTest method pingAsync.

@SmackIntegrationTest
public void pingAsync() throws InterruptedException, ExecutionException {
    List<Future<Boolean>> pongFutures = Collections.synchronizedList(new ArrayList<Future<Boolean>>());
    Runnable[] pinger = new Runnable[3];
    pinger[0] = new Pinger(conOne, pongFutures, conTwo.getUser(), conThree.getUser());
    pinger[1] = new Pinger(conTwo, pongFutures, conOne.getUser(), conThree.getUser());
    pinger[2] = new Pinger(conThree, pongFutures, conOne.getUser(), conTwo.getUser());
    ExecutorService executorService = Executors.newFixedThreadPool(pinger.length);
    for (Runnable runnable : pinger) {
        executorService.execute(runnable);
    }
    executorService.shutdown();
    executorService.awaitTermination(1, TimeUnit.MINUTES);
    for (Future<Boolean> pongFuture : pongFutures) {
        assertTrue(pongFuture.get());
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Aggregations

SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)66 EntityBareJid (org.jxmpp.jid.EntityBareJid)30 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)25 XMPPException (org.jivesoftware.smack.XMPPException)23 SmackException (org.jivesoftware.smack.SmackException)20 Resourcepart (org.jxmpp.jid.parts.Resourcepart)20 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)17 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)17 ResultSyncPoint (org.igniterealtime.smack.inttest.util.ResultSyncPoint)16 EntityFullJid (org.jxmpp.jid.EntityFullJid)12 Message (org.jivesoftware.smack.packet.Message)9 TimeoutException (java.util.concurrent.TimeoutException)8 Presence (org.jivesoftware.smack.packet.Presence)7 SmackIntegrationTestEnvironment (org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment)6 AfterClass (org.igniterealtime.smack.inttest.annotations.AfterClass)6 IntegrationTestRosterUtil (org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil)6 EntityCapabilitiesChangedListener (org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener)6 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)6 PepEventListener (org.jivesoftware.smackx.pep.PepEventListener)6 Assertions (org.junit.jupiter.api.Assertions)6