2017-06-02 12:26:37 +02:00
/ * *
*
* Copyright 2017 Florian Schmaus , Paul Schaub
*
* Licensed under the Apache License , Version 2 . 0 ( the " License " ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
package org.jivesoftware.smackx.omemo ;
2017-06-14 17:12:43 +02:00
import static junit.framework.TestCase.assertEquals ;
import static junit.framework.TestCase.assertNotNull ;
import static junit.framework.TestCase.assertTrue ;
import java.io.File ;
import java.util.logging.Level ;
import java.util.logging.Logger ;
2017-06-02 12:26:37 +02:00
import org.jivesoftware.smack.SmackException ;
import org.jivesoftware.smack.XMPPException ;
import org.jivesoftware.smack.roster.Roster ;
import org.jivesoftware.smack.roster.RosterEntry ;
2017-06-14 17:12:43 +02:00
2017-06-02 12:26:37 +02:00
import org.jivesoftware.smackx.omemo.element.OmemoBundleElement ;
import org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException ;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException ;
import org.jivesoftware.smackx.omemo.internal.CachedDeviceList ;
import org.jivesoftware.smackx.omemo.util.OmemoConstants ;
import org.jivesoftware.smackx.pubsub.PubSubException ;
2017-08-14 20:14:35 +02:00
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException ;
2017-06-02 12:26:37 +02:00
import org.jivesoftware.smackx.pubsub.PubSubManager ;
/ * *
* Class containing some helper methods for OmemoIntegrationTests .
* /
final class OmemoIntegrationTestHelper {
private static final Logger LOGGER = Logger . getLogger ( OmemoIntegrationTestHelper . class . getSimpleName ( ) ) ;
static void cleanServerSideTraces ( OmemoManager omemoManager ) {
cleanUpPubSub ( omemoManager ) ;
cleanUpRoster ( omemoManager ) ;
}
static void deletePath ( File storePath ) {
FileBasedOmemoStore . deleteDirectory ( storePath ) ;
}
static void deletePath ( OmemoManager omemoManager ) {
OmemoService . getInstance ( ) . getOmemoStoreBackend ( ) . purgeOwnDeviceKeys ( omemoManager ) ;
}
static void cleanUpPubSub ( OmemoManager omemoManager ) {
PubSubManager pm = PubSubManager . getInstance ( omemoManager . getConnection ( ) , omemoManager . getOwnJid ( ) ) ;
try {
omemoManager . requestDeviceListUpdateFor ( omemoManager . getOwnJid ( ) ) ;
} catch ( SmackException . NotConnectedException | InterruptedException | SmackException . NoResponseException e ) {
2017-11-20 08:53:19 +01:00
// ignore
2017-06-02 12:26:37 +02:00
}
CachedDeviceList deviceList = OmemoService . getInstance ( ) . getOmemoStoreBackend ( ) . loadCachedDeviceList ( omemoManager , omemoManager . getOwnJid ( ) ) ;
for ( int id : deviceList . getAllDevices ( ) ) {
try {
pm . getLeafNode ( OmemoConstants . PEP_NODE_BUNDLE_FROM_DEVICE_ID ( id ) ) . deleteAllItems ( ) ;
2017-08-14 20:14:35 +02:00
} catch ( InterruptedException | SmackException . NoResponseException | SmackException . NotConnectedException | PubSubException . NotALeafNodeException | XMPPException . XMPPErrorException | NotAPubSubNodeException e ) {
2017-11-20 08:53:19 +01:00
// Silent
2017-06-02 12:26:37 +02:00
}
try {
pm . deleteNode ( OmemoConstants . PEP_NODE_BUNDLE_FROM_DEVICE_ID ( id ) ) ;
2017-08-14 20:14:35 +02:00
} catch ( SmackException . NoResponseException | InterruptedException | SmackException . NotConnectedException | XMPPException . XMPPErrorException e ) {
2017-11-20 08:53:19 +01:00
// Silent
2017-06-02 12:26:37 +02:00
}
}
try {
pm . getLeafNode ( OmemoConstants . PEP_NODE_DEVICE_LIST ) . deleteAllItems ( ) ;
2017-08-14 20:14:35 +02:00
} catch ( InterruptedException | SmackException . NoResponseException | SmackException . NotConnectedException | PubSubException . NotALeafNodeException | XMPPException . XMPPErrorException | NotAPubSubNodeException e ) {
2017-11-20 08:53:19 +01:00
// Silent
2017-06-02 12:26:37 +02:00
}
try {
pm . deleteNode ( OmemoConstants . PEP_NODE_DEVICE_LIST ) ;
2017-08-14 20:14:35 +02:00
} catch ( SmackException . NoResponseException | InterruptedException | SmackException . NotConnectedException | XMPPException . XMPPErrorException e ) {
2017-11-20 08:53:19 +01:00
// Silent
2017-06-02 12:26:37 +02:00
}
}
static void cleanUpRoster ( OmemoManager omemoManager ) {
Roster roster = Roster . getInstanceFor ( omemoManager . getConnection ( ) ) ;
for ( RosterEntry r : roster . getEntries ( ) ) {
try {
roster . removeEntry ( r ) ;
} catch ( InterruptedException | SmackException . NoResponseException | SmackException . NotConnectedException | XMPPException . XMPPErrorException | SmackException . NotLoggedInException e ) {
2017-11-20 08:53:19 +01:00
// Silent
2017-06-02 12:26:37 +02:00
}
}
}
/ * *
* Let Alice subscribe to Bob .
* @param alice
* @param bob
* @throws SmackException . NotLoggedInException
* @throws XMPPException . XMPPErrorException
* @throws SmackException . NotConnectedException
* @throws InterruptedException
* @throws SmackException . NoResponseException
* /
static void subscribe ( OmemoManager alice , OmemoManager bob , String nick )
throws SmackException . NotLoggedInException , XMPPException . XMPPErrorException ,
SmackException . NotConnectedException , InterruptedException ,
SmackException . NoResponseException {
Roster aliceRoster = Roster . getInstanceFor ( alice . getConnection ( ) ) ;
Roster bobsRoster = Roster . getInstanceFor ( bob . getConnection ( ) ) ;
bobsRoster . setSubscriptionMode ( Roster . SubscriptionMode . accept_all ) ;
aliceRoster . createEntry ( bob . getOwnJid ( ) , nick , null ) ;
}
static void unidirectionalTrust ( OmemoManager alice , OmemoManager bob ) throws SmackException . NotConnectedException , InterruptedException , SmackException . NoResponseException , CannotEstablishOmemoSessionException {
2017-11-20 08:53:19 +01:00
// Fetch deviceList
2017-06-02 12:26:37 +02:00
alice . requestDeviceListUpdateFor ( bob . getOwnJid ( ) ) ;
LOGGER . log ( Level . INFO , " Current deviceList state: " + alice . getOwnDevice ( ) + " knows " + bob . getOwnDevice ( ) + " : "
+ OmemoService . getInstance ( ) . getOmemoStoreBackend ( ) . loadCachedDeviceList ( alice , bob . getOwnJid ( ) ) ) ;
assertTrue ( " Trusting party must know the others device at this point. " ,
alice . getOmemoService ( ) . getOmemoStoreBackend ( ) . loadCachedDeviceList ( alice , bob . getOwnJid ( ) )
. getActiveDevices ( ) . contains ( bob . getDeviceId ( ) ) ) ;
2017-11-20 08:53:19 +01:00
// Create sessions
2017-06-02 12:26:37 +02:00
alice . buildSessionsWith ( bob . getOwnJid ( ) ) ;
assertTrue ( " Trusting party must have a session with the other end at this point. " ,
! alice . getOmemoService ( ) . getOmemoStoreBackend ( ) . loadAllRawSessionsOf ( alice , bob . getOwnJid ( ) ) . isEmpty ( ) ) ;
2017-11-20 08:53:19 +01:00
// Trust the other party
2017-06-02 12:26:37 +02:00
alice . getOmemoService ( ) . getOmemoStoreBackend ( ) . trustOmemoIdentity ( alice , bob . getOwnDevice ( ) ,
alice . getOmemoService ( ) . getOmemoStoreBackend ( ) . getFingerprint ( alice , bob . getOwnDevice ( ) ) ) ;
}
2017-08-14 20:14:35 +02:00
static void setUpOmemoManager ( OmemoManager omemoManager ) throws CorruptedOmemoKeyException , InterruptedException , SmackException . NoResponseException , SmackException . NotConnectedException , XMPPException . XMPPErrorException , SmackException . NotLoggedInException , PubSubException . NotALeafNodeException , NotAPubSubNodeException {
2017-06-02 12:26:37 +02:00
omemoManager . initialize ( ) ;
OmemoBundleElement bundle = OmemoService . fetchBundle ( omemoManager , omemoManager . getOwnDevice ( ) ) ;
assertNotNull ( " Bundle must not be null. " , bundle ) ;
assertEquals ( " Published Bundle must equal our local bundle. " , bundle , omemoManager . getOmemoService ( ) . getOmemoStoreBackend ( ) . packOmemoBundle ( omemoManager ) ) ;
}
}