Search in sources :

Example 36 with SmackIntegrationTest

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

the class OXSecretKeyBackupIntegrationTest method test.

@SmackIntegrationTest
public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, SmackException.FeatureNotSupportedException, MissingUserIdOnKeyException, NoBackupFoundException, InvalidBackupCodeException, PGPException, MissingOpenPgpKeyException {
    OpenPgpStore beforeStore = new FileBasedOpenPgpStore(beforePath);
    beforeStore.setKeyRingProtector(new UnprotectedKeysProtector());
    PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(beforeStore);
    OpenPgpManager openPgpManager = OpenPgpManager.getInstanceFor(aliceConnection);
    openPgpManager.setOpenPgpProvider(beforeProvider);
    OpenPgpSelf self = openPgpManager.getOpenPgpSelf();
    assertNull(self.getSigningKeyFingerprint());
    OpenPgpV4Fingerprint keyFingerprint = openPgpManager.generateAndImportKeyPair(alice);
    assertEquals(keyFingerprint, self.getSigningKeyFingerprint());
    assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId()));
    PGPSecretKeyRing beforeSec = beforeStore.getSecretKeyRing(alice, keyFingerprint);
    assertNotNull(beforeSec);
    PGPPublicKeyRing beforePub = beforeStore.getPublicKeyRing(alice, keyFingerprint);
    assertNotNull(beforePub);
    OpenPgpSecretKeyBackupPassphrase backupPassphrase = openPgpManager.backupSecretKeyToServer(availableSecretKeys -> availableSecretKeys);
    FileBasedOpenPgpStore afterStore = new FileBasedOpenPgpStore(afterPath);
    afterStore.setKeyRingProtector(new UnprotectedKeysProtector());
    PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(afterStore);
    openPgpManager.setOpenPgpProvider(afterProvider);
    OpenPgpV4Fingerprint fingerprint = openPgpManager.restoreSecretKeyServerBackup(() -> backupPassphrase);
    assertEquals(keyFingerprint, fingerprint);
    assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId()));
    assertEquals(keyFingerprint, self.getSigningKeyFingerprint());
    PGPSecretKeyRing afterSec = afterStore.getSecretKeyRing(alice, keyFingerprint);
    assertNotNull(afterSec);
    assertArrayEquals(beforeSec.getEncoded(), afterSec.getEncoded());
    PGPPublicKeyRing afterPub = afterStore.getPublicKeyRing(alice, keyFingerprint);
    assertNotNull(afterPub);
    assertArrayEquals(beforePub.getEncoded(), afterPub.getEncoded());
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) UnprotectedKeysProtector(org.pgpainless.key.protection.UnprotectedKeysProtector) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) OpenPgpStore(org.jivesoftware.smackx.ox.store.definition.OpenPgpStore) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) PainlessOpenPgpProvider(org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 37 with SmackIntegrationTest

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

the class OXInstantMessagingIntegrationTest method basicInstantMessagingTest.

@SmackIntegrationTest
public void basicInstantMessagingTest() throws Exception {
    final SimpleResultSyncPoint bobReceivedMessage = new SimpleResultSyncPoint();
    final String body = "Writing integration tests is an annoying task, but it has to be done, so lets do it!!!";
    FileBasedOpenPgpStore aliceStore = new FileBasedOpenPgpStore(aliceStorePath);
    aliceStore.setKeyRingProtector(new UnprotectedKeysProtector());
    FileBasedOpenPgpStore bobStore = new FileBasedOpenPgpStore(bobStorePath);
    bobStore.setKeyRingProtector(new UnprotectedKeysProtector());
    PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore);
    PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore);
    aliceOpenPgp = OpenPgpManager.getInstanceFor(aliceConnection);
    bobOpenPgp = OpenPgpManager.getInstanceFor(bobConnection);
    OXInstantMessagingManager aliceInstantMessaging = OXInstantMessagingManager.getInstanceFor(aliceConnection);
    OXInstantMessagingManager bobInstantMessaging = OXInstantMessagingManager.getInstanceFor(bobConnection);
    bobInstantMessaging.addOxMessageListener(new OxMessageListener() {

        @Override
        public void newIncomingOxMessage(OpenPgpContact contact, Message originalMessage, SigncryptElement decryptedPayload, OpenPgpMetadata metadata) {
            if (((Message.Body) decryptedPayload.getExtension(Message.Body.NAMESPACE)).getMessage().equals(body)) {
                bobReceivedMessage.signal();
            } else {
                bobReceivedMessage.signalFailure();
            }
        }
    });
    aliceOpenPgp.setOpenPgpProvider(aliceProvider);
    bobOpenPgp.setOpenPgpProvider(bobProvider);
    aliceFingerprint = aliceOpenPgp.generateAndImportKeyPair(alice);
    bobFingerprint = bobOpenPgp.generateAndImportKeyPair(bob);
    aliceOpenPgp.announceSupportAndPublish();
    bobOpenPgp.announceSupportAndPublish();
    OpenPgpContact bobForAlice = aliceOpenPgp.getOpenPgpContact(bob.asEntityBareJidIfPossible());
    OpenPgpContact aliceForBob = bobOpenPgp.getOpenPgpContact(alice.asEntityBareJidIfPossible());
    bobForAlice.updateKeys(aliceConnection);
    assertFalse(bobForAlice.isTrusted(bobFingerprint));
    assertFalse(aliceForBob.isTrusted(aliceFingerprint));
    bobForAlice.trust(bobFingerprint);
    aliceForBob.trust(aliceFingerprint);
    assertTrue(bobForAlice.isTrusted(bobFingerprint));
    assertTrue(aliceForBob.isTrusted(aliceFingerprint));
    aliceInstantMessaging.sendOxMessage(bobForAlice, body);
    bobReceivedMessage.waitForResult(timeout);
}
Also used : SigncryptElement(org.jivesoftware.smackx.ox.element.SigncryptElement) Message(org.jivesoftware.smack.packet.Message) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) UnprotectedKeysProtector(org.pgpainless.key.protection.UnprotectedKeysProtector) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) PainlessOpenPgpProvider(org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider) OpenPgpContact(org.jivesoftware.smackx.ox.OpenPgpContact) OpenPgpMetadata(org.pgpainless.decryption_verification.OpenPgpMetadata) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 38 with SmackIntegrationTest

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

the class MultiUserChatEntityIntegrationTest method mucTestForDiscoveringRooms.

/**
 * Asserts that a MUC Service lists its public rooms.
 *
 * <p>From XEP-0045 § 6.3:</p>
 * <blockquote>
 * The service discovery items ("disco#items") protocol enables an entity to query a service for a list of
 * associated items, which in the case of a chat service would consist of the specific chat rooms hosted by the
 * service. The service SHOULD return a full list of the public rooms it hosts (i.e., not return any rooms that
 * are hidden).
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucTestForDiscoveringRooms() throws Exception {
    EntityBareJid mucAddressPublic = getRandomRoom("smack-inttest-publicroom");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddressPublic);
    EntityBareJid mucAddressHidden = getRandomRoom("smack-inttest-hiddenroom");
    MultiUserChat mucAsSeenByTwo = mucManagerTwo.getMultiUserChat(mucAddressHidden);
    createMuc(mucAsSeenByOne, Resourcepart.from("one-" + randomString));
    Map<EntityBareJid, HostedRoom> rooms;
    try {
        createHiddenMuc(mucAsSeenByTwo, Resourcepart.from("two-" + randomString));
        rooms = mucManagerThree.getRoomsHostedBy(mucService);
    } finally {
        tryDestroy(mucAsSeenByOne);
        tryDestroy(mucAsSeenByTwo);
    }
    assertTrue(rooms.containsKey(mucAddressPublic));
    assertFalse(rooms.containsKey(mucAddressHidden));
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 39 with SmackIntegrationTest

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

the class MultiUserChatEntityIntegrationTest method mucTestForDiscoveringRoomInfo.

/**
 * Asserts that a MUC Service returns disco info for a room.
 *
 * <p>From XEP-0045 § 6.4:</p>
 * <blockquote>
 * Using the disco#info protocol, an entity may also query a specific chat room for more detailed information
 * about the room....The room MUST return its identity and SHOULD return the features it supports
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucTestForDiscoveringRoomInfo() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest-discoinfo");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    createMuc(mucAsSeenByOne, Resourcepart.from("one-" + randomString));
    DiscoverInfo discoInfo;
    try {
        // Use SDM because mucManagerOne.getRoomInfo(mucAddress) might not use Disco
        discoInfo = ServiceDiscoveryManager.getInstanceFor(conOne).discoverInfo(mucAddress);
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
    assertTrue(discoInfo.getIdentities().size() > 0);
    assertTrue(discoInfo.getFeatures().size() > 0);
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) EntityBareJid(org.jxmpp.jid.EntityBareJid) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 40 with SmackIntegrationTest

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

the class MultiUserChatEntityIntegrationTest method mucTestForRejectingDiscoOnRoomOccupantByNonOccupant.

/**
 * Asserts that a non-occupant receives a Bad Request error when attempting to query an occupant by their
 * occupant JID.
 *
 * <p>From XEP-0045 § 6.6:</p>
 * <blockquote>
 * If a non-occupant attempts to send a disco request to an address of the form &lt;room@service/nick&gt;, a MUC service
 * MUST return a &lt;bad-request/&gt; error
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucTestForRejectingDiscoOnRoomOccupantByNonOccupant() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest-discoitems");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    final Resourcepart nicknameOne = Resourcepart.from("one-" + randomString);
    createMuc(mucAsSeenByOne, nicknameOne);
    final EntityFullJid mucAsSeenByOneUserJid = mucAsSeenByOne.getMyRoomJid();
    // be non-null after we created and joined the room. But it can not hurt to explicitly test for it either.
    if (mucAsSeenByOneUserJid == null) {
        throw new AssertionError();
    }
    XMPPException.XMPPErrorException xe;
    try {
        xe = assertThrows(XMPPException.XMPPErrorException.class, () -> ServiceDiscoveryManager.getInstanceFor(conTwo).discoverItems(mucAsSeenByOneUserJid));
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
    final StanzaError.Condition expectedCondition;
    switch(sinttestConfiguration.compatibilityMode) {
        default:
            expectedCondition = StanzaError.Condition.bad_request;
            break;
        case ejabberd:
            expectedCondition = StanzaError.Condition.not_acceptable;
            break;
    }
    assertEquals(xe.getStanzaError().getCondition(), expectedCondition);
}
Also used : EntityFullJid(org.jxmpp.jid.EntityFullJid) XMPPException(org.jivesoftware.smack.XMPPException) EntityBareJid(org.jxmpp.jid.EntityBareJid) Resourcepart(org.jxmpp.jid.parts.Resourcepart) StanzaError(org.jivesoftware.smack.packet.StanzaError) 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