1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

Replace hacks with synchronized call

This commit is contained in:
Paul Schaub 2018-01-10 16:40:56 +01:00
parent ebf7969954
commit 0a681f7353
2 changed files with 12 additions and 41 deletions

View file

@ -50,8 +50,6 @@ public class MessageEncryptionIntegrationTest extends AbstractTwoUsersOmemoInteg
* Bob publishes bundle B2 * Bob publishes bundle B2
* Alice still has A1 * Alice still has A1
* *
* (Alice sends second message to bob to avoid race condition in the code (just for this example))
*
* Bob responds to Alice (normal message) * Bob responds to Alice (normal message)
* Alice still has A1 * Alice still has A1
* Bob still has B2 * Bob still has B2
@ -73,18 +71,12 @@ public class MessageEncryptionIntegrationTest extends AbstractTwoUsersOmemoInteg
listener1.getSyncPoint().waitForResult(10 * 1000); listener1.getSyncPoint().waitForResult(10 * 1000);
bob.removeOmemoMessageListener(listener1); bob.removeOmemoMessageListener(listener1);
// Message A -> B
final String body2 = "This message is sent to mitigate a race condition in the test";
AbstractOmemoMessageListener.MessageListener listener2 =
new AbstractOmemoMessageListener.MessageListener(body2);
bob.addOmemoMessageListener(listener2);
OmemoMessage.Sent e2 = alice.encrypt(bob.getOwnJid(), body2);
alice.getConnection().sendStanza(e2.asMessage(bob.getOwnJid()));
listener2.getSyncPoint().waitForResult(10 * 1000);
bob.removeOmemoMessageListener(listener2);
OmemoBundleElement a1_ = alice.getOmemoService().getOmemoStoreBackend().packOmemoBundle(alice.getOwnDevice()); OmemoBundleElement a1_ = alice.getOmemoService().getOmemoStoreBackend().packOmemoBundle(alice.getOwnDevice());
OmemoBundleElement b2 = bob.getOmemoService().getOmemoStoreBackend().packOmemoBundle(bob.getOwnDevice()); OmemoBundleElement b2;
synchronized (bob.LOCK) { // Circumvent race condition where bundle gets replenished after getting stored in b2
b2 = bob.getOmemoService().getOmemoStoreBackend().packOmemoBundle(bob.getOwnDevice());
}
assertEquals("Alice sent bob a preKeyMessage, so her bundle MUST still be the same.", a1, a1_); assertEquals("Alice sent bob a preKeyMessage, so her bundle MUST still be the same.", a1, a1_);
assertNotEquals("Bob just received a preKeyMessage from alice, so his bundle must have changed.", b1, b2); assertNotEquals("Bob just received a preKeyMessage from alice, so his bundle must have changed.", b1, b2);

View file

@ -16,8 +16,6 @@
*/ */
package org.jivesoftware.smackx.omemo; package org.jivesoftware.smackx.omemo;
import java.util.logging.Level;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
@ -42,57 +40,38 @@ public class SessionRenegotiationIntegrationTest extends AbstractTwoUsersOmemoIn
"set to 'true'."); "set to 'true'.");
} }
// 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"; 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 = AbstractOmemoMessageListener.PreKeyMessageListener listener1 =
new AbstractOmemoMessageListener.PreKeyMessageListener(body1); new AbstractOmemoMessageListener.PreKeyMessageListener(body1);
OmemoMessage.Sent e1 = alice.encrypt(bob.getOwnJid(), body1); OmemoMessage.Sent e1 = alice.encrypt(bob.getOwnJid(), body1);
bob.addOmemoMessageListener(listener1); bob.addOmemoMessageListener(listener1);
alice.getConnection().sendStanza(e1.asMessage(bob.getOwnJid())); alice.getConnection().sendStanza(e1.asMessage(bob.getOwnJid()));
LOGGER.log(Level.INFO, "Message 1 sent");
listener1.getSyncPoint().waitForResult(10 * 1000); listener1.getSyncPoint().waitForResult(10 * 1000);
bob.removeOmemoMessageListener(listener1); bob.removeOmemoMessageListener(listener1);
LOGGER.log(Level.INFO, "Message 1 received"); // Remove the session on Bobs side.
synchronized (bob.LOCK) {
final String body2 = "This message is sent to circumvent a race condition in the test."; bob.getOmemoService().getOmemoStoreBackend().removeRawSession(bob.getOwnDevice(), alice.getOwnDevice());
AbstractOmemoMessageListener.MessageListener listener2 = new AbstractOmemoMessageListener.MessageListener(body2); }
OmemoMessage.Sent e2 = alice.encrypt(bob.getOwnJid(), body2);
bob.addOmemoMessageListener(listener2);
alice.getConnection().sendStanza(e2.asMessage(bob.getOwnJid()));
LOGGER.log(Level.INFO, "Message 2 sent");
listener2.getSyncPoint().waitForResult(10 * 1000);
bob.removeOmemoMessageListener(listener2);
LOGGER.log(Level.INFO, "Message 2 received");
// Remove bobs session with alice.
LOGGER.log(Level.INFO, "Delete session");
bob.getOmemoService().getOmemoStoreBackend().removeRawSession(bob.getOwnDevice(), alice.getOwnDevice());
// Send normal message -> fail, bob repairs session with preKeyMessage
final String body3 = "P = NP is also true for all N,P from the set of complex numbers, where N is equal to 1."; final String body3 = "P = NP is also true for all N,P from the set of complex numbers, where N is equal to 1.";
AbstractOmemoMessageListener.PreKeyKeyTransportListener listener3 = AbstractOmemoMessageListener.PreKeyKeyTransportListener listener3 =
new AbstractOmemoMessageListener.PreKeyKeyTransportListener(); new AbstractOmemoMessageListener.PreKeyKeyTransportListener();
OmemoMessage.Sent e3 = alice.encrypt(bob.getOwnJid(), body3); OmemoMessage.Sent e3 = alice.encrypt(bob.getOwnJid(), body3);
alice.addOmemoMessageListener(listener3); alice.addOmemoMessageListener(listener3);
alice.getConnection().sendStanza(e3.asMessage(bob.getOwnJid())); alice.getConnection().sendStanza(e3.asMessage(bob.getOwnJid()));
LOGGER.log(Level.INFO, "Message 3 sent");
listener3.getSyncPoint().waitForResult(10 * 1000); listener3.getSyncPoint().waitForResult(10 * 1000);
alice.removeOmemoMessageListener(listener3); alice.removeOmemoMessageListener(listener3);
LOGGER.log(Level.INFO, "Message 3 received"); // Send normal message -> success
final String body4 = "P = NP would be a disaster for the world of cryptography."; final String body4 = "P = NP would be a disaster for the world of cryptography.";
AbstractOmemoMessageListener.MessageListener listener4 = new AbstractOmemoMessageListener.MessageListener(body4); AbstractOmemoMessageListener.MessageListener listener4 = new AbstractOmemoMessageListener.MessageListener(body4);
LOGGER.log(Level.INFO, "Attempt to encrypt message 4");
OmemoMessage.Sent e4 = alice.encrypt(bob.getOwnJid(), body4); OmemoMessage.Sent e4 = alice.encrypt(bob.getOwnJid(), body4);
LOGGER.log(Level.INFO, "Message 4 encrypted");
bob.addOmemoMessageListener(listener4); bob.addOmemoMessageListener(listener4);
alice.getConnection().sendStanza(e4.asMessage(bob.getOwnJid())); alice.getConnection().sendStanza(e4.asMessage(bob.getOwnJid()));
LOGGER.log(Level.INFO, "Message 4 sent");
listener4.getSyncPoint().waitForResult(10 * 1000); listener4.getSyncPoint().waitForResult(10 * 1000);
bob.removeOmemoMessageListener(listener4); bob.removeOmemoMessageListener(listener4);
LOGGER.log(Level.INFO, "Message 4 received");
} }
} }