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

  21. import java.io.IOException;

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

  24. import org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException;
  25. import org.jivesoftware.smackx.omemo.exceptions.ReadOnlyDeviceException;
  26. import org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException;

  27. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  28. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  29. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  30. import org.igniterealtime.smack.inttest.annotations.SpecificationReference;

  31. @SpecificationReference(document = "XEP-0384", version = "0.3.0")
  32. public class ReadOnlyDeviceIntegrationTest extends AbstractTwoUsersOmemoIntegrationTest {

  33.     public ReadOnlyDeviceIntegrationTest(SmackIntegrationTestEnvironment environment) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, TestNotPossibleException {
  34.         super(environment);
  35.     }

  36.     @SmackIntegrationTest
  37.     public void test() throws InterruptedException, SmackException.NoResponseException,
  38.                     SmackException.NotLoggedInException, SmackException.NotConnectedException, CryptoFailedException,
  39.                     UndecidedOmemoIdentityException, IOException {
  40.         boolean prevIgnoreReadOnlyConf = OmemoConfiguration.getIgnoreReadOnlyDevices();
  41.         int prevMaxMessageCounter = OmemoConfiguration.getMaxReadOnlyMessageCount();

  42.         OmemoConfiguration.setIgnoreReadOnlyDevices(true);
  43.         // Set the maxReadOnlyMessageCount to ridiculously low threshold of 5.
  44.         // This means that Alice will be able to encrypt 5 messages for Bob, while the 6th will not be encrypted for Bob.
  45.         OmemoConfiguration.setMaxReadOnlyMessageCount(5);

  46.         // Reset counter to begin test
  47.         alice.getOmemoService().getOmemoStoreBackend().storeOmemoMessageCounter(alice.getOwnDevice(), bob.getOwnDevice(), 0);

  48.         // Since the max threshold is 5, we must be able to encrypt 5 messages for Bob.
  49.         for (int i = 0; i < 5; i++) {
  50.             assertEquals(i, alice.getOmemoService().getOmemoStoreBackend().loadOmemoMessageCounter(alice.getOwnDevice(), bob.getOwnDevice()));
  51.             OmemoMessage.Sent message = alice.encrypt(bob.getOwnJid(), "Hello World!");
  52.             assertFalse(message.getSkippedDevices().containsKey(bob.getOwnDevice()));
  53.         }

  54.         // Now the message counter must be too high and Bobs device must be skipped.
  55.         OmemoMessage.Sent message = alice.encrypt(bob.getOwnJid(), "Hello World!");
  56.         Throwable exception = message.getSkippedDevices().get(bob.getOwnDevice());
  57.         assertTrue(exception instanceof ReadOnlyDeviceException);
  58.         assertEquals(bob.getOwnDevice(), ((ReadOnlyDeviceException) exception).getDevice());

  59.         // Reset the message counter
  60.         alice.getOmemoService().getOmemoStoreBackend().storeOmemoMessageCounter(alice.getOwnDevice(), bob.getOwnDevice(), 0);

  61.         // Reset the configuration to previous values
  62.         OmemoConfiguration.setMaxReadOnlyMessageCount(prevMaxMessageCounter);
  63.         OmemoConfiguration.setIgnoreReadOnlyDevices(prevIgnoreReadOnlyConf);
  64.     }
  65. }