mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-07 05:31:08 +01:00
Rework support for XEP-0384: OMEMO Encryption
Changes:
Rework integration tests
New structure of base integration test classes
bump dependency on signal-protocol-java from 2.4.0 to 2.6.2
Introduced CachingOmemoStore implementations
Use CachingOmemoStore classes in integration tests
Removed OmemoSession classes (replaced with more logical OmemoRatchet classes)
Consequently also removed load/storeOmemoSession methods from OmemoStore
Removed some clutter from KeyUtil classes
Moved trust decision related code from OmemoStore to TrustCallback
Require authenticated connection for many functions
Add async initialization function in OmemoStore
Refactor omemo test package (/java/org/jivesoftware/smack/omemo -> /java/org/jivesoftware/smackx)
Remove OmemoStore method isFreshInstallation() as well as defaultDeviceId related stuff
FileBasedOmemoStore: Add cleaner methods to store/load base data types (Using tryWithResource, only for future releases, once Android API gets bumped)
Attempt to make OmemoManager thread safe
new logic for getInstanceFor() deviceId determination
OmemoManagers encrypt methods now don't throw exceptions when encryption for some devices fails. Instead message gets encrypted when possible and more information about failures gets returned alongside the message itself
Added OmemoMessage class for that purpose
Reworked entire OmemoService class
Use safer logic for creating trust-ignoring messages (like ratchet-update messages)
Restructure elements/provider in order to prepare for OMEMO namespace bumps
Remove OmemoManager.regenerate() methods in favor of getInstanceFor(connection, randomDeviceId)
Removed some unnecessary configuration options
Prepare for support of more AES message key types
Simplify session creation
Where possible, avoid side effects in methods
Add UntrustedOmemoIdentityException
Add TrustState enum
More improved tests
This commit is contained in:
parent
f290197f6a
commit
1f731f6318
96 changed files with 6915 additions and 5488 deletions
|
|
@ -1,47 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 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.smack.omemo;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.util.OmemoKeyUtil;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test KeyUtil functions.
|
||||
*
|
||||
* @author Paul Schaub
|
||||
*/
|
||||
public class OmemoKeyUtilTest {
|
||||
|
||||
@Test
|
||||
public void testAddInBounds() {
|
||||
int high = Integer.MAX_VALUE - 2;
|
||||
int max = Integer.MAX_VALUE;
|
||||
assertEquals(OmemoKeyUtil.addInBounds(high, 3), 1);
|
||||
assertEquals(OmemoKeyUtil.addInBounds(1,2), 3);
|
||||
assertEquals(OmemoKeyUtil.addInBounds(max, 5), 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrettyFingerprint() {
|
||||
String ugly = "FFFFFFFFEEEEEEEEDDDDDDDDCCCCCCCCBBBBBBBBAAAAAAAA9999999988888888";
|
||||
String pretty = OmemoKeyUtil.prettyFingerprint(ugly);
|
||||
assertEquals(pretty, "FFFFFFFF EEEEEEEE DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA 99999999 88888888");
|
||||
}
|
||||
}
|
||||
|
|
@ -14,15 +14,16 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.internal.CachedDeviceList;
|
||||
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
@ -42,7 +43,7 @@ public class DeviceListTest {
|
|||
*/
|
||||
@Test
|
||||
public void mergeDeviceListsTest() {
|
||||
CachedDeviceList cached = new CachedDeviceList();
|
||||
OmemoCachedDeviceList cached = new OmemoCachedDeviceList();
|
||||
assertNotNull(cached.getActiveDevices());
|
||||
assertNotNull(cached.getInactiveDevices());
|
||||
|
||||
|
|
@ -67,5 +68,11 @@ public class DeviceListTest {
|
|||
!cached.getInactiveDevices().contains(4));
|
||||
|
||||
assertTrue(cached.getAllDevices().size() == 4);
|
||||
|
||||
assertFalse(cached.contains(17));
|
||||
cached.addDevice(17);
|
||||
assertTrue(cached.getActiveDevices().contains(17));
|
||||
|
||||
assertNotNull(cached.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
@ -27,7 +27,7 @@ import org.jivesoftware.smack.test.util.TestUtils;
|
|||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoBundleVAxolotlElement;
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl;
|
||||
import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -50,7 +50,7 @@ public class OmemoBundleVAxolotlElementTest extends SmackTestSuite {
|
|||
preKeysB64.put(preKeyId1, preKey1B64);
|
||||
preKeysB64.put(preKeyId2, preKey2B64);
|
||||
|
||||
OmemoBundleVAxolotlElement bundle = new OmemoBundleVAxolotlElement(signedPreKeyId,
|
||||
OmemoBundleElement_VAxolotl bundle = new OmemoBundleElement_VAxolotl(signedPreKeyId,
|
||||
signedPreKeyB64, signedPreKeySigB64, identityKeyB64, preKeysB64);
|
||||
|
||||
assertEquals("ElementName must match.", "bundle", bundle.getElementName());
|
||||
|
|
@ -85,7 +85,7 @@ public class OmemoBundleVAxolotlElementTest extends SmackTestSuite {
|
|||
byte[] firstPreKey = "FirstPreKey".getBytes(StringUtils.UTF8);
|
||||
byte[] secondPreKey = "SecondPreKey".getBytes(StringUtils.UTF8);
|
||||
|
||||
OmemoBundleVAxolotlElement parsed = new OmemoBundleVAxolotlProvider().parse(TestUtils.getParser(actual));
|
||||
OmemoBundleElement_VAxolotl parsed = new OmemoBundleVAxolotlProvider().parse(TestUtils.getParser(actual));
|
||||
|
||||
assertTrue("B64-decoded signedPreKey must match.", Arrays.equals(signedPreKey, parsed.getSignedPreKey()));
|
||||
assertEquals("SignedPreKeyId must match", signedPreKeyId, parsed.getSignedPreKeyId());
|
||||
|
|
@ -14,14 +14,11 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.OmemoConfiguration;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
|
@ -34,25 +31,6 @@ public class OmemoConfigurationTest {
|
|||
@Test
|
||||
public void omemoConfigurationTest() {
|
||||
@SuppressWarnings("unused") OmemoConfiguration configuration = new OmemoConfiguration();
|
||||
// Default Store Path
|
||||
File storePath = new File("test");
|
||||
assertNull("getFileBasedOmemoStoreDefaultPath MUST return null at this point.",
|
||||
OmemoConfiguration.getFileBasedOmemoStoreDefaultPath());
|
||||
OmemoConfiguration.setFileBasedOmemoStoreDefaultPath(storePath);
|
||||
assertEquals("FileBasedOmemoStoreDefaultPath must equal the one we set.", storePath.getAbsolutePath(),
|
||||
OmemoConfiguration.getFileBasedOmemoStoreDefaultPath().getAbsolutePath());
|
||||
|
||||
// EME
|
||||
OmemoConfiguration.setAddEmeEncryptionHint(false);
|
||||
assertEquals(false, OmemoConfiguration.getAddEmeEncryptionHint());
|
||||
OmemoConfiguration.setAddEmeEncryptionHint(true);
|
||||
assertEquals(true, OmemoConfiguration.getAddEmeEncryptionHint());
|
||||
|
||||
// MAM
|
||||
OmemoConfiguration.setAddMAMStorageProcessingHint(false);
|
||||
assertEquals(false, OmemoConfiguration.getAddMAMStorageProcessingHint());
|
||||
OmemoConfiguration.setAddMAMStorageProcessingHint(true);
|
||||
assertEquals(true, OmemoConfiguration.getAddMAMStorageProcessingHint());
|
||||
|
||||
// Body hint
|
||||
OmemoConfiguration.setAddOmemoHintBody(false);
|
||||
|
|
@ -109,5 +87,17 @@ public class OmemoConfigurationTest {
|
|||
} catch (IllegalArgumentException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
// Repair broken sessions
|
||||
OmemoConfiguration.setRepairBrokenSessionsWithPrekeyMessages(false);
|
||||
assertFalse(OmemoConfiguration.getRepairBrokenSessionsWithPreKeyMessages());
|
||||
OmemoConfiguration.setRepairBrokenSessionsWithPrekeyMessages(true);
|
||||
assertTrue(OmemoConfiguration.getRepairBrokenSessionsWithPreKeyMessages());
|
||||
|
||||
// Complete fresh sessions
|
||||
OmemoConfiguration.setCompleteSessionWithEmptyMessage(false);
|
||||
assertFalse(OmemoConfiguration.getCompleteSessionWithEmptyMessage());
|
||||
OmemoConfiguration.setCompleteSessionWithEmptyMessage(true);
|
||||
assertTrue(OmemoConfiguration.getCompleteSessionWithEmptyMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
@ -24,7 +24,7 @@ import java.util.HashSet;
|
|||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
import org.jivesoftware.smack.test.util.TestUtils;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoDeviceListVAxolotlElement;
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl;
|
||||
import org.jivesoftware.smackx.omemo.provider.OmemoDeviceListVAxolotlProvider;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -41,11 +41,11 @@ public class OmemoDeviceListVAxolotlElementTest extends SmackTestSuite {
|
|||
ids.add(1234);
|
||||
ids.add(9876);
|
||||
|
||||
OmemoDeviceListVAxolotlElement element = new OmemoDeviceListVAxolotlElement(ids);
|
||||
OmemoDeviceListElement_VAxolotl element = new OmemoDeviceListElement_VAxolotl(ids);
|
||||
String xml = element.toXML(null).toString();
|
||||
|
||||
XmlPullParser parser = TestUtils.getParser(xml);
|
||||
OmemoDeviceListVAxolotlElement parsed = new OmemoDeviceListVAxolotlProvider().parse(parser);
|
||||
OmemoDeviceListElement_VAxolotl parsed = new OmemoDeviceListVAxolotlProvider().parse(parser);
|
||||
|
||||
assertTrue("Parsed element must equal the original.", parsed.getDeviceIds().equals(element.getDeviceIds()));
|
||||
assertEquals("Generated XML must match.",
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static junit.framework.TestCase.fail;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
@ -14,12 +14,12 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertNotSame;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.OmemoFingerprint;
|
||||
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 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;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public class OmemoServiceTest extends SmackTestSuite {
|
||||
|
||||
private static final long ONE_HOUR = 1000L * 60 * 60;
|
||||
private static final int IGNORE_STALE = OmemoConfiguration.getIgnoreStaleDevicesAfterHours();
|
||||
private static final int DELETE_STALE = OmemoConfiguration.getDeleteStaleDevicesAfterHours();
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void getInstanceFailsWhenNullTest() {
|
||||
OmemoService.getInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isServiceRegisteredTest() {
|
||||
assertFalse(OmemoService.isServiceRegistered());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test correct functionality of isStale method.
|
||||
* @throws XmppStringprepException
|
||||
*/
|
||||
@Test
|
||||
public void isStaleDeviceTest() throws XmppStringprepException {
|
||||
OmemoDevice user = new OmemoDevice(JidCreate.bareFrom("alice@wonderland.lit"), 123);
|
||||
OmemoDevice other = new OmemoDevice(JidCreate.bareFrom("bob@builder.tv"), 444);
|
||||
|
||||
Date now = new Date();
|
||||
Date ignoreMe = new Date(now.getTime() - ((IGNORE_STALE + 1) * ONE_HOUR));
|
||||
Date deleteMe = new Date(now.getTime() - ((DELETE_STALE + 1) * ONE_HOUR));
|
||||
Date imFine = new Date(now.getTime() - ONE_HOUR);
|
||||
|
||||
// One hour "old" devices are (probably) not not stale
|
||||
assertFalse(OmemoService.isStale(user, other, imFine, IGNORE_STALE));
|
||||
|
||||
// Devices one hour "older" than max ages are stale
|
||||
assertTrue(OmemoService.isStale(user, other, ignoreMe, IGNORE_STALE));
|
||||
assertTrue(OmemoService.isStale(user, other, deleteMe, DELETE_STALE));
|
||||
|
||||
// Own device is never stale, no matter how old
|
||||
assertFalse(OmemoService.isStale(user, user, deleteMe, DELETE_STALE));
|
||||
|
||||
// Always return false if date is null.
|
||||
assertFalse(OmemoService.isStale(user, other, null, DELETE_STALE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeOurDeviceTest() throws XmppStringprepException {
|
||||
OmemoDevice a = new OmemoDevice(JidCreate.bareFrom("a@b.c"), 123);
|
||||
OmemoDevice b = new OmemoDevice(JidCreate.bareFrom("a@b.c"), 124);
|
||||
|
||||
HashSet<OmemoDevice> devices = new HashSet<>();
|
||||
devices.add(a); devices.add(b);
|
||||
|
||||
assertTrue(devices.contains(a));
|
||||
assertTrue(devices.contains(b));
|
||||
OmemoService.removeOurDevice(a, devices);
|
||||
|
||||
assertFalse(devices.contains(a));
|
||||
assertTrue(devices.contains(b));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,345 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 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;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
import static junit.framework.TestCase.assertNotSame;
|
||||
import static junit.framework.TestCase.assertNull;
|
||||
import static junit.framework.TestCase.assertSame;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
|
||||
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
|
||||
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
|
||||
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
|
||||
public abstract class OmemoStoreTest<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> {
|
||||
|
||||
protected final OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store;
|
||||
private final OmemoDevice alice, bob;
|
||||
|
||||
private static final TemporaryFolder tmp = initStaticTemp();
|
||||
|
||||
OmemoStoreTest(OmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> store)
|
||||
throws XmppStringprepException {
|
||||
this.store = store;
|
||||
alice = new OmemoDevice(JidCreate.bareFrom("alice@wonderland.lit"), 123);
|
||||
bob = new OmemoDevice(JidCreate.bareFrom("bob@builder.tv"), 987);
|
||||
}
|
||||
|
||||
// Tests
|
||||
|
||||
@Test
|
||||
public void keyUtilNotNull() {
|
||||
assertNotNull(store.keyUtil());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateOmemoIdentityKeyPairDoesNotReturnNull() {
|
||||
assertNotNull(store.generateOmemoIdentityKeyPair());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void identityKeyFromIdentityKeyPairIsNotNull() {
|
||||
T_IdKeyPair pair = store.generateOmemoIdentityKeyPair();
|
||||
assertNotNull(store.keyUtil().identityKeyFromPair(pair));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeLoadRemoveOmemoIdentityKeyPair()
|
||||
throws IOException, CorruptedOmemoKeyException {
|
||||
|
||||
T_IdKeyPair before = store.generateOmemoIdentityKeyPair();
|
||||
|
||||
assertNull(store.loadOmemoIdentityKeyPair(alice));
|
||||
store.storeOmemoIdentityKeyPair(alice, before);
|
||||
|
||||
T_IdKeyPair after = store.loadOmemoIdentityKeyPair(alice);
|
||||
assertNotNull(after);
|
||||
|
||||
// Fingerprints equal
|
||||
assertEquals(store.keyUtil().getFingerprintOfIdentityKeyPair(before),
|
||||
store.keyUtil().getFingerprintOfIdentityKeyPair(after));
|
||||
|
||||
// Byte-representation equals
|
||||
assertTrue(Arrays.equals(
|
||||
store.keyUtil().identityKeyPairToBytes(before),
|
||||
store.keyUtil().identityKeyPairToBytes(after)));
|
||||
|
||||
// Non-existing keypair
|
||||
assertNull("Must return null for non-existing key pairs.", store.loadOmemoIdentityKeyPair(bob));
|
||||
|
||||
// Deleting works
|
||||
store.removeOmemoIdentityKeyPair(alice);
|
||||
assertNull(store.loadOmemoIdentityKeyPair(alice));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeLoadRemoveOmemoIdentityKey()
|
||||
throws IOException, CorruptedOmemoKeyException {
|
||||
|
||||
// Create IdentityKeys and get bytes
|
||||
T_IdKey keyA1 = store.keyUtil().identityKeyFromPair(store.generateOmemoIdentityKeyPair());
|
||||
T_IdKey keyB1 = store.keyUtil().identityKeyFromPair(store.generateOmemoIdentityKeyPair());
|
||||
byte[] bytesA1 = store.keyUtil().identityKeyToBytes(keyA1);
|
||||
byte[] bytesB = store.keyUtil().identityKeyToBytes(keyB1);
|
||||
|
||||
// Not null and not of length 0
|
||||
assertNotNull("Serialized identityKey cannot be null.", bytesA1);
|
||||
assertNotNull("Serialized identityKey cannot be null.", bytesB);
|
||||
assertNotSame("Serialized identityKey must be of length > 0.", 0, bytesA1.length);
|
||||
assertNotSame("Serialized identityKey must be of length > 0.", 0, bytesB.length);
|
||||
|
||||
// Keys do not equal
|
||||
assertFalse("Generated IdentityKeys must not be equal (ULTRA unlikely).",
|
||||
Arrays.equals(bytesA1, bytesB));
|
||||
|
||||
// Loading must return null before and not null after saving
|
||||
assertNull("Must return null, the store could not have this key by now.",
|
||||
store.loadOmemoIdentityKey(alice, bob));
|
||||
store.storeOmemoIdentityKey(alice, bob, keyA1);
|
||||
T_IdKey keyA2 = store.loadOmemoIdentityKey(alice, bob);
|
||||
assertNotNull(keyA2);
|
||||
|
||||
// Loaded key must equal stored one
|
||||
byte[] bytesA2 = store.keyUtil().identityKeyToBytes(keyA2);
|
||||
assertTrue("Serialized loaded key must equal serialized stored one.",
|
||||
Arrays.equals(bytesA1, bytesA2));
|
||||
|
||||
// Non-existing keys must return null
|
||||
assertNull("Non-existing keys must be returned as null.", store.loadOmemoIdentityKey(bob, alice));
|
||||
|
||||
// Key must vanish when deleted.
|
||||
store.removeOmemoIdentityKey(alice, bob);
|
||||
assertNull(store.loadOmemoIdentityKey(alice, bob));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateOmemoPreKeys() {
|
||||
TreeMap<Integer, T_PreKey> keys = store.generateOmemoPreKeys(31, 49);
|
||||
assertNotNull("Generated data structure must not be null.", keys);
|
||||
|
||||
byte[] lastKey = null;
|
||||
|
||||
for (int i = 31; i <= 79; i++) {
|
||||
assertEquals("Key ids must be ascending order, starting at 31.", Integer.valueOf(i), keys.firstKey());
|
||||
assertNotNull("Every id must match to a key.", keys.get(keys.firstKey()));
|
||||
byte[] bytes = store.keyUtil().preKeyToBytes(keys.get(keys.firstKey()));
|
||||
assertNotNull("Serialized preKey must not be null.", bytes);
|
||||
assertNotSame("Serialized preKey must not be of length 0.", 0, bytes.length);
|
||||
|
||||
if (lastKey != null) {
|
||||
assertFalse("PreKeys MUST NOT be equal.", Arrays.equals(lastKey, bytes));
|
||||
}
|
||||
lastKey = bytes;
|
||||
|
||||
keys.remove(keys.firstKey());
|
||||
|
||||
}
|
||||
|
||||
assertEquals("After deleting 49 keys, there must be no keys left.", 0, keys.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeLoadRemoveOmemoPreKeys()
|
||||
throws IOException, InterruptedException {
|
||||
|
||||
TreeMap<Integer, T_PreKey> before = store.generateOmemoPreKeys(1, 10);
|
||||
assertEquals("The store must have no prekeys before this test.", 0, store.loadOmemoPreKeys(alice).size());
|
||||
|
||||
store.storeOmemoPreKeys(alice, before);
|
||||
TreeMap<Integer, T_PreKey> after = store.loadOmemoPreKeys(alice);
|
||||
assertNotNull("Loaded preKeys must not be null.", after);
|
||||
assertEquals("Loaded preKey count must equal stored count.", before.size(), after.size());
|
||||
|
||||
// Non-existing key must be returned as null
|
||||
assertNull("Non-existing preKey must be returned as null.", store.loadOmemoPreKey(alice, 10000));
|
||||
|
||||
int last = after.size();
|
||||
for (int i = 1; i <= last; i++) {
|
||||
T_PreKey bKey = before.get(i);
|
||||
T_PreKey aKey = after.get(i);
|
||||
|
||||
assertTrue("Loaded keys must equal stored ones.", Arrays.equals(
|
||||
store.keyUtil().preKeyToBytes(bKey),
|
||||
store.keyUtil().preKeyToBytes(aKey)));
|
||||
|
||||
T_PreKey rKey = store.loadOmemoPreKey(alice, i);
|
||||
assertNotNull("Randomly accessed preKeys must not be null.", rKey);
|
||||
assertTrue("Randomly accessed preKeys must equal the stored ones.", Arrays.equals(
|
||||
store.keyUtil().preKeyToBytes(aKey),
|
||||
store.keyUtil().preKeyToBytes(rKey)));
|
||||
|
||||
store.removeOmemoPreKey(alice, i);
|
||||
assertNull("PreKey must be null after deletion.", store.loadOmemoPreKey(alice, i));
|
||||
}
|
||||
|
||||
TreeMap<Integer, T_PreKey> postDeletion = store.loadOmemoPreKeys(alice);
|
||||
assertSame("PreKey count must equal 0 after deletion of all keys.", 0, postDeletion.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeLoadRemoveOmemoSignedPreKeys()
|
||||
throws IOException, CorruptedOmemoKeyException {
|
||||
|
||||
TreeMap<Integer, T_SigPreKey> before = store.loadOmemoSignedPreKeys(alice);
|
||||
assertEquals("At this stage, there must be no signed prekeys in the store.", 0, before.size());
|
||||
|
||||
T_IdKeyPair idp = store.generateOmemoIdentityKeyPair();
|
||||
T_SigPreKey spk = store.generateOmemoSignedPreKey(idp, 125);
|
||||
|
||||
assertNotNull("SignedPreKey must not be null.", spk);
|
||||
assertEquals("ID of signedPreKey must match.", 125, store.keyUtil().signedPreKeyIdFromKey(spk));
|
||||
byte[] bytes = store.keyUtil().signedPreKeyToBytes(spk);
|
||||
assertNotNull("Serialized signedPreKey must not be null", bytes);
|
||||
assertNotSame("Serialized signedPreKey must not be of length 0.", 0, bytes.length);
|
||||
|
||||
// Stored key must equal loaded key
|
||||
store.storeOmemoSignedPreKey(alice, 125, spk);
|
||||
TreeMap<Integer, T_SigPreKey> after = store.loadOmemoSignedPreKeys(alice);
|
||||
assertEquals("We must have exactly 1 signedPreKey now.", 1, after.size());
|
||||
T_SigPreKey spk2 = after.get(after.firstKey());
|
||||
assertEquals("Id of the stored signedPreKey must match the one we stored.",
|
||||
125, store.keyUtil().signedPreKeyIdFromKey(spk2));
|
||||
assertTrue("Serialization of stored and loaded signed preKey must equal.", Arrays.equals(
|
||||
store.keyUtil().signedPreKeyToBytes(spk), store.keyUtil().signedPreKeyToBytes(spk2)));
|
||||
|
||||
// Random access
|
||||
T_SigPreKey rspk = store.loadOmemoSignedPreKey(alice, 125);
|
||||
assertTrue("Serialization of stored and randomly accessed signed preKey must equal.", Arrays.equals(
|
||||
store.keyUtil().signedPreKeyToBytes(spk), store.keyUtil().signedPreKeyToBytes(rspk)));
|
||||
assertNull("Non-existing signedPreKey must be returned as null.",
|
||||
store.loadOmemoSignedPreKey(alice, 10000));
|
||||
|
||||
// Deleting
|
||||
store.removeOmemoSignedPreKey(alice, 125);
|
||||
assertNull("Deleted key must be returned as null.", store.loadOmemoSignedPreKey(alice, 125));
|
||||
assertEquals(0, store.loadOmemoSignedPreKeys(alice).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadStoreDateOfLastSignedPreKeyRenewal() throws IOException {
|
||||
assertNull("The date of last signed preKey renewal must be null at this stage.",
|
||||
store.getDateOfLastSignedPreKeyRenewal(alice));
|
||||
Date before = new Date();
|
||||
store.setDateOfLastSignedPreKeyRenewal(alice, before);
|
||||
Date after = store.getDateOfLastSignedPreKeyRenewal(alice);
|
||||
assertEquals("Dates must equal.", after, before);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadStoreDateOfLastMessageReceived() throws IOException {
|
||||
assertNull("The date of last message received must be null at this stage.",
|
||||
store.getDateOfLastReceivedMessage(alice, bob));
|
||||
Date before = new Date();
|
||||
store.setDateOfLastReceivedMessage(alice, bob, before);
|
||||
Date after = store.getDateOfLastReceivedMessage(alice, bob);
|
||||
assertEquals("Dates must equal.", after, before);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadStoreCachedDeviceList() throws IOException {
|
||||
Integer[] active = new Integer[] {1,5,999,10};
|
||||
Integer[] inactive = new Integer[] {6,7,8};
|
||||
OmemoCachedDeviceList before = new OmemoCachedDeviceList(
|
||||
new HashSet<>(Arrays.asList(active)),
|
||||
new HashSet<>(Arrays.asList(inactive)));
|
||||
|
||||
assertNotNull("Loading a non-existent cached deviceList must return an empty list.",
|
||||
store.loadCachedDeviceList(alice, bob.getJid()));
|
||||
|
||||
store.storeCachedDeviceList(alice, bob.getJid(), before);
|
||||
OmemoCachedDeviceList after = store.loadCachedDeviceList(alice, bob.getJid());
|
||||
assertTrue("Loaded deviceList must not be empty", after.getAllDevices().size() != 0);
|
||||
|
||||
assertEquals("Number of entries in active devices must match.", active.length, after.getActiveDevices().size());
|
||||
assertEquals("Number of entries in inactive devices must match.", inactive.length, after.getInactiveDevices().size());
|
||||
assertEquals("Number of total entries must match.", active.length + inactive.length, after.getAllDevices().size());
|
||||
|
||||
for (Integer a : active) {
|
||||
assertTrue(after.getActiveDevices().contains(a));
|
||||
assertTrue(after.getAllDevices().contains(a));
|
||||
}
|
||||
|
||||
for (Integer i : inactive) {
|
||||
assertTrue(after.getInactiveDevices().contains(i));
|
||||
assertTrue(after.getAllDevices().contains(i));
|
||||
}
|
||||
|
||||
store.storeCachedDeviceList(alice, bob.getJid(), new OmemoCachedDeviceList());
|
||||
assertEquals("DeviceList must be empty after overwriting it with empty list.", 0,
|
||||
store.loadCachedDeviceList(alice, bob.getJid()).getAllDevices().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllRawSessionsReturnsEmptyMapTest() {
|
||||
HashMap<Integer, T_Sess> sessions = store.loadAllRawSessionsOf(alice, bob.getJid());
|
||||
assertNotNull(sessions);
|
||||
assertEquals(0, sessions.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadNonExistentRawSessionReturnsNullTest() {
|
||||
T_Sess session = store.loadRawSession(alice, bob);
|
||||
assertNull(session);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFingerprint() throws IOException, CorruptedOmemoKeyException {
|
||||
assertNull("Method must return null for a non-existent fingerprint.", store.getFingerprint(alice));
|
||||
store.storeOmemoIdentityKeyPair(alice, store.generateOmemoIdentityKeyPair());
|
||||
OmemoFingerprint fingerprint = store.getFingerprint(alice);
|
||||
assertNotNull("fingerprint must not be null", fingerprint);
|
||||
assertEquals("Fingerprint must be of length 64", 64, fingerprint.length());
|
||||
|
||||
store.removeOmemoIdentityKeyPair(alice); //clean up
|
||||
}
|
||||
|
||||
// ##############################################################
|
||||
// Workaround for https://github.com/junit-team/junit4/issues/671
|
||||
|
||||
static TemporaryFolder initStaticTemp() {
|
||||
try {
|
||||
return new TemporaryFolder() { { before(); } };
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException(t);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() throws Exception {
|
||||
FileBasedOmemoStore.deleteDirectory(tmp.getRoot());
|
||||
}
|
||||
|
||||
// ##############################################################
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
|
@ -24,9 +24,9 @@ import org.jivesoftware.smack.test.util.SmackTestSuite;
|
|||
import org.jivesoftware.smack.test.util.TestUtils;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoElement;
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoVAxolotlElement;
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl;
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl;
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoKeyElement;
|
||||
import org.jivesoftware.smackx.omemo.provider.OmemoVAxolotlProvider;
|
||||
import org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder;
|
||||
|
||||
|
|
@ -47,12 +47,12 @@ public class OmemoVAxolotlElementTest extends SmackTestSuite {
|
|||
int sid = 12131415;
|
||||
byte[] iv = OmemoMessageBuilder.generateIv();
|
||||
|
||||
ArrayList<OmemoElement.OmemoHeader.Key> keys = new ArrayList<>();
|
||||
keys.add(new OmemoElement.OmemoHeader.Key(keyData1, keyId1));
|
||||
keys.add(new OmemoElement.OmemoHeader.Key(keyData2, keyId2, true));
|
||||
ArrayList<OmemoKeyElement> keys = new ArrayList<>();
|
||||
keys.add(new OmemoKeyElement(keyData1, keyId1));
|
||||
keys.add(new OmemoKeyElement(keyData2, keyId2, true));
|
||||
|
||||
OmemoVAxolotlElement.OmemoHeader header = new OmemoElement.OmemoHeader(sid, keys, iv);
|
||||
OmemoVAxolotlElement element = new OmemoVAxolotlElement(header, payload);
|
||||
OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(sid, keys, iv);
|
||||
OmemoElement_VAxolotl element = new OmemoElement_VAxolotl(header, payload);
|
||||
|
||||
String expected =
|
||||
"<encrypted xmlns='eu.siacs.conversations.axolotl'>" +
|
||||
|
|
@ -69,7 +69,9 @@ public class OmemoVAxolotlElementTest extends SmackTestSuite {
|
|||
String actual = element.toXML(null).toString();
|
||||
assertEquals("Serialized xml of OmemoElement must match.", expected, actual);
|
||||
|
||||
OmemoVAxolotlElement parsed = new OmemoVAxolotlProvider().parse(TestUtils.getParser(actual));
|
||||
assertEquals("Parsed OmemoElement must equal the original.", element.toXML(null).toString(), parsed.toXML(null).toString());
|
||||
OmemoElement_VAxolotl parsed = new OmemoVAxolotlProvider().parse(TestUtils.getParser(actual));
|
||||
assertEquals("Parsed OmemoElement must equal the original.",
|
||||
element.toXML(null).toString(),
|
||||
parsed.toXML(null).toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -14,10 +14,12 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.omemo;
|
||||
package org.jivesoftware.smackx.omemo;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYLENGTH;
|
||||
import static org.jivesoftware.smackx.omemo.util.OmemoConstants.Crypto.KEYTYPE;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
|
@ -25,35 +27,20 @@ import static org.junit.Assert.assertNotNull;
|
|||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Security;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.element.OmemoElement;
|
||||
import org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException;
|
||||
import org.jivesoftware.smackx.omemo.internal.CipherAndAuthTag;
|
||||
import org.jivesoftware.smackx.omemo.internal.CiphertextTuple;
|
||||
import org.jivesoftware.smackx.omemo.internal.ClearTextMessage;
|
||||
import org.jivesoftware.smackx.omemo.internal.IdentityKeyWrapper;
|
||||
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
|
||||
import org.jivesoftware.smackx.omemo.internal.OmemoMessageInformation;
|
||||
import org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder;
|
||||
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.jid.BareJid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
|
||||
/**
|
||||
* Test the identityKeyWrapper.
|
||||
*/
|
||||
public class WrapperObjectsTest {
|
||||
|
||||
@Test
|
||||
public void identityKeyWrapperTest() {
|
||||
Object pseudoKey = new Object();
|
||||
IdentityKeyWrapper wrapper = new IdentityKeyWrapper(pseudoKey);
|
||||
assertEquals(pseudoKey, wrapper.getIdentityKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ciphertextTupleTest() {
|
||||
byte[] c = OmemoMessageBuilder.generateIv();
|
||||
|
|
@ -67,41 +54,19 @@ public class WrapperObjectsTest {
|
|||
assertEquals(OmemoElement.TYPE_OMEMO_MESSAGE, c2.getMessageType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearTextMessageTest() throws Exception {
|
||||
Object pseudoKey = new Object();
|
||||
IdentityKeyWrapper wrapper = new IdentityKeyWrapper(pseudoKey);
|
||||
BareJid senderJid = JidCreate.bareFrom("bob@server.tld");
|
||||
OmemoDevice sender = new OmemoDevice(senderJid, 1234);
|
||||
OmemoMessageInformation information = new OmemoMessageInformation(wrapper, sender, OmemoMessageInformation.CARBON.NONE);
|
||||
|
||||
assertTrue("OmemoInformation must state that the message is an OMEMO message.",
|
||||
information.isOmemoMessage());
|
||||
assertEquals(OmemoMessageInformation.CARBON.NONE, information.getCarbon());
|
||||
assertEquals(sender, information.getSenderDevice());
|
||||
assertEquals(wrapper, information.getSenderIdentityKey());
|
||||
|
||||
String body = "Decrypted Body";
|
||||
Message message = new Message(senderJid, body);
|
||||
ClearTextMessage c = new ClearTextMessage(body, message, information);
|
||||
|
||||
assertEquals(message, c.getOriginalMessage());
|
||||
assertEquals(information, c.getMessageInformation());
|
||||
assertEquals(body, c.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cipherAndAuthTagTest() throws NoSuchAlgorithmException, CryptoFailedException {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
byte[] key = OmemoMessageBuilder.generateKey();
|
||||
byte[] key = OmemoMessageBuilder.generateKey(KEYTYPE, KEYLENGTH);
|
||||
byte[] iv = OmemoMessageBuilder.generateIv();
|
||||
byte[] authTag = OmemoMessageBuilder.generateIv();
|
||||
|
||||
CipherAndAuthTag cat = new CipherAndAuthTag(key, iv, authTag);
|
||||
CipherAndAuthTag cat = new CipherAndAuthTag(key, iv, authTag, true);
|
||||
|
||||
assertNotNull(cat.getCipher());
|
||||
assertArrayEquals(key, cat.getKey());
|
||||
assertArrayEquals(iv, cat.getIv());
|
||||
assertArrayEquals(authTag, cat.getAuthTag());
|
||||
assertTrue(cat.wasPreKeyEncrypted());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
*
|
||||
* Copyright the original author or authors
|
||||
*
|
||||
* 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.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
|
||||
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
|
||||
import org.jivesoftware.smackx.omemo.trust.OmemoTrustCallback;
|
||||
import org.jivesoftware.smackx.omemo.trust.TrustState;
|
||||
|
||||
/**
|
||||
* Ephemera Trust Callback used to make trust decisions in tests.
|
||||
*/
|
||||
public class EphemeralTrustCallback implements OmemoTrustCallback {
|
||||
|
||||
private final HashMap<OmemoDevice, HashMap<OmemoFingerprint, TrustState>> trustStates = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public TrustState getTrust(OmemoDevice device, OmemoFingerprint fingerprint) {
|
||||
HashMap<OmemoFingerprint, TrustState> states = trustStates.get(device);
|
||||
|
||||
if (states != null) {
|
||||
TrustState state = states.get(fingerprint);
|
||||
|
||||
if (state != null) {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
return TrustState.undecided;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTrust(OmemoDevice device, OmemoFingerprint fingerprint, TrustState state) {
|
||||
HashMap<OmemoFingerprint, TrustState> states = trustStates.get(device);
|
||||
|
||||
if (states == null) {
|
||||
states = new HashMap<>();
|
||||
trustStates.put(device, states);
|
||||
}
|
||||
|
||||
states.put(fingerprint, state);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
*
|
||||
* Copyright the original author or authors
|
||||
*
|
||||
* 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.util;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class OmemoMessageBuilderTest extends SmackTestSuite {
|
||||
|
||||
private static final byte[] messageKey = new byte[16];
|
||||
private static final byte[] cipherTextWithAuthTag = new byte[35 + 16];
|
||||
|
||||
public OmemoMessageBuilderTest() {
|
||||
Random random = new Random();
|
||||
random.nextBytes(messageKey);
|
||||
random.nextBytes(cipherTextWithAuthTag);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveAuthTag() {
|
||||
// Extract authTag for testing purposes
|
||||
byte[] authTag = new byte[16];
|
||||
System.arraycopy(cipherTextWithAuthTag, 35, authTag, 0, 16);
|
||||
|
||||
byte[] messageKeyWithAuthTag = new byte[16 + 16];
|
||||
byte[] cipherTextWithoutAuthTag = new byte[35];
|
||||
|
||||
OmemoMessageBuilder.moveAuthTag(messageKey, cipherTextWithAuthTag, messageKeyWithAuthTag, cipherTextWithoutAuthTag);
|
||||
|
||||
// Check if first n - 16 bytes of cipherText got copied over to cipherTextWithoutAuthTag correctly
|
||||
byte[] checkCipherText = new byte[35];
|
||||
System.arraycopy(cipherTextWithAuthTag, 0, checkCipherText, 0, 35);
|
||||
assertTrue(Arrays.equals(checkCipherText, cipherTextWithoutAuthTag));
|
||||
|
||||
byte[] checkMessageKey = new byte[16];
|
||||
System.arraycopy(messageKeyWithAuthTag, 0, checkMessageKey, 0, 16);
|
||||
assertTrue(Arrays.equals(checkMessageKey, messageKey));
|
||||
|
||||
byte[] checkAuthTag = new byte[16];
|
||||
System.arraycopy(messageKeyWithAuthTag, 16, checkAuthTag, 0, 16);
|
||||
assertTrue(Arrays.equals(checkAuthTag, authTag));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCheckIllegalMessageKeyWithAuthTagLength() {
|
||||
byte[] illegalMessageKey = new byte[16 + 15]; // too short
|
||||
byte[] cipherTextWithoutAuthTag = new byte[35]; // ok
|
||||
|
||||
OmemoMessageBuilder.moveAuthTag(messageKey, cipherTextWithAuthTag, illegalMessageKey, cipherTextWithoutAuthTag);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testCheckIllegalCipherTextWithoutAuthTagLength() {
|
||||
byte[] messageKeyWithAuthTag = new byte[16 + 16]; // ok
|
||||
byte[] illegalCipherTextWithoutAuthTag = new byte[39]; // too long
|
||||
|
||||
OmemoMessageBuilder.moveAuthTag(messageKey, cipherTextWithAuthTag, messageKeyWithAuthTag, illegalCipherTextWithoutAuthTag);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue