SessionRenegotiationIntegrationTest.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo;

  18. import org.jivesoftware.smack.SmackException;
  19. import org.jivesoftware.smack.XMPPConnection;
  20. import org.jivesoftware.smack.XMPPException;
  21. import org.jivesoftware.smack.packet.MessageBuilder;

  22. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  23. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  24. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  25. import org.igniterealtime.smack.inttest.annotations.SpecificationReference;

  26. @SpecificationReference(document = "XEP-0384", version = "0.3.0")
  27. public class SessionRenegotiationIntegrationTest extends AbstractTwoUsersOmemoIntegrationTest {

  28.     public SessionRenegotiationIntegrationTest(SmackIntegrationTestEnvironment environment)
  29.             throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
  30.             SmackException.NoResponseException, TestNotPossibleException {
  31.         super(environment);
  32.     }

  33.     @SuppressWarnings("SynchronizeOnNonFinalField")
  34.     @SmackIntegrationTest
  35.     public void sessionRenegotiationTest() throws Exception {

  36.         boolean prevRepairProperty = OmemoConfiguration.getRepairBrokenSessionsWithPreKeyMessages();
  37.         OmemoConfiguration.setRepairBrokenSessionsWithPrekeyMessages(true);
  38.         boolean prevCompleteSessionProperty = OmemoConfiguration.getCompleteSessionWithEmptyMessage();
  39.         OmemoConfiguration.setCompleteSessionWithEmptyMessage(false);

  40.         // send PreKeyMessage -> Success
  41.         final String body1 = "P = NP is true for all N,P from the set of complex numbers, where P is equal to 0";
  42.         AbstractOmemoMessageListener.PreKeyMessageListener listener1 =
  43.                 new AbstractOmemoMessageListener.PreKeyMessageListener(body1);
  44.         OmemoMessage.Sent e1 = alice.encrypt(bob.getOwnJid(), body1);
  45.         bob.addOmemoMessageListener(listener1);

  46.         XMPPConnection alicesConnection = alice.getConnection();
  47.         MessageBuilder messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
  48.         alicesConnection.sendStanza(e1.buildMessage(messageBuilder, bob.getOwnJid()));
  49.         listener1.getSyncPoint().waitForResult(10 * 1000);
  50.         bob.removeOmemoMessageListener(listener1);

  51.         // Remove the session on Bobs side.
  52.         synchronized (bob) {
  53.             bob.getOmemoService().getOmemoStoreBackend().removeRawSession(bob.getOwnDevice(), alice.getOwnDevice());
  54.         }

  55.         // Send normal message -> fail, bob repairs session with preKeyMessage
  56.         final String body2 = "P = NP is also true for all N,P from the set of complex numbers, where N is equal to 1.";
  57.         AbstractOmemoMessageListener.PreKeyKeyTransportListener listener2 =
  58.                 new AbstractOmemoMessageListener.PreKeyKeyTransportListener();
  59.         OmemoMessage.Sent e2 = alice.encrypt(bob.getOwnJid(), body2);
  60.         alice.addOmemoMessageListener(listener2);

  61.         messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
  62.         alicesConnection.sendStanza(e2.buildMessage(messageBuilder, bob.getOwnJid()));
  63.         listener2.getSyncPoint().waitForResult(10 * 1000);
  64.         alice.removeOmemoMessageListener(listener2);

  65.         // Send normal message -> success
  66.         final String body3 = "P = NP would be a disaster for the world of cryptography.";
  67.         AbstractOmemoMessageListener.MessageListener listener3 = new AbstractOmemoMessageListener.MessageListener(body3);
  68.         OmemoMessage.Sent e3 = alice.encrypt(bob.getOwnJid(), body3);
  69.         bob.addOmemoMessageListener(listener3);

  70.         messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
  71.         alicesConnection.sendStanza(e3.buildMessage(messageBuilder, bob.getOwnJid()));
  72.         listener3.getSyncPoint().waitForResult(10 * 1000);
  73.         bob.removeOmemoMessageListener(listener3);

  74.         OmemoConfiguration.setRepairBrokenSessionsWithPrekeyMessages(prevRepairProperty);
  75.         OmemoConfiguration.setCompleteSessionWithEmptyMessage(prevCompleteSessionProperty);
  76.     }
  77. }