OmemoMamDecryptionTest.java

  1. /**
  2.  *
  3.  * Copyright 2018 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 java.io.IOException;
  20. import java.util.List;

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

  25. import org.jivesoftware.smackx.mam.MamManager;
  26. import org.jivesoftware.smackx.mam.element.MamPrefsIQ;
  27. import org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException;
  28. import org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException;
  29. import org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage;

  30. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  31. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  32. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  33. import org.igniterealtime.smack.inttest.annotations.SpecificationReference;

  34. /**
  35.  * This test sends a message from Alice to Bob, while Bob has automatic decryption disabled.
  36.  * Then Bob fetches his Mam archive and decrypts the result.
  37.  */
  38. @SpecificationReference(document = "XEP-0384", version = "0.3.0")
  39. public class OmemoMamDecryptionTest extends AbstractTwoUsersOmemoIntegrationTest {
  40.     public OmemoMamDecryptionTest(SmackIntegrationTestEnvironment environment)
  41.             throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
  42.             SmackException.NoResponseException, TestNotPossibleException {
  43.         super(environment);
  44.         MamManager bobsMamManager = MamManager.getInstanceFor(conTwo);
  45.         if (!bobsMamManager.isSupported()) {
  46.             throw new TestNotPossibleException("Test is not possible, because MAM is not supported on the server.");
  47.         }
  48.     }

  49.     @SmackIntegrationTest
  50.     public void mamDecryptionTest() throws XMPPException.XMPPErrorException, SmackException.NotLoggedInException,
  51.             SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException,
  52.             CryptoFailedException, UndecidedOmemoIdentityException, IOException {
  53.         // Make sure, Bobs server stores messages in the archive
  54.         MamManager bobsMamManager = MamManager.getInstanceFor(bob.getConnection());
  55.         bobsMamManager.enableMamForAllMessages();
  56.         bobsMamManager.setDefaultBehavior(MamPrefsIQ.DefaultBehavior.always);

  57.         // Prevent bob from automatically decrypting MAM messages.
  58.         bob.stopStanzaAndPEPListeners();

  59.         String body = "This message will be stored in MAM!";
  60.         OmemoMessage.Sent encrypted = alice.encrypt(bob.getOwnJid(), body);

  61.         XMPPConnection alicesConnection = alice.getConnection();
  62.         MessageBuilder messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
  63.         alicesConnection.sendStanza(encrypted.buildMessage(messageBuilder, bob.getOwnJid()));

  64.         MamManager.MamQuery query = bobsMamManager.queryArchive(MamManager.MamQueryArgs.builder().limitResultsToJid(alice.getOwnJid()).build());
  65.         assertEquals(1, query.getMessageCount(), "Unexpected message count in MAM query result of " + bob.getConnection().getUser());

  66.         List<MessageOrOmemoMessage> decryptedMamQuery = bob.decryptMamQueryResult(query);

  67.         assertEquals(1, decryptedMamQuery.size(), "Unexpected decrypted message count in MAM query result of " + bob.getConnection().getUser());
  68.         assertEquals(body, decryptedMamQuery.get(decryptedMamQuery.size() - 1).getOmemoMessage().getBody(),
  69.             "Expected decrypted body of message retrieved via a MAM query to be equal to the original body that was sent (but it was not).");
  70.     }
  71. }