Search in sources :

Example 26 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice 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 27 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class OmemoServiceTest method isStaleDeviceTest.

/**
 * Test correct functionality of isStale method.
 * @throws XmppStringprepException if the provided string is invalid.
 */
@Test
public void isStaleDeviceTest() throws XmppStringprepException {
    OmemoDevice user = new OmemoDevice(JidCreate.bareFrom("alice@wonderland.lit"), 123);
    OmemoDevice other = new OmemoDevice(JidCreate.bareFrom("bob@builder.tv"), 444);
    Date now = new Date();
    Date deleteMe = new Date(now.getTime() - ((DELETE_STALE + 1) * ONE_HOUR));
    // Devices one hour "older" than max ages are stale
    assertTrue(OmemoService.isStale(user, other, deleteMe, DELETE_STALE));
    // Own device is never stale, no matter how old
    assertFalse(OmemoService.isStale(user, user, deleteMe, DELETE_STALE));
    // Always return false if date is null.
    assertFalse(OmemoService.isStale(user, other, null, DELETE_STALE));
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) Date(java.util.Date) Test(org.junit.Test)

Example 28 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class OmemoServiceTest method removeOurDeviceTest.

@Test
public void removeOurDeviceTest() throws XmppStringprepException {
    OmemoDevice a = new OmemoDevice(JidCreate.bareFrom("a@b.c"), 123);
    OmemoDevice b = new OmemoDevice(JidCreate.bareFrom("a@b.c"), 124);
    HashSet<OmemoDevice> devices = new HashSet<>();
    devices.add(a);
    devices.add(b);
    assertTrue(devices.contains(a));
    assertTrue(devices.contains(b));
    OmemoService.removeOurDevice(a, devices);
    assertFalse(devices.contains(a));
    assertTrue(devices.contains(b));
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class OmemoClient method handleInput.

public void handleInput(String input) throws NotConnectedException, NotLoggedInException, InterruptedException, IOException {
    String[] com = input.split(" ", 3);
    switch(com[0]) {
        case "/omemo":
            if (com.length < 3) {
                print("Usage: /omemo <contact-jid> <message>");
                return;
            }
            BareJid recipient = JidCreate.bareFrom(com[1]);
            String body = com[2];
            MessageBuilder messageBuilder = connection.getStanzaFactory().buildMessageStanza();
            try {
                Message omemoMessage = omemoManager.encrypt(recipient, body).buildMessage(messageBuilder, recipient);
                connection.sendStanza(omemoMessage);
            } catch (UndecidedOmemoIdentityException e) {
                print("Undecided Identities!\n" + Arrays.toString(e.getUndecidedDevices().toArray()));
            } catch (CryptoFailedException | SmackException.NoResponseException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
            }
            break;
        case "/trust":
            print("Trust");
            if (com.length != 2) {
                print("Usage: /trust <contact-jid>");
            }
            BareJid contact = JidCreate.bareFrom(com[1]);
            HashMap<OmemoDevice, OmemoFingerprint> devices;
            try {
                devices = omemoManager.getActiveFingerprints(contact);
            } catch (CorruptedOmemoKeyException | CannotEstablishOmemoSessionException | SmackException.NoResponseException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
                return;
            }
            for (OmemoDevice d : devices.keySet()) {
                print("Trust (1) or distrust (2)?\n" + devices.get(d).blocksOf8Chars());
                if (Integer.parseInt(scanner.nextLine()) == 1) {
                    omemoManager.trustOmemoIdentity(d, devices.get(d));
                } else {
                    omemoManager.distrustOmemoIdentity(d, devices.get(d));
                }
            }
            print("Done.");
            break;
        case "/purge":
            try {
                omemoManager.purgeDeviceList();
                print("Purged.");
            } catch (XMPPException.XMPPErrorException | SmackException.NoResponseException | PubSubException.NotALeafNodeException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
            }
    }
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) OmemoMessage(org.jivesoftware.smackx.omemo.OmemoMessage) Message(org.jivesoftware.smack.packet.Message) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)

Aggregations

OmemoDevice (org.jivesoftware.smackx.omemo.internal.OmemoDevice)29 CannotEstablishOmemoSessionException (org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException)8 OmemoFingerprint (org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)8 CorruptedOmemoKeyException (org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException)6 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)6 Test (org.junit.Test)6 BareJid (org.jxmpp.jid.BareJid)6 EntityBareJid (org.jxmpp.jid.EntityBareJid)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashMap (java.util.HashMap)4 OmemoBundleElement (org.jivesoftware.smackx.omemo.element.OmemoBundleElement)4 OmemoElement (org.jivesoftware.smackx.omemo.element.OmemoElement)4 UndecidedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException)4 OmemoCachedDeviceList (org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 OmemoFingerprint (org.jivesoftware.smackx.omemo.OmemoFingerprint)3 NoIdentityKeyException (org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException)3 NoRawSessionException (org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException)3