OmemoManagerSetupHelper.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.assertFalse;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;

  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.List;

  24. import org.jivesoftware.smack.SmackException;
  25. import org.jivesoftware.smack.SmackException.NotConnectedException;
  26. import org.jivesoftware.smack.XMPPConnection;
  27. import org.jivesoftware.smack.XMPPException;
  28. import org.jivesoftware.smack.roster.Roster;
  29. import org.jivesoftware.smack.roster.RosterEntry;

  30. import org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException;
  31. import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
  32. import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
  33. import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
  34. import org.jivesoftware.smackx.pubsub.PubSubException;

  35. import com.google.common.collect.Maps;

  36. public class OmemoManagerSetupHelper {


  37.     public static void trustAllIdentities(OmemoManager alice, OmemoManager bob)
  38.             throws InterruptedException, SmackException.NotConnectedException, SmackException.NotLoggedInException,
  39.             SmackException.NoResponseException, CannotEstablishOmemoSessionException, CorruptedOmemoKeyException,
  40.             XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, IOException {
  41.         Roster roster = Roster.getInstanceFor(alice.getConnection());

  42.         if (alice.getOwnJid() != bob.getOwnJid() &&
  43.                 (!roster.iAmSubscribedTo(bob.getOwnJid()) || !roster.isSubscribedToMyPresence(bob.getOwnJid()))) {
  44.             throw new IllegalStateException("Before trusting identities of a user, we must be subscribed to one another.");
  45.         }

  46.         alice.requestDeviceListUpdateFor(bob.getOwnJid());
  47.         HashMap<OmemoDevice, OmemoFingerprint> fingerprints = alice.getActiveFingerprints(bob.getOwnJid());

  48.         for (OmemoDevice device : fingerprints.keySet()) {
  49.             OmemoFingerprint fingerprint = fingerprints.get(device);
  50.             alice.trustOmemoIdentity(device, fingerprint);
  51.         }
  52.     }

  53.     public static void trustAllIdentitiesWithTests(OmemoManager alice, OmemoManager bob)
  54.             throws InterruptedException, SmackException.NotConnectedException, SmackException.NotLoggedInException,
  55.             SmackException.NoResponseException, CannotEstablishOmemoSessionException, CorruptedOmemoKeyException,
  56.             XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, IOException {
  57.         alice.requestDeviceListUpdateFor(bob.getOwnJid());
  58.         HashMap<OmemoDevice, OmemoFingerprint> fps1 = alice.getActiveFingerprints(bob.getOwnJid());

  59.         assertFalse(fps1.isEmpty());
  60.         assertAllDevicesAreUndecided(alice, fps1);
  61.         assertAllDevicesAreUntrusted(alice, fps1);

  62.         trustAllIdentities(alice, bob);

  63.         HashMap<OmemoDevice, OmemoFingerprint> fps2 = alice.getActiveFingerprints(bob.getOwnJid());
  64.         assertEquals(fps1.size(), fps2.size());
  65.         assertTrue(Maps.difference(fps1, fps2).areEqual());

  66.         assertAllDevicesAreDecided(alice, fps2);
  67.         assertAllDevicesAreTrusted(alice, fps2);
  68.     }

  69.     public static OmemoManager prepareOmemoManager(XMPPConnection connection) throws Exception {
  70.         final OmemoManager manager = OmemoManager.getInstanceFor(connection, OmemoManager.randomDeviceId());
  71.         manager.setTrustCallback(new EphemeralTrustCallback());

  72.         if (connection.isAuthenticated()) {
  73.             manager.initialize();
  74.         } else {
  75.             throw new AssertionError("Connection must be authenticated.");
  76.         }
  77.         return manager;
  78.     }

  79.     public static void assertAllDevicesAreUndecided(OmemoManager manager, HashMap<OmemoDevice, OmemoFingerprint> devices) {
  80.         for (OmemoDevice device : devices.keySet()) {
  81.             // All fingerprints MUST be neither decided, nor trusted.
  82.             assertFalse(manager.isDecidedOmemoIdentity(device, devices.get(device)));
  83.         }
  84.     }

  85.     public static void assertAllDevicesAreUntrusted(OmemoManager manager, HashMap<OmemoDevice, OmemoFingerprint> devices) {
  86.         for (OmemoDevice device : devices.keySet()) {
  87.             // All fingerprints MUST be neither decided, nor trusted.
  88.             assertFalse(manager.isTrustedOmemoIdentity(device, devices.get(device)));
  89.         }
  90.     }

  91.     public static void assertAllDevicesAreDecided(OmemoManager manager, HashMap<OmemoDevice, OmemoFingerprint> devices) {
  92.         for (OmemoDevice device : devices.keySet()) {
  93.             // All fingerprints MUST be neither decided, nor trusted.
  94.             assertTrue(manager.isDecidedOmemoIdentity(device, devices.get(device)));
  95.         }
  96.     }

  97.     public static void assertAllDevicesAreTrusted(OmemoManager manager, HashMap<OmemoDevice, OmemoFingerprint> devices) {
  98.         for (OmemoDevice device : devices.keySet()) {
  99.             // All fingerprints MUST be neither decided, nor trusted.
  100.             assertTrue(manager.isTrustedOmemoIdentity(device, devices.get(device)));
  101.         }
  102.     }

  103.     public static void cleanUpPubSub(OmemoManager omemoManager)
  104.                     throws IOException, NotConnectedException, InterruptedException {
  105.         List<Exception> exceptions = omemoManager.purgeEverything();
  106.         assertTrue(exceptions.isEmpty(), "There where exceptions while purging OMEMO: " + exceptions);
  107.     }

  108.     public static void cleanUpRoster(OmemoManager omemoManager) {
  109.         Roster roster = Roster.getInstanceFor(omemoManager.getConnection());
  110.         for (RosterEntry r : roster.getEntries()) {
  111.             try {
  112.                 roster.removeEntry(r);
  113.             } catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException |
  114.                     XMPPException.XMPPErrorException | SmackException.NotLoggedInException e) {
  115.                 // Silent
  116.             }
  117.         }
  118.     }
  119. }