MessageEncryptionIntegrationTest.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 static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotEquals;

  20. import org.jivesoftware.smack.SmackException;
  21. import org.jivesoftware.smack.XMPPConnection;
  22. import org.jivesoftware.smack.XMPPException;
  23. import org.jivesoftware.smack.packet.MessageBuilder;

  24. import org.jivesoftware.smackx.omemo.element.OmemoBundleElement;

  25. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  26. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  27. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  28. import org.igniterealtime.smack.inttest.annotations.SpecificationReference;

  29. /**
  30.  * Simple OMEMO message encryption integration test.
  31.  * During this test Alice sends an encrypted message to Bob. Bob decrypts it and sends a response to Alice.
  32.  * It is checked whether the messages can be decrypted, and if used up pre-keys result in renewed bundles.
  33.  */
  34. @SpecificationReference(document = "XEP-0384", version = "0.3.0")
  35. public class MessageEncryptionIntegrationTest extends AbstractTwoUsersOmemoIntegrationTest {

  36.     public MessageEncryptionIntegrationTest(SmackIntegrationTestEnvironment environment)
  37.             throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
  38.             SmackException.NoResponseException, TestNotPossibleException {
  39.         super(environment);
  40.     }

  41.     /**
  42.      * This test checks whether the following actions are performed.
  43.      *
  44.      * Alice publishes bundle A1
  45.      * Bob publishes bundle B1
  46.      *
  47.      * Alice sends message to Bob (preKeyMessage)
  48.      * Bob publishes bundle B2
  49.      * Alice still has A1
  50.      *
  51.      * Bob responds to Alice (normal message)
  52.      * Alice still has A1
  53.      * Bob still has B2
  54.      * @throws Exception if an exception occurs.
  55.      */
  56.     @SuppressWarnings("SynchronizeOnNonFinalField")
  57.     @SmackIntegrationTest
  58.     public void messageTest() throws Exception {
  59.         OmemoBundleElement a1 = alice.getOmemoService().getOmemoStoreBackend().packOmemoBundle(alice.getOwnDevice());
  60.         OmemoBundleElement b1 = bob.getOmemoService().getOmemoStoreBackend().packOmemoBundle(bob.getOwnDevice());

  61.         // Alice sends message(s) to bob
  62.         // PreKeyMessage A -> B
  63.         final String body1 = "One is greater than zero (for small values of zero).";
  64.         AbstractOmemoMessageListener.PreKeyMessageListener listener1 =
  65.                 new AbstractOmemoMessageListener.PreKeyMessageListener(body1);
  66.         bob.addOmemoMessageListener(listener1);
  67.         OmemoMessage.Sent e1 = alice.encrypt(bob.getOwnJid(), body1);

  68.         XMPPConnection alicesConnection = alice.getConnection();
  69.         MessageBuilder messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
  70.         alicesConnection.sendStanza(e1.buildMessage(messageBuilder, bob.getOwnJid()));
  71.         listener1.getSyncPoint().waitForResult(10 * 1000);
  72.         bob.removeOmemoMessageListener(listener1);

  73.         OmemoBundleElement a1_ = alice.getOmemoService().getOmemoStoreBackend().packOmemoBundle(alice.getOwnDevice());
  74.         OmemoBundleElement b2;

  75.         synchronized (bob) { // Circumvent race condition where bundle gets replenished after getting stored in b2
  76.             b2 = bob.getOmemoService().getOmemoStoreBackend().packOmemoBundle(bob.getOwnDevice());
  77.         }

  78.         assertEquals(a1, a1_, "Alice sent bob a preKeyMessage, so her bundle MUST still be the same.");
  79.         assertNotEquals(b1, b2, "Bob just received a preKeyMessage from alice, so his bundle must have changed.");

  80.         // Message B -> A
  81.         final String body3 = "The german words for 'leek' and 'wimp' are the same.";
  82.         AbstractOmemoMessageListener.MessageListener listener3 =
  83.                 new AbstractOmemoMessageListener.MessageListener(body3);
  84.         alice.addOmemoMessageListener(listener3);
  85.         OmemoMessage.Sent e3 = bob.encrypt(alice.getOwnJid(), body3);
  86.         XMPPConnection bobsConnection = bob.getConnection();
  87.         messageBuilder = bobsConnection.getStanzaFactory().buildMessageStanza();
  88.         bobsConnection.sendStanza(e3.buildMessage(messageBuilder, alice.getOwnJid()));
  89.         listener3.getSyncPoint().waitForResult(10 * 1000);
  90.         alice.removeOmemoMessageListener(listener3);

  91.         OmemoBundleElement a1__ = alice.getOmemoService().getOmemoStoreBackend().packOmemoBundle(alice.getOwnDevice());
  92.         OmemoBundleElement b2_ = bob.getOmemoService().getOmemoStoreBackend().packOmemoBundle(bob.getOwnDevice());

  93.         assertEquals(a1_, a1__, "Since alice initiated the session with bob, at no time he sent a preKeyMessage, " +
  94.                 "so her bundle MUST still be the same.");
  95.         assertEquals(b2, b2_, "Bob changed his bundle earlier, but at this point his bundle must be equal to " +
  96.                 "after the first change.");
  97.     }
  98. }