AbstractTwoUsersOmemoIntegrationTest.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 java.io.IOException;

  21. import org.jivesoftware.smack.SmackException;
  22. import org.jivesoftware.smack.SmackException.NotConnectedException;
  23. import org.jivesoftware.smack.XMPPException;

  24. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  25. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  26. import org.igniterealtime.smack.inttest.annotations.AfterClass;
  27. import org.igniterealtime.smack.inttest.annotations.BeforeClass;
  28. import org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil;

  29. /**
  30.  * Abstract OMEMO integration test framing, which creates and initializes two OmemoManagers (for conOne and conTwo).
  31.  * Both users subscribe to one another and trust their identities.
  32.  * After the test traces in PubSub and in the users Rosters are removed.
  33.  */
  34. public abstract class AbstractTwoUsersOmemoIntegrationTest extends AbstractOmemoIntegrationTest {

  35.     protected OmemoManager alice, bob;

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

  41.     @BeforeClass
  42.     public void setup() throws Exception {
  43.         alice = OmemoManagerSetupHelper.prepareOmemoManager(conOne);
  44.         bob = OmemoManagerSetupHelper.prepareOmemoManager(conTwo);

  45.         // TODO is this a test assertion, or a bug in the test implementation (in which case an Exception should be thrown instead).
  46.         assertNotEquals(alice.getDeviceId(), bob.getDeviceId(),
  47.             "Expected device ID for " + conOne.getUser() + " to differ from that of " + conTwo.getUser() + " (but they did not)");

  48.         // Subscribe presences
  49.         IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(alice.getConnection(), bob.getConnection(), timeout);

  50.         OmemoManagerSetupHelper.trustAllIdentitiesWithTests(alice, bob);    // Alice trusts Bob's devices
  51.         OmemoManagerSetupHelper.trustAllIdentitiesWithTests(bob, alice);    // Bob trusts Alice' and Mallory's devices

  52.         assertEquals(bob.getOwnFingerprint(), alice.getActiveFingerprints(bob.getOwnJid()).get(bob.getOwnDevice()),
  53.             "Expected fingerprint of " + conTwo.getUser() + "'s device as known to " + conOne.getUser() + " to be equal to " + conTwo.getUser() + "'s own fingerprint (but it was not).");
  54.         assertEquals(alice.getOwnFingerprint(), bob.getActiveFingerprints(alice.getOwnJid()).get(alice.getOwnDevice()),
  55.             "Expected fingerprint of " + conOne.getUser() + "'s device as known to " + conTwo.getUser() + " to be equal to " + conOne.getUser() + "'s own fingerprint (but it was not).");
  56.     }

  57.     @AfterClass
  58.     public void cleanUp() throws IOException, NotConnectedException, InterruptedException {
  59.         alice.stopStanzaAndPEPListeners();
  60.         bob.stopStanzaAndPEPListeners();
  61.         OmemoManagerSetupHelper.cleanUpPubSub(alice);
  62.         OmemoManagerSetupHelper.cleanUpRoster(alice);
  63.         OmemoManagerSetupHelper.cleanUpPubSub(bob);
  64.         OmemoManagerSetupHelper.cleanUpRoster(bob);
  65.     }
  66. }