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));
}
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));
}
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));
}
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);
}
}
}
Aggregations