getKeys() {
+ return new ArrayList<>(keys);
+ }
+
+ @Override
+ public String getElementName() {
+ return ELEMENT;
+ }
+
+ @Override
+ public String getNamespace() {
+ return NAMESPACE;
+ }
+
+ @Override
+ public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
+ XmlStringBuilder sb = new XmlStringBuilder(this, enclosingXmlEnvironment);
+ sb.attribute(ATTR_JID, jid);
+ sb.rightAngleBracket();
+
+ for (OmemoKeyElement_VOmemo k : getKeys()) {
+ sb.append(k);
+ }
+ sb.closeElement(this);
+ return sb;
+ }
+}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoOptOutElement.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoOptOutElement.java
new file mode 100644
index 0000000000..946665b257
--- /dev/null
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/element/OmemoOptOutElement.java
@@ -0,0 +1,70 @@
+/*
+ *
+ * Copyright 2014~2024 Eng Chong Meng
+ *
+ * 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.element;
+
+import org.jivesoftware.smack.packet.XmlElement;
+import org.jivesoftware.smack.packet.XmlEnvironment;
+import org.jivesoftware.smack.util.XmlStringBuilder;
+
+import org.jivesoftware.smackx.omemo.util.OmemoConstants;
+
+public class OmemoOptOutElement implements XmlElement {
+ public static final String ELEMENT = "opt-out";
+ public static final String NAMESPACE = OmemoConstants.OMEMO_NAMESPACE_V_OMEMO;
+
+ public static final String ATTR_REASON = "reason";
+
+ private String reason = "OMEMO chat opt-out.\n Sorry, for compliance reasons I need a permanent, server-side, record of our conversation.";
+
+ public OmemoOptOutElement() {
+ }
+
+ public String getReason() {
+ return reason;
+ }
+
+ public void setReason(String reason) {
+ this.reason = reason;
+ }
+
+ @Override
+ public String getElementName() {
+ return ELEMENT;
+ }
+
+ @Override
+ public String getNamespace() {
+ return NAMESPACE;
+ }
+
+ @Override
+ public XmlStringBuilder toXML(XmlEnvironment enclosingNamespace) {
+ XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace).rightAngleBracket();
+
+ xml.openElement(ATTR_REASON)
+ .append(getReason())
+ .closeElement(ATTR_REASON);
+
+ xml.closeElement(this);
+ return xml;
+ }
+
+ @Override
+ public final String toString() {
+ return "Omemo Opt-Out reason: \n" + reason;
+ }
+}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoCachedDeviceList.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoCachedDeviceList.java
index bac762f99a..4c04cd13f0 100644
--- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoCachedDeviceList.java
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoCachedDeviceList.java
@@ -20,30 +20,33 @@
import java.util.HashSet;
import java.util.Set;
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceElement;
+
/**
- * This class is used to represent device lists of contacts.
- * There are active devices (a set of device ids, which was published with the last device list update)
+ * This class is used to represent devices of contacts.
+ * There are active devices (a set of OmemoDevice elements, which was published with the last device list update)
* and inactive devices (set of devices that once were active, but are not included in recent list updates).
* Both kinds are cached by the client. When a device that was active in the last update is not included in
* a new update, it becomes an inactive device. Vice versa, inactive devices can also become active again, by
* being included in the latest device list update.
*
- * The client ensures, that his own device id is on the list of active devices, as soon as he gets online.
+ * The client ensures, that his own OmemoDevice elements is on the list of active devices, as soon as he gets online.
*
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public class OmemoCachedDeviceList implements Serializable {
private static final long serialVersionUID = 3153579238321261203L;
- private final Set activeDevices;
- private final Set inactiveDevices;
+ private final Set activeDevices;
+ private final Set inactiveDevices;
public OmemoCachedDeviceList() {
this.activeDevices = new HashSet<>();
this.inactiveDevices = new HashSet<>();
}
- public OmemoCachedDeviceList(Set activeDevices, Set inactiveDevices) {
+ public OmemoCachedDeviceList(Set activeDevices, Set inactiveDevices) {
this();
this.activeDevices.addAll(activeDevices);
this.inactiveDevices.addAll(inactiveDevices);
@@ -59,7 +62,7 @@ public OmemoCachedDeviceList(OmemoCachedDeviceList original) {
*
* @return active devices
*/
- public Set getActiveDevices() {
+ public Set getActiveDevices() {
return activeDevices;
}
@@ -70,7 +73,7 @@ public Set getActiveDevices() {
*
* @return inactive devices
*/
- public Set getInactiveDevices() {
+ public Set getInactiveDevices() {
return inactiveDevices;
}
@@ -79,8 +82,8 @@ public Set getInactiveDevices() {
*
* @return all devices
*/
- public Set getAllDevices() {
- Set all = new HashSet<>();
+ public Set getAllDevices() {
+ Set all = new HashSet<>();
all.addAll(activeDevices);
all.addAll(inactiveDevices);
return all;
@@ -92,7 +95,7 @@ public Set getAllDevices() {
*
* @param deviceListUpdate received device list update
*/
- public void merge(Set deviceListUpdate) {
+ public void merge(Set deviceListUpdate) {
inactiveDevices.addAll(activeDevices);
activeDevices.clear();
activeDevices.addAll(deviceListUpdate);
@@ -102,16 +105,16 @@ public void merge(Set deviceListUpdate) {
/**
* Add a device to the list of active devices and remove it from inactive.
*
- * @param deviceId deviceId that will be added
+ * @param device deviceId that will be added
*/
- public void addDevice(int deviceId) {
- activeDevices.add(deviceId);
- inactiveDevices.remove(deviceId);
+ public void addDevice(OmemoDeviceElement device) {
+ activeDevices.add(device);
+ inactiveDevices.remove(device);
}
- public void addInactiveDevice(int deviceId) {
- activeDevices.remove(deviceId);
- inactiveDevices.add(deviceId);
+ public void addInactiveDevice(OmemoDeviceElement device) {
+ activeDevices.remove(device);
+ inactiveDevices.add(device);
}
/**
@@ -120,22 +123,22 @@ public void addInactiveDevice(int deviceId) {
* @param deviceId id
* @return true or false
*/
- public boolean contains(int deviceId) {
+ public boolean contains(OmemoDeviceElement deviceId) {
return activeDevices.contains(deviceId) || inactiveDevices.contains(deviceId);
}
- public boolean isActive(int deviceId) {
+ public boolean isActive(OmemoDeviceElement deviceId) {
return getActiveDevices().contains(deviceId);
}
@Override
public String toString() {
String out = "active: [";
- for (int id : activeDevices) {
+ for (OmemoDeviceElement id : activeDevices) {
out += id + " ";
}
out += "] inacitve: [";
- for (int id : inactiveDevices) {
+ for (OmemoDeviceElement id : inactiveDevices) {
out += id + " ";
}
out += "]";
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoDevice.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoDevice.java
index 96dfc323b1..9ef09fb5ff 100644
--- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoDevice.java
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/internal/OmemoDevice.java
@@ -24,6 +24,7 @@
* Class that combines a BareJid and a deviceId.
*
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public class OmemoDevice {
private final BareJid jid;
@@ -72,16 +73,19 @@ public boolean equals(Object other) {
@Override
public int hashCode() {
- Integer i;
+ int i;
i = jid.hashCode() + deviceId;
- return i.hashCode();
+ return Integer.hashCode(i);
}
/**
* Return the name of the PubSub {@link org.jivesoftware.smackx.pubsub.LeafNode} of this device.
+ *
+ * @param vOmemo2 omemo:2 option state.
+ *
* @return node name.
*/
- public String getBundleNodeName() {
- return OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(getDeviceId());
+ public String getBundleNodeName(boolean vOmemo2) {
+ return OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(getDeviceId(), vOmemo2);
}
}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoBundleVAxolotlProvider.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoBundleVAxolotlProvider.java
index 468b07eb71..ad7e259cc5 100644
--- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoBundleVAxolotlProvider.java
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoBundleVAxolotlProvider.java
@@ -16,16 +16,9 @@
*/
package org.jivesoftware.smackx.omemo.provider;
-import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.IDENTITY_KEY;
-import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEYS;
-import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEY_ID;
-import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEY_PUB;
-import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_ID;
-import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_PUB;
-import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_SIG;
-
import java.io.IOException;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
@@ -37,9 +30,10 @@
import org.jxmpp.JxmppContext;
/**
- * Smack ExtensionProvider that parses OMEMO bundle element into OmemoBundleElement objects.
+ * Smack ExtensionProvider that parses OMEMO bundle element into OmemoBundleElement objects for Axolotl namespace.
*
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public class OmemoBundleVAxolotlProvider extends ExtensionElementProvider {
@Override
@@ -50,7 +44,8 @@ public OmemoBundleElement_VAxolotl parse(XmlPullParser parser, int initialDepth,
String signedPreKey = null;
String signedPreKeySignature = null;
String identityKey = null;
- HashMap preKeys = new HashMap<>();
+ // Preserve the order of the received preKeyPublic's in prekeys.
+ Map preKeys = new LinkedHashMap<>();
outerloop: while (true) {
XmlPullParser.Event tag = parser.next();
@@ -59,9 +54,9 @@ public OmemoBundleElement_VAxolotl parse(XmlPullParser parser, int initialDepth,
String name = parser.getName();
final int attributeCount = parser.getAttributeCount();
//
- if (name.equals(SIGNED_PRE_KEY_PUB)) {
+ if (name.equals(OmemoBundleElement_VAxolotl.SIGNED_PRE_KEY_PUB)) {
for (int i = 0; i < attributeCount; i++) {
- if (parser.getAttributeName(i).equals(SIGNED_PRE_KEY_ID)) {
+ if (parser.getAttributeName(i).equals(OmemoBundleElement_VAxolotl.SIGNED_PRE_KEY_ID)) {
int id = Integer.parseInt(parser.getAttributeValue(i));
signedPreKey = parser.nextText();
signedPreKeyId = id;
@@ -69,21 +64,21 @@ public OmemoBundleElement_VAxolotl parse(XmlPullParser parser, int initialDepth,
}
}
//
- else if (name.equals(SIGNED_PRE_KEY_SIG)) {
+ else if (name.equals(OmemoBundleElement_VAxolotl.SIGNED_PRE_KEY_SIG)) {
signedPreKeySignature = parser.nextText();
}
//
- else if (name.equals(IDENTITY_KEY)) {
+ else if (name.equals(OmemoBundleElement_VAxolotl.IDENTITY_KEY)) {
identityKey = parser.nextText();
}
//
- else if (name.equals(PRE_KEYS)) {
+ else if (name.equals(OmemoBundleElement_VAxolotl.PRE_KEYS)) {
inPreKeys = true;
}
//
- else if (inPreKeys && name.equals(PRE_KEY_PUB)) {
+ else if (inPreKeys && name.equals(OmemoBundleElement_VAxolotl.PRE_KEY_PUB)) {
for (int i = 0; i < attributeCount; i++) {
- if (parser.getAttributeName(i).equals(PRE_KEY_ID)) {
+ if (parser.getAttributeName(i).equals(OmemoBundleElement_VAxolotl.PRE_KEY_ID)) {
preKeys.put(Integer.parseInt(parser.getAttributeValue(i)),
parser.nextText());
}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoBundleVOmemoProvider.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoBundleVOmemoProvider.java
new file mode 100644
index 0000000000..380f583cb1
--- /dev/null
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoBundleVOmemoProvider.java
@@ -0,0 +1,101 @@
+/*
+ *
+ * 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.provider;
+
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.jivesoftware.smack.packet.XmlEnvironment;
+import org.jivesoftware.smack.provider.ExtensionElementProvider;
+import org.jivesoftware.smack.xml.XmlPullParser;
+import org.jivesoftware.smack.xml.XmlPullParserException;
+
+import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VOmemo;
+
+import org.jxmpp.JxmppContext;
+
+/**
+ * Smack ExtensionProvider that parses OMEMO bundle element into OmemoBundleElement objects for omemo:2 namespace.
+ *
+ * @author Paul Schaub
+ * @author Eng Chong Meng
+ */
+public class OmemoBundleVOmemoProvider extends ExtensionElementProvider {
+ @Override
+ public OmemoBundleElement_VOmemo parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
+ boolean inPreKeys = false;
+
+ int signedPreKeyId = -1;
+ String signedPreKey = null;
+ String signedPreKeySignature = null;
+ String identityKey = null;
+ // Preserve the order of the received pk's in prekeys.
+ Map preKeys = new LinkedHashMap<>();
+
+ outerloop:
+ while (true) {
+ XmlPullParser.Event tag = parser.next();
+ switch (tag) {
+ case START_ELEMENT:
+ String name = parser.getName();
+ final int attributeCount = parser.getAttributeCount();
+ //
+ if (name.equals(OmemoBundleElement_VOmemo.SIGNED_PRE_KEY)) {
+ for (int i = 0; i < attributeCount; i++) {
+ if (parser.getAttributeName(i).equals(OmemoBundleElement_VOmemo.KEY_ID)) {
+ int id = Integer.parseInt(parser.getAttributeValue(i));
+ signedPreKey = parser.nextText();
+ signedPreKeyId = id;
+ }
+ }
+ }
+ //
+ else if (name.equals(OmemoBundleElement_VOmemo.SIGNED_PRE_KEY_SIG)) {
+ signedPreKeySignature = parser.nextText();
+ }
+ //
+ else if (name.equals(OmemoBundleElement_VOmemo.IDENTITY_KEY)) {
+ identityKey = parser.nextText();
+ }
+ //
+ else if (name.equals(OmemoBundleElement_VOmemo.PRE_KEYS)) {
+ inPreKeys = true;
+ }
+ // b64/encoded/data
+ else if (inPreKeys && name.equals(OmemoBundleElement_VOmemo.PRE_KEY)) {
+ for (int i = 0; i < attributeCount; i++) {
+ if (parser.getAttributeName(i).equals(OmemoBundleElement_VOmemo.KEY_ID)) {
+ preKeys.put(Integer.parseInt(parser.getAttributeValue(i)),
+ parser.nextText());
+ }
+ }
+ }
+ break;
+ case END_ELEMENT:
+ if (parser.getDepth() == initialDepth) {
+ break outerloop;
+ }
+ break;
+ default:
+ // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
+ break;
+ }
+ }
+ return new OmemoBundleElement_VOmemo(signedPreKeyId, signedPreKey, signedPreKeySignature, identityKey, preKeys);
+ }
+}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoDeviceListVAxolotlProvider.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoDeviceListVAxolotlProvider.java
index 4a92dbc69c..7149aab45d 100644
--- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoDeviceListVAxolotlProvider.java
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoDeviceListVAxolotlProvider.java
@@ -16,9 +16,6 @@
*/
package org.jivesoftware.smackx.omemo.provider;
-import static org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement.DEVICE;
-import static org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement.ID;
-
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
@@ -28,6 +25,7 @@
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceElement;
import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl;
import org.jxmpp.JxmppContext;
@@ -36,22 +34,23 @@
* Smack ExtensionProvider that parses OMEMO device list element into OmemoDeviceListElement objects.
*
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public class OmemoDeviceListVAxolotlProvider extends ExtensionElementProvider {
@Override
public OmemoDeviceListElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
- Set deviceListIds = new HashSet<>();
+ Set deviceListIds = new HashSet<>();
outerloop: while (true) {
XmlPullParser.Event tag = parser.next();
switch (tag) {
case START_ELEMENT:
String name = parser.getName();
- if (name.equals(DEVICE)) {
+ if (name.equals(OmemoDeviceElement.DEVICE)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
- if (parser.getAttributeName(i).equals(ID)) {
- Integer deviceId = Integer.parseInt(parser.getAttributeValue(i));
- deviceListIds.add(deviceId);
+ if (parser.getAttributeName(i).equals(OmemoDeviceElement.ATTR_ID)) {
+ int deviceId = Integer.parseInt(parser.getAttributeValue(i));
+ deviceListIds.add(new OmemoDeviceElement(deviceId));
}
}
}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoDeviceListVOmemoProvider.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoDeviceListVOmemoProvider.java
new file mode 100644
index 0000000000..39a73e1f7d
--- /dev/null
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoDeviceListVOmemoProvider.java
@@ -0,0 +1,79 @@
+/*
+ *
+ * Copyright 2017 Paul Schaub, 2021-2025 Florian Schmaus
+ *
+ * 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.provider;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.jivesoftware.smack.packet.XmlEnvironment;
+import org.jivesoftware.smack.provider.ExtensionElementProvider;
+import org.jivesoftware.smack.xml.XmlPullParser;
+import org.jivesoftware.smack.xml.XmlPullParserException;
+
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceElement;
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VOmemo;
+
+import org.jxmpp.JxmppContext;
+
+/**
+ * Smack ExtensionProvider that parses OMEMO device list element into OmemoDeviceListElement objects.
+ *
+ * @author Eng Chong Meng
+ */
+public class OmemoDeviceListVOmemoProvider extends ExtensionElementProvider {
+
+ @Override
+ public OmemoDeviceListElement_VOmemo parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
+ Set deviceListIds = new HashSet<>();
+ outerloop:
+ while (true) {
+ XmlPullParser.Event tag = parser.next();
+ switch (tag) {
+ case START_ELEMENT:
+ String name = parser.getName();
+ if (name.equals(OmemoDeviceElement.DEVICE)) {
+ int deviceId = -1;
+ String label = null;
+ String labelsig = null;
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ if (parser.getAttributeName(i).equals(OmemoDeviceElement.ATTR_ID)) {
+ deviceId = Integer.parseInt(parser.getAttributeValue(i));
+ }
+ else if (parser.getAttributeName(i).equals(OmemoDeviceElement.ATTR_LABEL)) {
+ label = parser.getAttributeValue(i);
+ }
+ else if (parser.getAttributeName(i).equals(OmemoDeviceElement.ATTR_LABELSIG)) {
+ labelsig = parser.getAttributeValue(i);
+ }
+ }
+ deviceListIds.add(new OmemoDeviceElement(deviceId, label, labelsig));
+ }
+ break;
+ case END_ELEMENT:
+ if (parser.getDepth() == initialDepth) {
+ break outerloop;
+ }
+ break;
+ default:
+ // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
+ break;
+ }
+ }
+ return new OmemoDeviceListElement_VOmemo(deviceListIds);
+ }
+}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoOptOutProvider.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoOptOutProvider.java
new file mode 100644
index 0000000000..fc5b07facd
--- /dev/null
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoOptOutProvider.java
@@ -0,0 +1,62 @@
+/*
+ *
+ * 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.provider;
+
+import java.io.IOException;
+
+import org.jivesoftware.smack.packet.XmlEnvironment;
+import org.jivesoftware.smack.provider.ExtensionElementProvider;
+import org.jivesoftware.smack.xml.XmlPullParser;
+import org.jivesoftware.smack.xml.XmlPullParserException;
+
+import org.jivesoftware.smackx.omemo.element.OmemoOptOutElement;
+
+import org.jxmpp.JxmppContext;
+
+/**
+ * Smack ExtensionProvider that parses OMEMO OptOut reason element.
+ *
+ * @author Eng Chong Meng
+ */
+public class OmemoOptOutProvider extends ExtensionElementProvider {
+ @Override
+ public OmemoOptOutElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
+ OmemoOptOutElement optOutElement = new OmemoOptOutElement();
+
+ outerloop:
+ while (true) {
+ XmlPullParser.Event tag = parser.next();
+ switch (tag) {
+ case START_ELEMENT:
+ String name = parser.getName();
+ if (name.equals(OmemoOptOutElement.ATTR_REASON)) {
+ parser.next();
+ optOutElement.setReason(parser.getText());
+ }
+ break;
+ case END_ELEMENT:
+ if (parser.getDepth() == initialDepth) {
+ break outerloop;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ return optOutElement;
+ }
+}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVAxolotlProvider.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVAxolotlProvider.java
index 9d09a89947..f31db7ce23 100644
--- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVAxolotlProvider.java
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVAxolotlProvider.java
@@ -16,8 +16,6 @@
*/
package org.jivesoftware.smackx.omemo.provider;
-import static org.jivesoftware.smackx.omemo.element.OmemoElement.ATTR_PAYLOAD;
-
import java.io.IOException;
import java.util.ArrayList;
@@ -27,68 +25,73 @@
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
+import org.jivesoftware.smackx.omemo.element.OmemoElement;
import org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl;
import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement;
import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl;
import org.jivesoftware.smackx.omemo.element.OmemoKeyElement;
+import org.jivesoftware.smackx.omemo.element.OmemoKeyElement_VAxolotl;
import org.jxmpp.JxmppContext;
/**
- * Smack ExtensionProvider that parses incoming OMEMO Message element into OmemoMessageElement objects.
+ * Smack ExtensionProvider that parses incoming OMEMO Message element for Axolotl into OmemoMessageElement objects.
*
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public class OmemoVAxolotlProvider extends ExtensionElementProvider {
@Override
public OmemoElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
int sid = -1;
- ArrayList keys = new ArrayList<>();
+ ArrayList keys = new ArrayList<>();
byte[] iv = null;
byte[] payload = null;
- outerloop: while (true) {
+ outerloop:
+ while (true) {
XmlPullParser.Event tag = parser.next();
switch (tag) {
- case START_ELEMENT:
- String name = parser.getName();
- switch (name) {
- case OmemoHeaderElement.ELEMENT:
- for (int i = 0; i < parser.getAttributeCount(); i++) {
- if (parser.getAttributeName(i).equals(OmemoHeaderElement.ATTR_SID)) {
- sid = Integer.parseInt(parser.getAttributeValue(i));
- }
- }
- break;
- case OmemoKeyElement.ELEMENT:
- boolean prekey = false;
- int rid = -1;
- for (int i = 0; i < parser.getAttributeCount(); i++) {
- if (parser.getAttributeName(i).equals(OmemoKeyElement.ATTR_PREKEY)) {
- prekey = Boolean.parseBoolean(parser.getAttributeValue(i));
- } else if (parser.getAttributeName(i).equals(OmemoKeyElement.ATTR_RID)) {
- rid = Integer.parseInt(parser.getAttributeValue(i));
- }
- }
- keys.add(new OmemoKeyElement(Base64.decode(parser.nextText()), rid, prekey));
- break;
- case OmemoHeaderElement.ATTR_IV:
- iv = Base64.decode(parser.nextText());
- break;
- case ATTR_PAYLOAD:
- payload = Base64.decode(parser.nextText());
- break;
+ case START_ELEMENT:
+ String name = parser.getName();
+ switch (name) {
+ case OmemoHeaderElement.ELEMENT:
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ if (parser.getAttributeName(i).equals(OmemoHeaderElement.ATTR_SID)) {
+ sid = Integer.parseInt(parser.getAttributeValue(i));
+ }
}
break;
- case END_ELEMENT:
- if (parser.getDepth() == initialDepth) {
- break outerloop;
+ case OmemoKeyElement_VAxolotl.ELEMENT:
+ boolean prekey = false;
+ int rid = -1;
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ if (parser.getAttributeName(i).equals(OmemoKeyElement_VAxolotl.ATTR_PREKEY)) {
+ prekey = Boolean.parseBoolean(parser.getAttributeValue(i));
+ }
+ else if (parser.getAttributeName(i).equals(OmemoKeyElement.ATTR_RID)) {
+ rid = Integer.parseInt(parser.getAttributeValue(i));
+ }
}
+ keys.add(new OmemoKeyElement_VAxolotl(Base64.decode(parser.nextText()), rid, prekey));
+ break;
+ case OmemoHeaderElement.ATTR_IV:
+ iv = Base64.decode(parser.nextText());
break;
- default:
- // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
+ case OmemoElement.ATTR_PAYLOAD:
+ payload = Base64.decode(parser.nextText());
break;
+ }
+ break;
+ case END_ELEMENT:
+ if (parser.getDepth() == initialDepth) {
+ break outerloop;
+ }
+ break;
+ default:
+ // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
+ break;
}
}
OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(sid, keys, iv);
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVOmemoProvider.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVOmemoProvider.java
new file mode 100644
index 0000000000..8254b5532c
--- /dev/null
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/provider/OmemoVOmemoProvider.java
@@ -0,0 +1,134 @@
+/*
+ *
+ * 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.provider;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jivesoftware.smack.packet.XmlEnvironment;
+import org.jivesoftware.smack.provider.ExtensionElementProvider;
+import org.jivesoftware.smack.util.stringencoder.Base64;
+import org.jivesoftware.smack.xml.XmlPullParser;
+import org.jivesoftware.smack.xml.XmlPullParserException;
+
+import org.jivesoftware.smackx.omemo.element.OmemoElement;
+import org.jivesoftware.smackx.omemo.element.OmemoElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement;
+import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoKeyElement;
+import org.jivesoftware.smackx.omemo.element.OmemoKeyElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoKeysElement_VOmemo;
+
+import org.jxmpp.JxmppContext;
+
+/**
+ * Smack ExtensionProvider that parses incoming OMEMO Message element for omemo:2 into OmemoMessageElement objects.
+ *
+ * @author Paul Schaub
+ * @author Eng Chong Meng
+ */
+public class OmemoVOmemoProvider extends ExtensionElementProvider {
+
+ @Override
+ public OmemoElement_VOmemo parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
+ int sid = -1;
+
+ List omemoKeys = new ArrayList<>();
+ byte[] iv = null;
+ byte[] payload = null;
+
+ outerloop:
+ while (true) {
+ XmlPullParser.Event tag = parser.next();
+ switch (tag) {
+ case START_ELEMENT:
+ String name = parser.getName();
+ switch (name) {
+ case OmemoHeaderElement.ELEMENT:
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ if (parser.getAttributeName(i).equals(OmemoHeaderElement.ATTR_SID)) {
+ sid = Integer.parseInt(parser.getAttributeValue(i));
+ }
+ }
+ break;
+ case OmemoKeysElement_VOmemo.ELEMENT:
+ String jid = parser.getAttributeValue(OmemoKeysElement_VOmemo.ATTR_JID);
+ List keys = parseKeyChildElement(parser, parser.getDepth());
+ OmemoKeysElement_VOmemo keysElement = new OmemoKeysElement_VOmemo(jid, keys);
+ omemoKeys.add(keysElement);
+ break;
+ case OmemoHeaderElement.ATTR_IV:
+ iv = Base64.decode(parser.nextText());
+ break;
+ case OmemoElement.ATTR_PAYLOAD:
+ payload = Base64.decode(parser.nextText());
+ break;
+ }
+ break;
+ case END_ELEMENT:
+ if (parser.getDepth() == initialDepth) {
+ break outerloop;
+ }
+ break;
+ default:
+ // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
+ break;
+ }
+ }
+
+ OmemoHeaderElement_VOmemo header = new OmemoHeaderElement_VOmemo(sid, omemoKeys, iv);
+ return new OmemoElement_VOmemo(header, payload);
+ }
+
+ public static List parseKeyChildElement(XmlPullParser parser, int initialDepth)
+ throws XmlPullParserException, IOException {
+ List keyElements = new ArrayList<>();
+
+ outerloop:
+ while (true) {
+ XmlPullParser.Event tag = parser.next();
+ switch (tag) {
+ case START_ELEMENT:
+ String name = parser.getName();
+ if (name.equals(OmemoKeyElement.ELEMENT)) {
+ boolean prekey = false;
+ int rid = -1;
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ if (parser.getAttributeName(i).equals(OmemoKeyElement_VOmemo.ATTR_PREKEY)) {
+ prekey = Boolean.parseBoolean(parser.getAttributeValue(i));
+ }
+ else if (parser.getAttributeName(i).equals(OmemoKeyElement.ATTR_RID)) {
+ rid = Integer.parseInt(parser.getAttributeValue(i));
+ }
+ }
+ keyElements.add(new OmemoKeyElement_VOmemo(Base64.decode(parser.nextText()), rid, prekey));
+ }
+ break;
+ case END_ELEMENT:
+ if (parser.getDepth() == initialDepth) {
+ break outerloop;
+ }
+ break;
+ default:
+ // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
+ break;
+ }
+ }
+ return keyElements;
+ }
+}
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java
index 6425bb94b9..ea9cf39c14 100644
--- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoConstants.java
@@ -17,21 +17,25 @@
package org.jivesoftware.smackx.omemo.util;
/**
- * Some constants related to OMEMO.
+ * Some constants related to OMEMO for both Axolotl and omemo:2 namespaces.
*
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public final class OmemoConstants {
-
/**
* Omemo related namespace.
*/
public static final String OMEMO_NAMESPACE_V_AXOLOTL = "eu.siacs.conversations.axolotl";
+ public static final String OMEMO_NAMESPACE_V_OMEMO = "urn:xmpp:omemo:2";
public static final String OMEMO = "OMEMO";
// PubSub Node names
- public static final String PEP_NODE_DEVICE_LIST = OMEMO_NAMESPACE_V_AXOLOTL + ".devicelist";
- public static final String PEP_NODE_BUNDLES = OMEMO_NAMESPACE_V_AXOLOTL + ".bundles";
+ public static final String PEP_NODE_DEVICES_V_AXOLOTL = OMEMO_NAMESPACE_V_AXOLOTL + ".devicelist";
+ public static final String PEP_NODE_BUNDLES_V_AXOLOTL = OMEMO_NAMESPACE_V_AXOLOTL + ".bundles";
+
+ public static final String PEP_NODE_DEVICES_V_OMEMO = OMEMO_NAMESPACE_V_OMEMO + ":devices";
+ public static final String PEP_NODE_BUNDLES_V_OMEMO = OMEMO_NAMESPACE_V_OMEMO + ":bundles";
/**
* How many preKeys do we want to publish?
@@ -42,13 +46,23 @@ public final class OmemoConstants {
* Return the node name of the PEP node containing the device bundle of the device with device id deviceId.
*
* @param deviceId id of the device
+ * @param vOmemo2 omemo:2 option state.
+ *
* @return node name of the devices bundle node
*/
- public static String PEP_NODE_BUNDLE_FROM_DEVICE_ID(int deviceId) {
- return PEP_NODE_BUNDLES + ":" + deviceId;
+ public static String PEP_NODE_BUNDLE_FROM_DEVICE_ID(int deviceId, boolean vOmemo2) {
+ return vOmemo2 ? PEP_NODE_BUNDLES_V_OMEMO : PEP_NODE_BUNDLES_V_AXOLOTL + ":" + deviceId;
}
- public static final String BODY_OMEMO_HINT = "I sent you an OMEMO encrypted message but your client doesn't seem to support that. Find more information on https://conversations.im/omemo";
+ public static String getOmemoNS(boolean vOmemo2) {
+ return vOmemo2 ? OmemoConstants.PEP_NODE_DEVICES_V_OMEMO : OmemoConstants.PEP_NODE_DEVICES_V_AXOLOTL;
+ }
+
+ public static final String BODY_OMEMO_HINT = "I sent you an OMEMO '%s' encrypted message but your client doesn't seem to support that. Find more information on https://conversations.im/omemo";
+
+ public static String getOmemoHint(boolean vOmemo2) {
+ return String.format(BODY_OMEMO_HINT, vOmemo2 ? OmemoConstants.OMEMO_NAMESPACE_V_OMEMO : OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL);
+ }
/**
* Information about the keys used for message encryption.
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java
index 0e57f82205..2d1c4379c5 100644
--- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoMessageBuilder.java
@@ -24,6 +24,7 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
+import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
@@ -36,8 +37,12 @@
import org.jivesoftware.smackx.omemo.OmemoService;
import org.jivesoftware.smackx.omemo.element.OmemoElement;
import org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoElement_VOmemo;
import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl;
-import org.jivesoftware.smackx.omemo.element.OmemoKeyElement;
+import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoKeyElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoKeyElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoKeysElement_VOmemo;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException;
import org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException;
@@ -50,18 +55,20 @@
/**
- * Class used to build OMEMO messages.
+ * Class used to build OMEMO messages for both Axolotl and omemo:2 namespaces.
*
* @param IdentityKeyPair class
- * @param IdentityKey class
- * @param PreKey class
+ * @param IdentityKey class
+ * @param PreKey class
* @param SignedPreKey class
- * @param Session class
- * @param Address class
- * @param Elliptic Curve PublicKey class
- * @param Bundle class
- * @param Cipher class
+ * @param Session class
+ * @param Address class
+ * @param Elliptic Curve PublicKey class
+ * @param Bundle class
+ * @param Cipher class
+ *
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public class OmemoMessageBuilder {
@@ -73,7 +80,8 @@ public class OmemoMessageBuilder keys = new ArrayList<>();
+ private final ArrayList keys = new ArrayList<>();
+ private final ArrayList keysElement = new ArrayList<>();
/**
* Create an OmemoMessageBuilder.
@@ -93,11 +101,11 @@ public class OmemoMessageBuilder ratchet,
- byte[] aesKey,
- byte[] iv,
- String message)
+ OmemoTrustCallback callback,
+ OmemoRatchet ratchet,
+ byte[] aesKey,
+ byte[] iv,
+ String message)
throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException,
IllegalBlockSizeException,
InvalidAlgorithmParameterException {
@@ -125,9 +133,9 @@ public OmemoMessageBuilder(OmemoDevice userDevice,
* @throws InvalidAlgorithmParameterException if the provided arguments are invalid.
*/
public OmemoMessageBuilder(OmemoDevice userDevice,
- OmemoTrustCallback callback,
- OmemoRatchet ratchet,
- String message)
+ OmemoTrustCallback callback,
+ OmemoRatchet ratchet,
+ String message)
throws NoSuchPaddingException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException,
InvalidAlgorithmParameterException {
this(userDevice, callback, ratchet, generateKey(KEYTYPE, KEYLENGTH), generateIv(), message);
@@ -176,9 +184,9 @@ private void setMessage(String message)
* @param cipherTextWithoutAuthTag destination cipherText without authTag
*/
static void moveAuthTag(byte[] messageKey,
- byte[] cipherText,
- byte[] messageKeyWithAuthTag,
- byte[] cipherTextWithoutAuthTag) {
+ byte[] cipherText,
+ byte[] messageKeyWithAuthTag,
+ byte[] cipherTextWithoutAuthTag) {
// Check dimensions of arrays
if (messageKeyWithAuthTag.length != messageKey.length + 16) {
throw new IllegalArgumentException("Length of messageKeyWithAuthTag must be length of messageKey + " +
@@ -200,15 +208,16 @@ static void moveAuthTag(byte[] messageKey,
* Add a new recipient device to the message.
*
* @param contactsDevice device of the recipient
+ * @param vOmemo2 omemo:2 option state.
*
* @throws NoIdentityKeyException if we have no identityKey of that device. Can be fixed by fetching and
- * processing the devices bundle.
+ * processing the devices bundle.
* @throws CorruptedOmemoKeyException if the identityKey of that device is corrupted.
* @throws UndecidedOmemoIdentityException if the user hasn't yet decided whether to trust that device or not.
* @throws UntrustedOmemoIdentityException if the user has decided not to trust that device.
* @throws IOException if an I/O error occurred.
*/
- public void addRecipient(OmemoDevice contactsDevice)
+ public void addRecipient(OmemoDevice contactsDevice, boolean vOmemo2)
throws NoIdentityKeyException, CorruptedOmemoKeyException, UndecidedOmemoIdentityException,
UntrustedOmemoIdentityException, IOException {
@@ -217,16 +226,23 @@ public void addRecipient(OmemoDevice contactsDevice)
switch (trustCallback.getTrust(contactsDevice, fingerprint)) {
- case undecided:
- throw new UndecidedOmemoIdentityException(contactsDevice);
-
- case trusted:
- CiphertextTuple encryptedKey = ratchet.doubleRatchetEncrypt(contactsDevice, messageKey);
- keys.add(new OmemoKeyElement(encryptedKey.getCiphertext(), contactsDevice.getDeviceId(), encryptedKey.isPreKeyMessage()));
- break;
+ case undecided:
+ throw new UndecidedOmemoIdentityException(contactsDevice);
- case untrusted:
- throw new UntrustedOmemoIdentityException(contactsDevice, fingerprint);
+ case trusted:
+ CiphertextTuple encryptedKey = ratchet.doubleRatchetEncrypt(contactsDevice, messageKey);
+ if (vOmemo2) {
+ OmemoKeyElement_VOmemo keyElement = new OmemoKeyElement_VOmemo(encryptedKey.getCiphertext(), contactsDevice.getDeviceId(), encryptedKey.isPreKeyMessage());
+ OmemoKeysElement_VOmemo keys = new OmemoKeysElement_VOmemo(contactsDevice.getJid().toString(), Arrays.asList(keyElement));
+ keysElement.add(keys);
+ }
+ else {
+ OmemoKeyElement_VAxolotl keyElement = new OmemoKeyElement_VAxolotl(encryptedKey.getCiphertext(), contactsDevice.getDeviceId(), encryptedKey.isPreKeyMessage());
+ keys.add(keyElement);
+ }
+ break;
+ case untrusted:
+ throw new UntrustedOmemoIdentityException(contactsDevice, fingerprint);
}
}
@@ -234,15 +250,27 @@ public void addRecipient(OmemoDevice contactsDevice)
/**
* Assemble an OmemoMessageElement from the current state of the builder.
*
+ * @param vOmemo2 omemo:2 option state.
+ *
* @return OMEMO element
*/
- public OmemoElement finish() {
- OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(
- userDevice.getDeviceId(),
- keys,
- initializationVector
- );
- return new OmemoElement_VAxolotl(header, ciphertextMessage);
+ public OmemoElement finish(boolean vOmemo2) {
+ if (vOmemo2) {
+ OmemoHeaderElement_VOmemo header = new OmemoHeaderElement_VOmemo(
+ userDevice.getDeviceId(),
+ keysElement,
+ initializationVector
+ );
+ return new OmemoElement_VOmemo(header, ciphertextMessage);
+ }
+ else {
+ OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(
+ userDevice.getDeviceId(),
+ keys,
+ initializationVector
+ );
+ return new OmemoElement_VAxolotl(header, ciphertextMessage);
+ }
}
/**
@@ -250,6 +278,7 @@ public OmemoElement finish() {
*
* @param keyType Key Type
* @param keyLength Key Length in bit
+ *
* @return new AES key
*
* @throws NoSuchAlgorithmException if no such algorithm is available.
diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoOptOutUtil.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoOptOutUtil.java
new file mode 100644
index 0000000000..2dba7952ae
--- /dev/null
+++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/util/OmemoOptOutUtil.java
@@ -0,0 +1,146 @@
+/*
+ *
+ * Copyright 2014~2024 Eng Chong Meng
+ *
+ * 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.io.IOException;
+import java.text.ParseException;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.temporal.ChronoUnit;
+import java.util.Date;
+import java.util.List;
+
+import org.jivesoftware.smack.packet.XmlElement;
+import org.jivesoftware.smack.parsing.SmackParsingException;
+import org.jivesoftware.smack.util.PacketParserUtils;
+import org.jivesoftware.smack.util.StringUtils;
+import org.jivesoftware.smack.xml.XmlPullParser;
+import org.jivesoftware.smack.xml.XmlPullParserException;
+
+import org.jivesoftware.smackx.omemo.OmemoManager;
+import org.jivesoftware.smackx.omemo.OmemoMessage;
+import org.jivesoftware.smackx.omemo.element.OmemoOptOutElement;
+import org.jivesoftware.smackx.stanza_content_encryption.element.AffixElement;
+import org.jivesoftware.smackx.stanza_content_encryption.element.ContentElement;
+import org.jivesoftware.smackx.stanza_content_encryption.element.EnvelopeElement;
+import org.jivesoftware.smackx.stanza_content_encryption.element.FromAffixElement;
+import org.jivesoftware.smackx.stanza_content_encryption.element.TimestampAffixElement;
+import org.jivesoftware.smackx.stanza_content_encryption.element.ToAffixElement;
+import org.jivesoftware.smackx.stanza_content_encryption.provider.EnvelopeElementProvider;
+
+import org.jxmpp.jid.BareJid;
+
+public class OmemoOptOutUtil {
+ private static TimestampAffixElement affixTimeStamp = null;
+ private static FromAffixElement affixFrom = null;
+ private static ToAffixElement affixTo = null;
+ private static int timeDeltaInMinute = 10;
+
+ @SuppressWarnings("JavaUtilDate")
+ public static EnvelopeElement createOmemoOptOut(OmemoManager manager, BareJid recipient, String reason) {
+ OmemoOptOutElement optoutElement = new OmemoOptOutElement();
+ if (StringUtils.isNotEmpty(reason)) {
+ optoutElement.setReason(reason);
+ }
+
+ EnvelopeElement envelopeElement = EnvelopeElement.builder()
+ .setFrom(manager.getOwnJid())
+ .addTo(recipient)
+ .setTimestamp(new Date())
+ .setRandomPadding()
+ .addContentItem(optoutElement)
+ .build();
+
+ return envelopeElement;
+ }
+
+ public static String parseEnvelopElement(OmemoMessage.Received decryptedMessage, Date date) {
+ String reason = null;
+
+ String msgBody = decryptedMessage.getBody();
+ if (msgBody.contains(EnvelopeElement.NAMESPACE)) {
+ BareJid fromJid = decryptedMessage.getSenderDevice().getJid();
+ try {
+ XmlPullParser parser = PacketParserUtils.getParserFor(msgBody);
+ EnvelopeElementProvider provider = new EnvelopeElementProvider();
+ EnvelopeElement envelopElement = provider.parse(parser, parser.getDepth(), null, null);
+ reason = getReason(envelopElement, fromJid, date);
+ }
+ catch (XmlPullParserException | IOException | ParseException | SmackParsingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return reason;
+ }
+
+ private static String getReason(EnvelopeElement envelopElement, BareJid fromJid, Date timeStamp) {
+ String reasonText = ValidateOptOutContent(envelopElement, fromJid, timeStamp);
+ if (StringUtils.isNullOrEmpty(reasonText)) {
+ ContentElement content = envelopElement.getContentElement();
+ List contentElements = content.getItems();
+
+ XmlElement contentElement = (contentElements.size() != 0) ? contentElements.get(0) : null;
+ if (contentElement != null) {
+ reasonText = ((OmemoOptOutElement) contentElement).getReason();
+ }
+ }
+ return reasonText;
+ }
+
+ private static String ValidateOptOutContent(EnvelopeElement envelopElement, BareJid fromJid, Date date) {
+ String reason = null;
+ String warning = "Warning: Received suspicious omemo chat Opt-out!";
+
+ List affixElements = envelopElement.getAffixElements();
+ extractAffixes(affixElements);
+
+ boolean isValid = affixElements.contains(new FromAffixElement(fromJid));
+ if (!isValid) {
+ reason = warning + " Invalid sender: " + affixFrom.getJid();
+ }
+
+ if (affixTo != null && StringUtils.isNotEmpty(reason)) {
+ reason += "Recipient: " + affixTo.getJid();
+ }
+
+ LocalDateTime localDateTime1 = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
+ LocalDateTime localDateTime2 = LocalDateTime.ofInstant(affixTimeStamp.getTimestamp().toInstant(), ZoneId.systemDefault());
+ long diffTime = ChronoUnit.MINUTES.between(localDateTime1, localDateTime2);
+ if (Math.abs(diffTime) > timeDeltaInMinute) {
+ reason = warning + " Time difference (mins): " + diffTime;
+ }
+ return reason;
+ }
+
+ /**
+ * This function extracts only important affix elements for app query.
+ */
+ private static void extractAffixes(List affixElements) {
+ for (AffixElement affixElement : affixElements) {
+ if (affixElement instanceof TimestampAffixElement) {
+ affixTimeStamp = (TimestampAffixElement) affixElement;
+ }
+ else if (affixElement instanceof ToAffixElement) {
+ affixTo = (ToAffixElement) affixElement;
+ }
+ else if (affixElement instanceof FromAffixElement) {
+ affixFrom = (FromAffixElement) affixElement;
+ }
+ }
+ }
+}
diff --git a/smack-omemo/src/main/resources/org.jivesoftware.smackx.omemo/omemo.providers b/smack-omemo/src/main/resources/org.jivesoftware.smackx.omemo/omemo.providers
index 895314d02e..71d2eb1b12 100644
--- a/smack-omemo/src/main/resources/org.jivesoftware.smackx.omemo/omemo.providers
+++ b/smack-omemo/src/main/resources/org.jivesoftware.smackx.omemo/omemo.providers
@@ -1,7 +1,23 @@
+
+
+ encrypted
+ urn:xmpp:omemo:2
+ org.jivesoftware.smackx.omemo.provider.OmemoVOmemoProvider
+
+
+ devices
+ urn:xmpp:omemo:2
+ org.jivesoftware.smackx.omemo.provider.OmemoDeviceListVOmemoProvider
+
+
+ bundle
+ urn:xmpp:omemo:2
+ org.jivesoftware.smackx.omemo.provider.OmemoBundleVOmemoProvider
+
-
+
encrypted
eu.siacs.conversations.axolotl
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/DeviceListTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/DeviceListTest.java
index 464846f14b..8746235b18 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/DeviceListTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/DeviceListTest.java
@@ -23,19 +23,18 @@
import java.util.HashSet;
import java.util.Set;
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceElement;
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
import org.junit.Test;
-
/**
* Test behavior of device lists.
*
* @author Paul Schaub
+ * @author Eng Chong Meng
*/
public class DeviceListTest {
-
-
/**
* Test, whether deviceList updates are correctly merged into the cached device list.
* IDs in the update become active devices, active IDs that were not in the update become inactive.
@@ -47,31 +46,31 @@ public void mergeDeviceListsTest() {
assertNotNull(cached.getActiveDevices());
assertNotNull(cached.getInactiveDevices());
- cached.getInactiveDevices().add(1);
- cached.getInactiveDevices().add(2);
- cached.getActiveDevices().add(3);
+ cached.getInactiveDevices().add(new OmemoDeviceElement(1));
+ cached.getInactiveDevices().add(new OmemoDeviceElement(2));
+ cached.getActiveDevices().add(new OmemoDeviceElement(3));
- Set update = new HashSet<>();
- update.add(4);
- update.add(1);
+ Set update = new HashSet<>();
+ update.add(new OmemoDeviceElement(1));
+ update.add(new OmemoDeviceElement(4));
cached.merge(update);
- assertTrue(cached.getActiveDevices().contains(1) &&
- !cached.getActiveDevices().contains(2) &&
- !cached.getActiveDevices().contains(3) &&
- cached.getActiveDevices().contains(4));
+ assertTrue(cached.getActiveDevices().contains(new OmemoDeviceElement(1)) &&
+ !cached.getActiveDevices().contains(new OmemoDeviceElement(2)) &&
+ !cached.getActiveDevices().contains(new OmemoDeviceElement(3)) &&
+ cached.getActiveDevices().contains(new OmemoDeviceElement(4)));
- assertTrue(!cached.getInactiveDevices().contains(1) &&
- cached.getInactiveDevices().contains(2) &&
- cached.getInactiveDevices().contains(3) &&
- !cached.getInactiveDevices().contains(4));
+ assertTrue(!cached.getInactiveDevices().contains(new OmemoDeviceElement(1)) &&
+ cached.getInactiveDevices().contains(new OmemoDeviceElement(2)) &&
+ cached.getInactiveDevices().contains(new OmemoDeviceElement(3)) &&
+ !cached.getInactiveDevices().contains(new OmemoDeviceElement(4)));
assertTrue(cached.getAllDevices().size() == 4);
- assertFalse(cached.contains(17));
- cached.addDevice(17);
- assertTrue(cached.getActiveDevices().contains(17));
+ assertFalse(cached.contains(new OmemoDeviceElement(17)));
+ cached.addDevice(new OmemoDeviceElement(17));
+ assertTrue(cached.getActiveDevices().contains(new OmemoDeviceElement(17)));
assertNotNull(cached.toString());
}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleElementTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleElementTest.java
new file mode 100644
index 0000000000..f2cd8e2d07
--- /dev/null
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleElementTest.java
@@ -0,0 +1,157 @@
+/*
+ *
+ * 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;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
+
+import java.nio.charset.StandardCharsets;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.jivesoftware.smack.test.util.SmackTestSuite;
+import org.jivesoftware.smack.test.util.TestUtils;
+import org.jivesoftware.smack.util.stringencoder.Base64;
+import org.jivesoftware.smack.xml.XmlPullParser;
+
+import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VOmemo;
+import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider;
+import org.jivesoftware.smackx.omemo.provider.OmemoBundleVOmemoProvider;
+
+import org.junit.Test;
+
+/**
+ * Test serialization and parsing of the Omemo Bundle Element.
+ *
+ * @author Paul Schaub
+ * @author Eng Chong Meng
+ */
+public class OmemoBundleElementTest extends SmackTestSuite {
+ int signedPreKeyId = 0;
+ static int preKeyId1 = 54, preKeyId2 = 98;
+ static String preKey1B64 = Base64.encodeToString("FirstPreKey".getBytes(StandardCharsets.UTF_8));
+ static String preKey2B64 = Base64.encodeToString("SecondPreKey".getBytes(StandardCharsets.UTF_8));
+
+ // Preserve the order of the pk's in prekeys.
+ static Map preKeysB64 = new LinkedHashMap<>();
+ static {
+ preKeysB64.put(preKeyId1, preKey1B64);
+ preKeysB64.put(preKeyId2, preKey2B64);
+ }
+
+ String signedPreKeyB64 = Base64.encodeToString("SignedPreKey".getBytes(StandardCharsets.UTF_8));
+ String signedPreKeySignatureB64 = Base64.encodeToString("SignedPreKeySignature".getBytes(StandardCharsets.UTF_8));
+ String identityKeyB64 = Base64.encodeToString("IdentityKey".getBytes(StandardCharsets.UTF_8));
+
+ byte[] signedPreKey = "SignedPreKey".getBytes(StandardCharsets.UTF_8);
+ byte[] signedPreKeySignature = "SignedPreKeySignature".getBytes(StandardCharsets.UTF_8);
+ byte[] identityKey = "IdentityKey".getBytes(StandardCharsets.UTF_8);
+ byte[] firstPreKey = "FirstPreKey".getBytes(StandardCharsets.UTF_8);
+ byte[] secondPreKey = "SecondPreKey".getBytes(StandardCharsets.UTF_8);
+
+ // ===== NameSpace: eu.siacs.conversations.axolotl Test =====/
+ @Test
+ public void serializationAxolotlTest() throws Exception {
+ OmemoBundleElement_VAxolotl bundle = new OmemoBundleElement_VAxolotl(signedPreKeyId,
+ signedPreKeyB64, signedPreKeySignatureB64, identityKeyB64, preKeysB64);
+
+ assertEquals("ElementName must match.", "bundle", bundle.getElementName());
+ assertEquals("Namespace must match.", "eu.siacs.conversations.axolotl", bundle.getNamespace());
+
+ String expected = "" +
+ "" + signedPreKeyB64 + "" +
+ "" + signedPreKeySignatureB64 + "" +
+ "" + identityKeyB64 + "" +
+ "" +
+ "" + preKey1B64 + "" +
+ "" + preKey2B64 + "" +
+ "" +
+ "";
+ String actual = bundle.toXML().toString();
+ assertEquals("Bundles XML must match.", expected, actual);
+
+ OmemoBundleElement_VAxolotl parsed = new OmemoBundleVAxolotlProvider().parse(TestUtils.getParser(actual));
+
+ assertArrayEquals("B64-decoded signedPreKey must match.", signedPreKey, parsed.getSignedPreKey());
+ assertEquals("SignedPreKeyId must match", signedPreKeyId, parsed.getSignedPreKeyId());
+ assertArrayEquals("B64-decoded signedPreKey signature must match.", signedPreKeySignature, parsed.getSignedPreKeySignature());
+ assertArrayEquals("B64-decoded identityKey must match.", identityKey, parsed.getIdentityKey());
+ assertArrayEquals("B64-decoded first preKey must match.", firstPreKey, parsed.getPreKey(preKeyId1));
+ assertArrayEquals("B64-decoded second preKey must match.", secondPreKey, parsed.getPreKey(preKeyId2));
+ assertEquals("toString outputs must match.", bundle.toString(), parsed.toString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void emptyPreKeysAxolotlShouldFailTest() throws Exception {
+ String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki";
+ XmlPullParser parser = TestUtils.getParser(s);
+ new OmemoBundleVAxolotlProvider().parse(parser);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void missingPreKeysAxolotlShouldAlsoFailTest() throws Exception {
+ String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki";
+ XmlPullParser parser = TestUtils.getParser(s);
+ new OmemoBundleVAxolotlProvider().parse(parser);
+ }
+
+ // ===== NameSpace: urn:xmpp:omemo:2 Test =====/
+ @Test
+ public void serializationOmemoTest() throws Exception {
+ OmemoBundleElement_VOmemo bundle = new OmemoBundleElement_VOmemo(signedPreKeyId, signedPreKeyB64, signedPreKeySignatureB64, identityKeyB64, preKeysB64);
+
+ assertEquals("ElementName must match.", "bundle", bundle.getElementName());
+ assertEquals("Namespace must match.", "urn:xmpp:omemo:2", bundle.getNamespace());
+
+ String expected = "" +
+ "" + signedPreKeyB64 + "" +
+ "" + signedPreKeySignatureB64 + "" +
+ "" + identityKeyB64 + "" +
+ "" +
+ "" + preKey1B64 + "" +
+ "" + preKey2B64 + "" +
+ "" +
+ "";
+ String actual = bundle.toXML().toString();
+ assertEquals("Bundles XML must match.", expected, actual);
+
+ OmemoBundleElement_VOmemo parsed = new OmemoBundleVOmemoProvider().parse(TestUtils.getParser(actual));
+
+ assertArrayEquals("B64-decoded signedPreKey must match.", signedPreKey, parsed.getSignedPreKey());
+ assertEquals("SignedPreKeyId must match", signedPreKeyId, parsed.getSignedPreKeyId());
+ assertArrayEquals("B64-decoded signedPreKey signature must match.", signedPreKeySignature, parsed.getSignedPreKeySignature());
+ assertArrayEquals("B64-decoded identityKey must match.", identityKey, parsed.getIdentityKey());
+ assertArrayEquals("B64-decoded first preKey must match.", firstPreKey, parsed.getPreKey(preKeyId1));
+ assertArrayEquals("B64-decoded second preKey must match.", secondPreKey, parsed.getPreKey(preKeyId2));
+ assertEquals("toString outputs must match.", bundle.toString(), parsed.toString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void emptyPreKeysOmemoShouldFailTest() throws Exception {
+ String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki";
+ XmlPullParser parser = TestUtils.getParser(s);
+ new OmemoBundleVOmemoProvider().parse(parser);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void missingPreKeysOmemoShouldAlsoFailTest() throws Exception {
+ String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki";
+ XmlPullParser parser = TestUtils.getParser(s);
+ new OmemoBundleVOmemoProvider().parse(parser);
+ }
+}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleVAxolotlElementTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleVAxolotlElementTest.java
deleted file mode 100644
index 8304c6813d..0000000000
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoBundleVAxolotlElementTest.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- *
- * 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;
-
-import static junit.framework.TestCase.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.HashMap;
-
-import org.jivesoftware.smack.test.util.SmackTestSuite;
-import org.jivesoftware.smack.test.util.TestUtils;
-import org.jivesoftware.smack.util.stringencoder.Base64;
-import org.jivesoftware.smack.xml.XmlPullParser;
-
-import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl;
-import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider;
-
-import org.junit.Test;
-
-/**
- * Test serialization and parsing of the OmemoBundleVAxolotlElement.
- */
-public class OmemoBundleVAxolotlElementTest extends SmackTestSuite {
-
- @Test
- public void serializationTest() throws Exception {
- int signedPreKeyId = 420;
- String signedPreKeyB64 = Base64.encodeToString("SignedPreKey".getBytes(StandardCharsets.UTF_8));
- String signedPreKeySigB64 = Base64.encodeToString("SignedPreKeySignature".getBytes(StandardCharsets.UTF_8));
- String identityKeyB64 = Base64.encodeToString("IdentityKey".getBytes(StandardCharsets.UTF_8));
- int preKeyId1 = 220, preKeyId2 = 284;
- String preKey1B64 = Base64.encodeToString("FirstPreKey".getBytes(StandardCharsets.UTF_8)),
- preKey2B64 = Base64.encodeToString("SecondPreKey".getBytes(StandardCharsets.UTF_8));
- HashMap preKeysB64 = new HashMap<>();
- preKeysB64.put(preKeyId1, preKey1B64);
- preKeysB64.put(preKeyId2, preKey2B64);
-
- OmemoBundleElement_VAxolotl bundle = new OmemoBundleElement_VAxolotl(signedPreKeyId,
- signedPreKeyB64, signedPreKeySigB64, identityKeyB64, preKeysB64);
-
- assertEquals("ElementName must match.", "bundle", bundle.getElementName());
- assertEquals("Namespace must match.", "eu.siacs.conversations.axolotl", bundle.getNamespace());
-
- String expected =
- "" +
- "" +
- signedPreKeyB64 +
- "" +
- "" +
- signedPreKeySigB64 +
- "" +
- "" +
- identityKeyB64 +
- "" +
- "" +
- "" +
- preKey1B64 +
- "" +
- "" +
- preKey2B64 +
- "" +
- "" +
- "";
- String actual = bundle.toXML().toString();
- assertEquals("Bundles XML must match.", expected, actual);
-
- byte[] signedPreKey = "SignedPreKey".getBytes(StandardCharsets.UTF_8);
- byte[] signedPreKeySig = "SignedPreKeySignature".getBytes(StandardCharsets.UTF_8);
- byte[] identityKey = "IdentityKey".getBytes(StandardCharsets.UTF_8);
- byte[] firstPreKey = "FirstPreKey".getBytes(StandardCharsets.UTF_8);
- byte[] secondPreKey = "SecondPreKey".getBytes(StandardCharsets.UTF_8);
-
- 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());
- assertTrue("B64-decoded signedPreKey signature must match.", Arrays.equals(signedPreKeySig, parsed.getSignedPreKeySignature()));
- assertTrue("B64-decoded identityKey must match.", Arrays.equals(identityKey, parsed.getIdentityKey()));
- assertTrue("B64-decoded first preKey must match.", Arrays.equals(firstPreKey, parsed.getPreKey(220)));
- assertTrue("B64-decoded second preKey must match.", Arrays.equals(secondPreKey, parsed.getPreKey(284)));
- assertEquals("toString outputs must match.", bundle.toString(), parsed.toString());
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void emptyPreKeysShouldFailTest() throws Exception {
- String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki";
- XmlPullParser parser = TestUtils.getParser(s);
- new OmemoBundleVAxolotlProvider().parse(parser);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void missingPreKeysShouldAlsoFailTest() throws Exception {
- String s = "BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZbMaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki";
- XmlPullParser parser = TestUtils.getParser(s);
- new OmemoBundleVAxolotlProvider().parse(parser);
- }
-}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoConfigurationTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoConfigurationTest.java
index f9fda419f9..18b21e3f47 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoConfigurationTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoConfigurationTest.java
@@ -34,15 +34,15 @@ public void omemoConfigurationTest() {
// Body hint
OmemoConfiguration.setAddOmemoHintBody(false);
- assertEquals(false, OmemoConfiguration.getAddOmemoHintBody());
+ assertFalse(OmemoConfiguration.getAddOmemoHintBody());
OmemoConfiguration.setAddOmemoHintBody(true);
- assertEquals(true, OmemoConfiguration.getAddOmemoHintBody());
+ assertTrue(OmemoConfiguration.getAddOmemoHintBody());
// Delete stale devices
OmemoConfiguration.setDeleteStaleDevices(false);
- assertEquals(false, OmemoConfiguration.getDeleteStaleDevices());
+ assertFalse(OmemoConfiguration.getDeleteStaleDevices());
OmemoConfiguration.setDeleteStaleDevices(true);
- assertEquals(true, OmemoConfiguration.getDeleteStaleDevices());
+ assertTrue(OmemoConfiguration.getDeleteStaleDevices());
OmemoConfiguration.setDeleteStaleDevicesAfterHours(25);
assertEquals(25, OmemoConfiguration.getDeleteStaleDevicesAfterHours());
try {
@@ -54,9 +54,9 @@ public void omemoConfigurationTest() {
// Renew signedPreKeys
OmemoConfiguration.setRenewOldSignedPreKeys(false);
- assertEquals(false, OmemoConfiguration.getRenewOldSignedPreKeys());
+ assertFalse(OmemoConfiguration.getRenewOldSignedPreKeys());
OmemoConfiguration.setRenewOldSignedPreKeys(true);
- assertEquals(true, OmemoConfiguration.getRenewOldSignedPreKeys());
+ assertTrue(OmemoConfiguration.getRenewOldSignedPreKeys());
OmemoConfiguration.setRenewOldSignedPreKeysAfterHours(77);
assertEquals(77, OmemoConfiguration.getRenewOldSignedPreKeysAfterHours());
try {
@@ -86,4 +86,5 @@ public void omemoConfigurationTest() {
OmemoConfiguration.setCompleteSessionWithEmptyMessage(true);
assertTrue(OmemoConfiguration.getCompleteSessionWithEmptyMessage());
}
+
}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceListVAxolotlElementTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceListElementTest.java
similarity index 53%
rename from smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceListVAxolotlElementTest.java
rename to smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceListElementTest.java
index e0038c125e..15733afb11 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceListVAxolotlElementTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceListElementTest.java
@@ -17,42 +17,62 @@
package org.jivesoftware.smackx.omemo;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import java.util.HashSet;
+import java.util.Set;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceElement;
import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VOmemo;
import org.jivesoftware.smackx.omemo.provider.OmemoDeviceListVAxolotlProvider;
+import org.jivesoftware.smackx.omemo.provider.OmemoDeviceListVOmemoProvider;
import org.junit.Test;
/**
* Test serialization and parsing of DeviceListElement.
+ *
+ * @author Paul Schaub
+ * @author Eng Chong Meng
*/
-public class OmemoDeviceListVAxolotlElementTest extends SmackTestSuite {
+public class OmemoDeviceListElementTest extends SmackTestSuite {
@Test
public void serializationTest() throws Exception {
- HashSet ids = new HashSet<>();
- ids.add(1234);
- ids.add(9876);
+ Set devices = new HashSet<>(Set.of(
+ new OmemoDeviceElement(1234),
+ new OmemoDeviceElement(9876)));
- OmemoDeviceListElement_VAxolotl element = new OmemoDeviceListElement_VAxolotl(ids);
+ OmemoDeviceListElement_VAxolotl element = new OmemoDeviceListElement_VAxolotl(devices);
String xml = element.toXML().toString();
XmlPullParser parser = TestUtils.getParser(xml);
OmemoDeviceListElement_VAxolotl parsed = new OmemoDeviceListVAxolotlProvider().parse(parser);
- assertTrue("Parsed element must equal the original.", parsed.getDeviceIds().equals(element.getDeviceIds()));
+ assertEquals("Parsed element must equal the original.", parsed.getDevices(), element.getDevices());
assertEquals("Generated XML must match.",
"" +
- "" +
- "" +
+ "" +
+ "" +
"
",
xml);
+
+ OmemoDeviceListElement_VOmemo element2 = new OmemoDeviceListElement_VOmemo(devices);
+ String xml2 = element2.toXML().toString();
+
+ XmlPullParser parser2 = TestUtils.getParser(xml2);
+ OmemoDeviceListElement_VOmemo parsed2 = new OmemoDeviceListVOmemoProvider().parse(parser2);
+
+ assertEquals("Parsed element must equal the original.", parsed2.getDevices(), element2.getDevices());
+ assertEquals("Generated XML must match.",
+ "" +
+ "" +
+ "" +
+ "",
+ xml2);
}
}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceTest.java
index 93d9de27b7..7e56506cdb 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoDeviceTest.java
@@ -16,8 +16,8 @@
*/
package org.jivesoftware.smackx.omemo;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
@@ -55,9 +55,9 @@ public void testEquals() {
OmemoDevice j = new OmemoDevice(juliet, 3);
OmemoDevice j2 = new OmemoDevice(juliet, 1);
- assertTrue(r.equals(g));
- assertFalse(r.equals(r2));
- assertFalse(j.equals(j2));
- assertFalse(j2.equals(r2));
+ assertEquals(r, g);
+ assertNotEquals(r, r2);
+ assertNotEquals(j, j2);
+ assertNotEquals(j2, r2);
}
}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoElementTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoElementTest.java
new file mode 100644
index 0000000000..db309798f3
--- /dev/null
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoElementTest.java
@@ -0,0 +1,138 @@
+/*
+ *
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jivesoftware.smack.test.util.SmackTestSuite;
+import org.jivesoftware.smack.test.util.TestUtils;
+import org.jivesoftware.smack.util.stringencoder.Base64;
+
+import org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoKeyElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoKeyElement_VOmemo;
+import org.jivesoftware.smackx.omemo.element.OmemoKeysElement_VOmemo;
+import org.jivesoftware.smackx.omemo.provider.OmemoVAxolotlProvider;
+import org.jivesoftware.smackx.omemo.provider.OmemoVOmemoProvider;
+import org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder;
+
+import org.junit.Test;
+
+/**
+ * Test serialization and parsing of OmemoVAxolotlElements.
+ */
+public class OmemoElementTest extends SmackTestSuite {
+
+ @Test
+ public void serializationTest() throws Exception {
+ byte[] payload = "This is payload.".getBytes(StandardCharsets.UTF_8);
+ int keyId1 = 8;
+ int keyId2 = 33333;
+ byte[] keyData1 = "KEYDATA".getBytes(StandardCharsets.UTF_8);
+ byte[] keyData2 = "DATAKEY".getBytes(StandardCharsets.UTF_8);
+ int sid = 12131415;
+ byte[] iv = OmemoMessageBuilder.generateIv();
+
+ // === Test with VAxolotl ===/
+ ArrayList keys = new ArrayList<>();
+ keys.add(new OmemoKeyElement_VAxolotl(keyData1, keyId1));
+ keys.add(new OmemoKeyElement_VAxolotl(keyData2, keyId2, true));
+
+ OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(sid, keys, iv);
+ OmemoElement_VAxolotl element = new OmemoElement_VAxolotl(header, payload);
+
+ String expected =
+ "" +
+ "" +
+ "" + Base64.encodeToString(keyData1) + "" +
+ "" + Base64.encodeToString(keyData2) + "" +
+ "" + Base64.encodeToString(iv) + "" +
+ "" +
+ "" +
+ Base64.encodeToString(payload) +
+ "" +
+ "";
+
+ String actual = element.toXML().toString();
+ assertEquals("Serialized xml of OmemoElement must match.", expected, actual);
+
+ OmemoElement_VAxolotl parsed = new OmemoVAxolotlProvider().parse(TestUtils.getParser(actual));
+ assertEquals("Parsed OmemoElement must equal the original.",
+ element.toXML().toString(),
+ parsed.toXML().toString());
+
+ // === Test with VOmemo === /
+ int keySid = 27183;
+ int keyRid1 = 31415;
+ int keyRid2 = 1337;
+ int keyRid3 = 12321;
+ String sJid = "juliet@capulet.lit";
+ String rJid = "romeo@montague.lit";
+
+ List keySids = new ArrayList<>(
+ List.of(new OmemoKeyElement_VOmemo(keyData1, keyRid1))
+ );
+ OmemoKeysElement_VOmemo keysSid = new OmemoKeysElement_VOmemo(sJid, keySids);
+
+ List keyRids = new ArrayList<>(
+ List.of(new OmemoKeyElement_VOmemo(keyData1, keyRid2),
+ new OmemoKeyElement_VOmemo(keyData2, keyRid3, true)
+ ));
+ OmemoKeysElement_VOmemo keysRid = new OmemoKeysElement_VOmemo(rJid, keyRids);
+
+ List keysElement = new ArrayList<>(
+ List.of(keysSid, keysRid)
+ );
+
+ OmemoHeaderElement_VOmemo header2 = new OmemoHeaderElement_VOmemo(keySid, keysElement, iv);
+ OmemoElement_VOmemo element2 = new OmemoElement_VOmemo(header2, payload);
+
+ String expected2 =
+ "" +
+ "" +
+ "" +
+ "" + Base64.encodeToString(keyData1) + "" +
+ "" +
+ "" +
+ "" + Base64.encodeToString(keyData1) + "" +
+ "" + Base64.encodeToString(keyData2) + "" +
+ "" +
+ "" + Base64.encodeToString(iv) + "" +
+ "" +
+ "" +
+ Base64.encodeToString(payload) +
+ "" +
+ "";
+
+ String actual2 = element2.toXML().toString();
+ assertEquals("Serialized xml of OmemoElement must match.", expected2, actual2);
+
+ OmemoElement_VOmemo parsed2 = new OmemoVOmemoProvider().parse(TestUtils.getParser(actual2));
+ assertEquals("Parsed OmemoElement must equal the original.",
+ element2.toXML().toString(),
+ parsed2.toXML().toString());
+ }
+
+
+}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoExceptionsTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoExceptionsTest.java
index a2ecbb4a6e..e12deeb368 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoExceptionsTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoExceptionsTest.java
@@ -46,17 +46,17 @@ public void undecidedOmemoIdentityExceptionTest() throws XmppStringprepException
UndecidedOmemoIdentityException u = new UndecidedOmemoIdentityException(alice);
assertTrue(u.getUndecidedDevices().contains(alice));
- assertTrue(u.getUndecidedDevices().size() == 1);
+ assertEquals(1, u.getUndecidedDevices().size());
UndecidedOmemoIdentityException v = new UndecidedOmemoIdentityException(bob);
v.getUndecidedDevices().add(mallory);
- assertTrue(v.getUndecidedDevices().size() == 2);
+ assertEquals(2, v.getUndecidedDevices().size());
assertTrue(v.getUndecidedDevices().contains(bob));
assertTrue(v.getUndecidedDevices().contains(mallory));
u.getUndecidedDevices().add(bob);
u.join(v);
- assertTrue(u.getUndecidedDevices().size() == 3);
+ assertEquals(3, u.getUndecidedDevices().size());
}
@Test
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoFingerprintTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoFingerprintTest.java
index a55d5b8a0b..dbb77cb841 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoFingerprintTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoFingerprintTest.java
@@ -34,6 +34,6 @@ public void fingerprintTest() {
OmemoFingerprint second = new OmemoFingerprint("TOE");
assertNotSame(first, second);
- assertEquals(first, new OmemoFingerprint("FINGER"));
+ assertEquals(new OmemoFingerprint("FINGER"), first);
}
}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoKeyUtilTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoKeyUtilTest.java
index 071596d2fb..a244de28d2 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoKeyUtilTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoKeyUtilTest.java
@@ -21,17 +21,19 @@
import static junit.framework.TestCase.assertNull;
import java.io.IOException;
-import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
+import org.jivesoftware.smackx.omemo.element.OmemoBundleElement;
import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl;
+import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VOmemo;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider;
+import org.jivesoftware.smackx.omemo.provider.OmemoBundleVOmemoProvider;
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
import org.jivesoftware.smackx.omemo.util.OmemoKeyUtil;
@@ -39,7 +41,7 @@
import org.jxmpp.jid.impl.JidCreate;
public abstract class OmemoKeyUtilTest
-extends SmackTestSuite {
+ extends SmackTestSuite {
protected OmemoKeyUtil keyUtil;
@@ -48,7 +50,6 @@ public OmemoKeyUtilTest(OmemoKeyUtil bundles = keyUtil.BUNDLE.bundles(bundle, device);
assertEquals("There must be 100 bundles in the HashMap.", 100, bundles.size());
assertNotNull(keyUtil.BUNDLE.identityKey(bundle));
- Iterator it = bundles.keySet().iterator();
- while (it.hasNext()) {
- assertNotNull(keyUtil.BUNDLE.preKeyPublic(bundle, it.next()));
+ for (Integer integer : bundles.keySet()) {
+ assertNotNull(keyUtil.BUNDLE.preKeyPublic(bundle, integer));
}
assertEquals(1, keyUtil.BUNDLE.signedPreKeyId(bundle));
@@ -108,8 +118,7 @@ public void parsedBundlesDoNotContainNullValues() throws Exception {
@Test
public void generateOmemoPreKeysIdsMatchAndNoNullValues() {
- TreeMap pks =
- keyUtil.generateOmemoPreKeys(1, 20);
+ TreeMap pks = keyUtil.generateOmemoPreKeys(1, 20);
for (int i = 1; i <= 20; i++) {
assertEquals("PreKeyIds must correspond the requested ids.", Integer.valueOf(i), pks.firstKey());
@@ -122,15 +131,15 @@ public void generateOmemoPreKeysIdsMatchAndNoNullValues() {
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);
+ assertEquals(1, OmemoKeyUtil.addInBounds(high, 3));
+ assertEquals(3, OmemoKeyUtil.addInBounds(1, 2));
+ assertEquals(5, OmemoKeyUtil.addInBounds(max, 5));
}
@Test
public void testPrettyFingerprint() {
OmemoFingerprint fingerprint = new OmemoFingerprint("FFFFFFFFEEEEEEEEDDDDDDDDCCCCCCCCBBBBBBBBAAAAAAAA9999999988888888");
String pretty = fingerprint.blocksOf8Chars();
- assertEquals(pretty, "FFFFFFFF EEEEEEEE DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA 99999999 88888888");
+ assertEquals("FFFFFFFF EEEEEEEE DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA 99999999 88888888", pretty);
}
}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoStoreTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoStoreTest.java
index 6d49315134..0b32416b35 100644
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoStoreTest.java
+++ b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoStoreTest.java
@@ -29,8 +29,10 @@
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
+import org.jivesoftware.smackx.omemo.element.OmemoDeviceElement;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
@@ -166,7 +168,6 @@ public void generateOmemoPreKeys() {
keys.remove(keys.firstKey());
}
-
assertEquals("After deleting 49 keys, there must be no keys left.", 0, keys.size());
}
@@ -271,11 +272,18 @@ public void loadStoreDateOfLastMessageReceived() throws IOException {
@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)));
+ Set active = new HashSet<>(Set.of(
+ new OmemoDeviceElement(1),
+ new OmemoDeviceElement(5),
+ new OmemoDeviceElement(999),
+ new OmemoDeviceElement(10)));
+
+ Set inactive = new HashSet<>(Set.of(
+ new OmemoDeviceElement(6),
+ new OmemoDeviceElement(7),
+ new OmemoDeviceElement(8)));
+
+ OmemoCachedDeviceList before = new OmemoCachedDeviceList(active, inactive);
assertNotNull("Loading a non-existent cached deviceList must return an empty list.",
store.loadCachedDeviceList(alice, bob.getJid()));
@@ -284,16 +292,16 @@ public void loadStoreCachedDeviceList() throws IOException {
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());
+ assertEquals("Number of entries in active devices must match.", active.size(), after.getActiveDevices().size());
+ assertEquals("Number of entries in inactive devices must match.", inactive.size(), after.getInactiveDevices().size());
+ assertEquals("Number of total entries must match.", active.size() + inactive.size(), after.getAllDevices().size());
- for (Integer a : active) {
+ for (OmemoDeviceElement a : active) {
assertTrue(after.getActiveDevices().contains(a));
assertTrue(after.getAllDevices().contains(a));
}
- for (Integer i : inactive) {
+ for (OmemoDeviceElement i : inactive) {
assertTrue(after.getInactiveDevices().contains(i));
assertTrue(after.getAllDevices().contains(i));
}
@@ -339,8 +347,13 @@ public void getFingerprint() throws IOException, CorruptedOmemoKeyException {
static TemporaryFolder initStaticTemp() {
try {
- return new TemporaryFolder() { { before(); } };
- } catch (Throwable t) {
+ return new TemporaryFolder() {
+ {
+ before();
+ }
+ };
+ }
+ catch (Throwable t) {
throw new RuntimeException(t);
}
}
diff --git a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoVAxolotlElementTest.java b/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoVAxolotlElementTest.java
deleted file mode 100644
index d0e2d51686..0000000000
--- a/smack-omemo/src/test/java/org/jivesoftware/smackx/omemo/OmemoVAxolotlElementTest.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- *
- * 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;
-
-import static org.junit.Assert.assertEquals;
-
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-
-import org.jivesoftware.smack.test.util.SmackTestSuite;
-import org.jivesoftware.smack.test.util.TestUtils;
-import org.jivesoftware.smack.util.stringencoder.Base64;
-
-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;
-
-import org.junit.Test;
-
-/**
- * Test serialization and parsing of OmemoVAxolotlElements.
- */
-public class OmemoVAxolotlElementTest extends SmackTestSuite {
-
- @Test
- public void serializationTest() throws Exception {
- byte[] payload = "This is payload.".getBytes(StandardCharsets.UTF_8);
- int keyId1 = 8;
- int keyId2 = 33333;
- byte[] keyData1 = "KEYDATA".getBytes(StandardCharsets.UTF_8);
- byte[] keyData2 = "DATAKEY".getBytes(StandardCharsets.UTF_8);
- int sid = 12131415;
- byte[] iv = OmemoMessageBuilder.generateIv();
-
- ArrayList keys = new ArrayList<>();
- keys.add(new OmemoKeyElement(keyData1, keyId1));
- keys.add(new OmemoKeyElement(keyData2, keyId2, true));
-
- OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(sid, keys, iv);
- OmemoElement_VAxolotl element = new OmemoElement_VAxolotl(header, payload);
-
- String expected =
- "" +
- "