2017-06-02 12:26:37 +02:00
|
|
|
/**
|
|
|
|
*
|
2018-06-13 12:29:16 +02:00
|
|
|
* Copyright Paul Schaub
|
2017-06-02 12:26:37 +02:00
|
|
|
*
|
2018-06-13 12:29:16 +02:00
|
|
|
* 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
|
2017-06-02 12:26:37 +02:00
|
|
|
*
|
2018-06-13 12:29:16 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-06-02 12:26:37 +02:00
|
|
|
*
|
2018-06-13 12:29:16 +02:00
|
|
|
* 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.
|
2017-06-02 12:26:37 +02:00
|
|
|
*/
|
2018-06-13 12:29:16 +02:00
|
|
|
package org.jivesoftware.smackx.omemo;
|
2017-06-02 12:26:37 +02:00
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import static junit.framework.TestCase.assertEquals;
|
|
|
|
import static junit.framework.TestCase.assertNotNull;
|
2018-06-13 12:29:16 +02:00
|
|
|
import static junit.framework.TestCase.assertNull;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2018-06-13 12:29:16 +02:00
|
|
|
import java.io.IOException;
|
2017-06-14 17:12:43 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2018-06-13 12:29:16 +02:00
|
|
|
import java.util.TreeMap;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2017-06-02 12:26:37 +02:00
|
|
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
|
|
|
import org.jivesoftware.smack.test.util.TestUtils;
|
2018-06-13 12:29:16 +02:00
|
|
|
import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl;
|
2017-06-02 12:26:37 +02:00
|
|
|
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
|
|
|
|
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
|
|
|
|
import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider;
|
2018-06-13 12:29:16 +02:00
|
|
|
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
|
|
|
|
import org.jivesoftware.smackx.omemo.util.OmemoKeyUtil;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2017-06-02 12:26:37 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
|
2018-06-13 12:29:16 +02:00
|
|
|
public abstract class OmemoKeyUtilTest<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_ECPub, T_Bundle>
|
|
|
|
extends SmackTestSuite {
|
2017-06-02 12:26:37 +02:00
|
|
|
|
2018-06-13 12:29:16 +02:00
|
|
|
protected OmemoKeyUtil<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_ECPub, T_Bundle> keyUtil;
|
|
|
|
|
|
|
|
public OmemoKeyUtilTest(OmemoKeyUtil<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_ECPub, T_Bundle> keyUtil) {
|
|
|
|
this.keyUtil = keyUtil;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void test() {
|
2017-06-02 12:26:37 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void identityKeyPairFromNullBytesReturnsNull() throws CorruptedOmemoKeyException {
|
|
|
|
assertNull(keyUtil.identityKeyPairFromBytes(null));
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void identityKeyFromNullBytesReturnsNull() throws CorruptedOmemoKeyException {
|
|
|
|
assertNull(keyUtil.identityKeyFromBytes(null));
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void preKeyFromNullBytesReturnsNull() throws IOException {
|
|
|
|
assertNull(keyUtil.preKeyFromBytes(null));
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void ellipticCurvePublicKeyFromNullBytesReturnsNull() throws CorruptedOmemoKeyException {
|
|
|
|
assertNull(keyUtil.ellipticCurvePublicKeyFromBytes(null));
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void signedPreKeyFromNullBytesReturnsNull() throws IOException {
|
|
|
|
assertNull(keyUtil.signedPreKeyFromBytes(null));
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void signedPreKeyPublicFromNullBytesReturnsNull() throws CorruptedOmemoKeyException {
|
|
|
|
assertNull(keyUtil.signedPreKeyPublicFromBytes(null));
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void rawSessionFromNullBytesReturnsNull() throws IOException {
|
|
|
|
assertNull(keyUtil.rawSessionFromBytes(null));
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-06-13 12:29:16 +02:00
|
|
|
public void parsedBundlesDoNotContainNullValues() throws Exception {
|
2017-06-02 12:26:37 +02:00
|
|
|
OmemoDevice device = new OmemoDevice(JidCreate.bareFrom("test@test.tld"), 1337);
|
|
|
|
String bundleXML = "<bundle xmlns='eu.siacs.conversations.axolotl'><signedPreKeyPublic signedPreKeyId='1'>BYKq4s6+plpjAAuCnGO+YFpLP71tMUPgj9ZZmkMSko4E</signedPreKeyPublic><signedPreKeySignature>TYMUtzWpc5USMCXStUrCbXFeHTOX3xkBTrU6/MuE/16s4ql1vRN0+JLtYPgZtTm3hb2dHwLA5BUzeTRGjSZwig==</signedPreKeySignature><identityKey>BY3AYRje4YBA6W4uuAXYNKzbII/UJbw7qE8kWHI15eti</identityKey><prekeys><preKeyPublic preKeyId='1'>BbzKUJbnqYW19h2dWCyLMbYEpF8r477Ukv9wqMayERQE</preKeyPublic><preKeyPublic preKeyId='2'>Beit9Pz31QxklV69BZ0qIxktnUO5TYAgHacFWDYsDnhd</preKeyPublic><preKeyPublic preKeyId='3'>BSlbqC8nOpG4TMqvZmCPr6TCPNRcuuoO8Fp2rLGwLFYz</preKeyPublic><preKeyPublic preKeyId='4'>BWYsJTsJLtmOgChiz4ilS/cgoEptnfv87tuvq5VpZFV+</preKeyPublic><preKeyPublic preKeyId='5'>BY/xq67AkvgIaUO1NbROJeG+r6CcpzByoKvpIaPYyaw/</preKeyPublic><preKeyPublic preKeyId='6'>BVRkNWaoocepKEqah95F1DG/uTE1iNEgIZ40wnGd39g/</preKeyPublic><preKeyPublic preKeyId='7'>BWMI2ivYBIziOiJsnxJHmiUNN1GcPs3vP/E4vn7hu10B</preKeyPublic><preKeyPublic preKeyId='8'>Bd7QSMnxJULdKHohRhxUW/DVVRhdaY9SSX16j+CJF8Yd</preKeyPublic><preKeyPublic preKeyId='9'>BSgQ8NXIkq9fZrtYEdV6qkz5EK7YXVRAiIAFaaDuwUZH</preKeyPublic><preKeyPublic preKeyId='10'>Bf9Q2r9P4P15GvIiaHWTEU5gLyk/A8ys6Pzz01pLuu9Z</preKeyPublic><preKeyPublic preKeyId='11'>BVU6/JKCXqaNa4ApbPFxYExxKuQKuRctk8a1brNcRbJU</preKeyPublic><preKeyPublic preKeyId='12'>BfFGHormRpE7x92Eo3IcZcyhxa1//lKyLCNLdlL5Gg1P</preKeyPublic><preKeyPublic preKeyId='13'>Bd/Je4PdYYJy+6gXrcy7CRqDxBHVgPKN9AOiGxpRX7gk</preKeyPublic><preKeyPublic preKeyId='14'>BVtdD2xyJnxPYNJPCT7sYdCXAoD7pMLgf27Dj0dU9vU3</preKeyPublic><preKeyPublic preKeyId='15'>BX41BkuSp/qGYDlEzsuE5Tlia1IjzmYsiZRcjAp8D2tq</preKeyPublic><preKeyPublic preKeyId='16'>BRY9W9zotVhB7DV2s/I7RYFzzg/Rok0AjU6ODs+iBUtF</preKeyPublic><preKeyPublic preKeyId='17'>Bb4DW8bURvMuh21PzHGqQlQm6eaI2S4pPLD482yV65IU</preKeyPublic><preKeyPublic preKeyId='18'>BSFOrkueqrJDACBIUDpaYiOV51fUuFit4dGYYkvV3Sty</preKeyPublic><preKeyPublic preKeyId='19'>BT402/OG5FLw2jt+cpYepykpoRVPbI+bWcUx42CqSlwx</preKeyPublic><preKeyPublic preKeyId='20'>BeMDEcZ23jnocObmU+esIhAGUvEVCyeiqq+n29Ex38Fw</preKeyPublic><preKeyPublic preKeyId='21'>BYUDDsKjORZTuZ1ImIIcwhL2peK1K+kTS+QhqCufoIRJ</preKeyPublic><preKeyPublic preKeyId='22'>BcC/x3Q3zZKv2DKaZlTWpM2Qzg8UogXJ2MmyKQzNI6RJ</preKeyPublic><preKeyPublic preKeyId='23'>Bad8sDrpoVujQTlenKtSfc7JbWlXq5MGDb71q+5DCo88</preKeyPublic><preKeyPublic preKeyId='24'>BYlAA5ZyhfiKLFE/U6lufiokNmQjGYP5eMCKhZsuv9BX</preKeyPublic><preKeyPublic preKeyId='25'>BbK+LNKsLizmJtd6iEd+QUDdBEgmxIylkTyAS2gxghEH</preKeyPublic><preKeyPublic preKeyId='26'>BZ+9oZGHWkRJXPnzT54+UPhQY0vpUdzGltMvneZHqfML</preKeyPublic><preKeyPublic preKeyId='27'>BRRXzcCruX3Gb+kbBodA9OaHcEx/XYT3dpwKK6hx8mYf</preKeyPublic><preKeyPublic preKeyId='28'>BTgeei2VCoKk3dBG0FP45UjDoJBV9wQiDn2pW9xwTMkS</preKeyPublic><preKeyPublic preKeyId='29'>BZHFWmtevdvuYAbMOpQ7nAAdv+oJxY+A7GFi2jU/PftP</preKeyPublic><preKeyPublic preKeyId='30'>BRn4+vobphaBHjOl4gYrVIPHEGMvsn63pbAVgdx69XQR</preKeyPublic><preKeyPublic preKeyId='31'>BaUv1tnXFTkJ2jiFT0vlUjH9upOASZHN4EmXGX9n9UAc</preKeyPublic><preKeyPublic preKeyId='32'>BU+13hmRR2dkuIqBKxItFFaIdnaAti3beOnmezR+/VtW</preKeyPublic><preKeyPublic preKeyId='33'>BbmmB27Q1B72qhxxW++CyrNHCy0UwiAOdkKOBUKCkyZ0</preKeyPublic><preKeyPublic preKeyId='34'>BemHNdH5VhufFn9n4qu6e1pVyYjn47ivQy1xHmQL6eh1</preKeyPublic><preKeyPublic preKeyId='35'>BSnbvvDgCRGpu/SkapLOe66hxxeJKw7U160d6vxUkYM6</preKeyPublic><preKeyPublic preKeyId='36'>BVaUjCB5ZhooG2umXa4CVu6BjmNDkkUUM19pzangbfEU</preKeyPublic><preKeyPublic preKeyId='37'>BZD+gzgJ4jXxjfJtMMuWvHJmr/f5vJ+u7vhH4y7KjYM3</preKeyPublic><preKeyPublic preKeyId='38'>BW3zmMGSm5jhMTpSjT8u0dsDnK2pXMRVPTr08xmh7vhJ</preKeyPublic><preKeyPublic preKeyId='39'>BSE7XKChX5zcJrJtoBTAVtUL/gB9iFFb2rE0fKj2b2UQ</preKeyPublic><preKeyPublic preKeyId='40'>BXVao8jlCDAeOMr4thch7T8Gl+7h2OhcihFAOqkmzf9M</preKeyPublic><preKeyPublic preKeyId='41'>BdPqg07COBd2OInhQqc1yCZbixd1CpEbpcG9NjbxGwRU</preKeyPublic><preKeyPublic preKeyId='42'>BTzmunAmQX61OaIlTYdfWQU3VtkVXdiLCcegUIOzg/hw</preKeyPublic><preKeyPublic preKeyId='43'>BfRxST8negQ2vxMQLufVXdOM/U5I
|
2018-06-13 12:29:16 +02:00
|
|
|
OmemoBundleElement_VAxolotl bundle = new OmemoBundleVAxolotlProvider().parse(TestUtils.getParser(bundleXML));
|
|
|
|
HashMap<Integer, T_Bundle> bundles = keyUtil.BUNDLE.bundles(bundle, device);
|
2017-06-02 12:26:37 +02:00
|
|
|
|
|
|
|
assertEquals("There must be 100 bundles in the HashMap.", 100, bundles.size());
|
|
|
|
assertNotNull(keyUtil.BUNDLE.identityKey(bundle));
|
|
|
|
|
|
|
|
Iterator<Integer> it = bundles.keySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
assertNotNull(keyUtil.BUNDLE.preKeyPublic(bundle, it.next()));
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(1, keyUtil.BUNDLE.signedPreKeyId(bundle));
|
|
|
|
assertNotNull(keyUtil.BUNDLE.signedPreKeyPublic(bundle));
|
|
|
|
assertNotNull(keyUtil.BUNDLE.signedPreKeySignature(bundle));
|
|
|
|
}
|
2018-06-13 12:29:16 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void generateOmemoPreKeysIdsMatchAndNoNullValues() {
|
|
|
|
TreeMap<Integer, T_PreKey> pks =
|
|
|
|
keyUtil.generateOmemoPreKeys(1, 20);
|
|
|
|
|
|
|
|
for (int i = 1; i <= 20; i++) {
|
|
|
|
assertEquals("PreKeyIds must correspond the requested ids.", Integer.valueOf(i), pks.firstKey());
|
|
|
|
assertNotNull("All PreKeys must not be null.", pks.get(pks.firstKey()));
|
|
|
|
pks.remove(pks.firstKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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() {
|
|
|
|
OmemoFingerprint fingerprint = new OmemoFingerprint("FFFFFFFFEEEEEEEEDDDDDDDDCCCCCCCCBBBBBBBBAAAAAAAA9999999988888888");
|
|
|
|
String pretty = fingerprint.blocksOf8Chars();
|
|
|
|
assertEquals(pretty, "FFFFFFFF EEEEEEEE DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA 99999999 88888888");
|
|
|
|
}
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|