Fixed prototype implementation of ikey

This commit is contained in:
Paul Schaub 2020-09-11 14:41:48 +02:00
parent 466eb6f228
commit 58740c4a69
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
29 changed files with 281 additions and 99 deletions

3
.gitmodules vendored
View File

@ -1,6 +1,3 @@
[submodule "libs/Smack"] [submodule "libs/Smack"]
path = libs/Smack path = libs/Smack
url = git@codeberg.org:Mercury-IM/Smack.git url = git@codeberg.org:Mercury-IM/Smack.git
[submodule "libs/jxmpp"]
path = libs/jxmpp
url = git@codeberg.org:Mercury-IM/jxmpp.git

View File

@ -114,7 +114,9 @@ dependencies {
implementation "org.igniterealtime.smack:smack-android-extensions:$smackAndroidExtensionsVersion" implementation "org.igniterealtime.smack:smack-android-extensions:$smackAndroidExtensionsVersion"
// Testing - as if... // Testing - as if...
testImplementation "junit:junit:$junitVersion" testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
androidTestImplementation "androidx.test:runner:$andxTestRunnerVersion" androidTestImplementation "androidx.test:runner:$andxTestRunnerVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$andxTestEspressoVersion" androidTestImplementation "androidx.test.espresso:espresso-core:$andxTestEspressoVersion"
} }

View File

@ -30,7 +30,9 @@ dependencies {
implementation 'com.google.code.findbugs:jsr305:3.0.2' implementation 'com.google.code.findbugs:jsr305:3.0.2'
// JUnit for testing // JUnit for testing
testImplementation "junit:junit:$junitVersion" testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation 'org.xerial:sqlite-jdbc:3.30.1' testImplementation 'org.xerial:sqlite-jdbc:3.30.1'
} }

View File

@ -18,6 +18,12 @@ dependencies {
api "org.igniterealtime.smack:smack-tcp:$smackTcpVersion" api "org.igniterealtime.smack:smack-tcp:$smackTcpVersion"
api "org.igniterealtime.smack:smack-openpgp:$smackOpenpgpVersion" api "org.igniterealtime.smack:smack-openpgp:$smackOpenpgpVersion"
api "org.jxmpp:jxmpp-core:1.0.1"
api "org.jxmpp:jxmpp-jid:1.0.1"
api "org.apache.santuario:xmlsec:2.2.0"
testImplementation "org.igniterealtime.smack:smack-java7:$smackJava7Version" testImplementation "org.igniterealtime.smack:smack-java7:$smackJava7Version"
// RxJava2 // RxJava2
@ -29,13 +35,22 @@ dependencies {
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion" annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
testAnnotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion" testAnnotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
api "org.apache.santuario:xmlsec:2.2.0"
compileOnly "org.projectlombok:lombok:$lombokVersion" compileOnly "org.projectlombok:lombok:$lombokVersion"
annotationProcessor "org.projectlombok:lombok:$lombokVersion" annotationProcessor "org.projectlombok:lombok:$lombokVersion"
// JUnit for testing // JUnit for testing
testImplementation "junit:junit:$junitVersion" testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
// The smack-extensions subproject uses mockito in its fest
// fixtures, and we want to have mockito also available in
// test, so we use API here.
testImplementation "org.mockito:mockito-core:3.3.3"
// To mock final classes
testImplementation 'org.mockito:mockito-inline:3.3.3'
testImplementation 'com.jamesmurty.utils:java-xmlbuilder:1.2'
} }
sourceCompatibility = "8" sourceCompatibility = "8"

View File

@ -1,8 +1,9 @@
package org.jivesoftware.smackx.ikey; package org.jivesoftware.smackx.ikey;
import org.jivesoftware.smack.parsing.SmackParsingException; import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.xml.SmackXmlParser;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.ikey.element.IkeyElement; import org.jivesoftware.smackx.ikey.element.IkeyElement;
import org.jivesoftware.smackx.ikey.provider.IkeyElementProvider; import org.jivesoftware.smackx.ikey.provider.IkeyElementProvider;
@ -16,6 +17,8 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
public class FileBasedIkeyStore implements IkeyStore { public class FileBasedIkeyStore implements IkeyStore {
@ -34,7 +37,7 @@ public class FileBasedIkeyStore implements IkeyStore {
} }
try { try {
String content = getFileContent(new FileInputStream(file)); String content = getFileContent(new FileInputStream(file));
return IkeyElementProvider.INSTANCE.parse(TestUtils.getParser(content)); return IkeyElementProvider.INSTANCE.parse(getParser(content));
} catch (XmlPullParserException | SmackParsingException e) { } catch (XmlPullParserException | SmackParsingException e) {
throw new IOException(e); throw new IOException(e);
} }
@ -67,4 +70,36 @@ public class FileBasedIkeyStore implements IkeyStore {
} }
} }
private static XmlPullParser getParser(String xml) {
return getParser(new StringReader(xml), null);
}
private static XmlPullParser getParser(Reader reader, String startTag) {
XmlPullParser parser;
try {
parser = SmackXmlParser.newXmlParser(reader);
if (startTag == null) {
while (parser.getEventType() != XmlPullParser.Event.START_ELEMENT) {
parser.next();
}
return parser;
}
boolean found = false;
while (!found) {
if ((parser.next() == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals(startTag))
found = true;
}
if (!found)
throw new IllegalArgumentException("Can not find start tag '" + startTag + "'");
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return parser;
}
} }

View File

@ -9,6 +9,7 @@ import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.Async; import org.jivesoftware.smack.util.Async;
import org.jivesoftware.smackx.ikey.element.IkeyElement; import org.jivesoftware.smackx.ikey.element.IkeyElement;
import org.jivesoftware.smackx.ikey.mechanism.IkeySignatureVerificationMechanism; import org.jivesoftware.smackx.ikey.mechanism.IkeySignatureVerificationMechanism;
@ -91,7 +92,7 @@ public final class IkeyManager extends Manager {
return fetchIkeyElementFrom(pubSubManager); return fetchIkeyElementFrom(pubSubManager);
} }
private IkeyElement fetchIkeyElementFrom(PubSubManager pubSubManager) private static IkeyElement fetchIkeyElementFrom(PubSubManager pubSubManager)
throws PubSubException.NotALeafNodeException, SmackException.NoResponseException, throws PubSubException.NotALeafNodeException, SmackException.NoResponseException,
SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException,
PubSubException.NotAPubSubNodeException { PubSubException.NotAPubSubNodeException {
@ -131,7 +132,7 @@ public final class IkeyManager extends Manager {
return verifier.verify(element, from); return verifier.verify(element, from);
} }
private IkeySignatureVerificationMechanism getSignatureVerificationMechanismFor(IkeyElement ikeyElement) private static IkeySignatureVerificationMechanism getSignatureVerificationMechanismFor(IkeyElement ikeyElement)
throws IOException, UnsupportedSignatureAlgorithmException { throws IOException, UnsupportedSignatureAlgorithmException {
switch (ikeyElement.getType()) { switch (ikeyElement.getType()) {
case OX: case OX:
@ -150,7 +151,7 @@ public final class IkeyManager extends Manager {
return elementTimestamp.after(now); return elementTimestamp.after(now);
} }
private boolean existsSameOrNewerRecord(IkeyElement ikeyElement) { private boolean existsSameOrNewerRecord(IkeyElement ikeyElement) throws IOException {
IkeyElement existingRecord = store.loadIkeyRecord(ikeyElement.getSubordinates().getJid()); IkeyElement existingRecord = store.loadIkeyRecord(ikeyElement.getSubordinates().getJid());
if (existingRecord == null) { if (existingRecord == null) {
return false; return false;
@ -161,16 +162,15 @@ public final class IkeyManager extends Manager {
|| latestTimestamp.after(eventTimestamp); // newer || latestTimestamp.after(eventTimestamp); // newer
} }
private final PepEventListener<IkeyElement> pepEventListener = @SuppressWarnings("UnnecessaryAnonymousClass")
(from, event, id, carrierMessage) -> Async.go(() -> { private final PepEventListener<IkeyElement> pepEventListener = new PepEventListener<IkeyElement>() {
try { @Override
processIkeyElement(from, event); public void onPepEvent(EntityBareJid from, IkeyElement event, String id, Message carrierMessage) {
} catch (XMLParserException | CanonicalizationException e) { try {
e.printStackTrace(); processIkeyElement(from, event);
} catch (IOException e) { } catch (XMLParserException | CanonicalizationException | IOException | UnsupportedSignatureAlgorithmException e) {
e.printStackTrace(); LOGGER.log(Level.WARNING, "Error:", e);
} catch (UnsupportedSignatureAlgorithmException e) { }
e.printStackTrace(); }
} };
});
} }

View File

@ -31,7 +31,7 @@ public class IkeySignatureVerifier {
return signatureVerificationMechanism.isSignatureValid(canonicalizedXml, signature); return signatureVerificationMechanism.isSignatureValid(canonicalizedXml, signature);
} }
private void throwIfMismatchingOwnerJid(IkeyElement element, EntityBareJid owner) { private static void throwIfMismatchingOwnerJid(IkeyElement element, EntityBareJid owner) {
if (!element.getSubordinates().getJid().equals(owner)) { if (!element.getSubordinates().getJid().equals(owner)) {
throw new IllegalArgumentException("Provided ikey element does not contain jid of " + owner); throw new IllegalArgumentException("Provided ikey element does not contain jid of " + owner);
} }

View File

@ -2,6 +2,8 @@ package org.jivesoftware.smackx.ikey.element;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.ikey.util.IkeyConstants; import org.jivesoftware.smackx.ikey.util.IkeyConstants;
import org.jivesoftware.smackx.ikey.mechanism.IkeyType; import org.jivesoftware.smackx.ikey.mechanism.IkeyType;
@ -14,6 +16,7 @@ public class IkeyElement implements ExtensionElement {
public static final String ELEMENT = "ikey"; public static final String ELEMENT = "ikey";
public static final String ATTR_IKEY_TYPE = "type"; public static final String ATTR_IKEY_TYPE = "type";
@SuppressWarnings("unused")
private static final QName QNAME = new QName(IkeyConstants.NAMESPACE, ELEMENT); private static final QName QNAME = new QName(IkeyConstants.NAMESPACE, ELEMENT);
private final IkeyType type; private final IkeyType type;
@ -64,4 +67,25 @@ public class IkeyElement implements ExtensionElement {
.append(getProof()) .append(getProof())
.closeElement(this); .closeElement(this);
} }
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getType())
.append(getSuperordinate())
.append(getSubordinates())
.append(getProof())
.build();
}
@Override
public boolean equals(Object other) {
return EqualsUtil.equals(this, other, (e, o) -> e
.append(getElementName(), o.getElementName())
.append(getType(), o.getType())
.append(getSuperordinate(), o.getSuperordinate())
.append(getSubordinates(), o.getSubordinates())
.append(getProof(), o.getProof()));
}
} }

View File

@ -2,6 +2,8 @@ package org.jivesoftware.smackx.ikey.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
public class ProofElement implements NamedElement { public class ProofElement implements NamedElement {
@ -29,4 +31,18 @@ public class ProofElement implements NamedElement {
.append(getBase64Signature()) .append(getBase64Signature())
.closeElement(this); .closeElement(this);
} }
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getBase64Signature()).build();
}
@Override
public boolean equals(Object other) {
return EqualsUtil.equals(this, other, (e, o) -> e
.append(getElementName(), o.getElementName())
.append(getBase64Signature(), o.getBase64Signature()));
}
} }

View File

@ -2,6 +2,8 @@ package org.jivesoftware.smackx.ikey.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -43,4 +45,21 @@ public class SubordinateElement implements NamedElement {
.closeEmptyElement(); .closeEmptyElement();
return xml; return xml;
} }
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getFingerprint())
.append(getUri())
.build();
}
@Override
public boolean equals(Object other) {
return EqualsUtil.equals(this, other, (e, o) -> e
.append(getElementName(), o.getElementName())
.append(getFingerprint(), o.getFingerprint())
.append(getUri(), o.getUri()));
}
} }

View File

@ -2,6 +2,8 @@ package org.jivesoftware.smackx.ikey.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.ikey.util.IkeyConstants; import org.jivesoftware.smackx.ikey.util.IkeyConstants;
@ -53,4 +55,23 @@ public class SubordinateListElement implements NamedElement {
.append(getSubordinates()) .append(getSubordinates())
.closeElement(this); .closeElement(this);
} }
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getJid())
.append(getTimestamp())
.append(getSubordinates())
.build();
}
@Override
public boolean equals(Object other) {
return EqualsUtil.equals(this, other, (e, o) -> e
.append(getElementName(), o.getElementName())
.append(getJid(), o.getJid())
.append(getTimestamp(), o.getTimestamp())
.append(getSubordinates(), o.getSubordinates()));
}
} }

View File

@ -2,6 +2,8 @@ package org.jivesoftware.smackx.ikey.element;
import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
@ -39,4 +41,19 @@ public class SuperordinateElement implements NamedElement {
.append(getBase64PubKey()) .append(getBase64PubKey())
.closeElement(this); .closeElement(this);
} }
@Override
public int hashCode() {
return HashCode.builder()
.append(getElementName())
.append(getBase64PubKey())
.build();
}
@Override
public boolean equals(Object other) {
return EqualsUtil.equals(this, other, (e, o) -> e
.append(getElementName(), o.getElementName())
.append(getBase64PubKey(), o.getBase64PubKey()));
}
} }

View File

@ -27,7 +27,7 @@ public class IkeyElementProvider extends ExtensionElementProvider<IkeyElement> {
@Override @Override
public IkeyElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) public IkeyElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException { throws XmlPullParserException, IOException, SmackParsingException {
String typeString = ParserUtils.getRequiredAttribute(parser, IkeyElement.ELEMENT); String typeString = ParserUtils.getRequiredAttribute(parser, IkeyElement.ATTR_IKEY_TYPE);
IkeyType type = IkeyType.valueOf(typeString); IkeyType type = IkeyType.valueOf(typeString);
SuperordinateElement superordinate = null; SuperordinateElement superordinate = null;
List<SubordinateElement> subordinates = new ArrayList<>(); List<SubordinateElement> subordinates = new ArrayList<>();
@ -35,7 +35,7 @@ public class IkeyElementProvider extends ExtensionElementProvider<IkeyElement> {
Date timestamp = null; Date timestamp = null;
ProofElement proofElement = null; ProofElement proofElement = null;
while (parser.getDepth() != initialDepth) { do {
switch (parser.nextTag()) { switch (parser.nextTag()) {
case START_ELEMENT: case START_ELEMENT:
switch (parser.getName()) { switch (parser.getName()) {
@ -55,14 +55,17 @@ public class IkeyElementProvider extends ExtensionElementProvider<IkeyElement> {
URI uri = URI.create(uriString); URI uri = URI.create(uriString);
String fingerprint = ParserUtils.getRequiredAttribute(parser, SubordinateElement.ATTR_SUB_FINGERPRINT); String fingerprint = ParserUtils.getRequiredAttribute(parser, SubordinateElement.ATTR_SUB_FINGERPRINT);
subordinates.add(new SubordinateElement(uri, fingerprint)); subordinates.add(new SubordinateElement(uri, fingerprint));
break;
case ProofElement.ELEMENT: case ProofElement.ELEMENT:
proofElement = new ProofElement(parser.nextText()); proofElement = new ProofElement(parser.nextText());
break; break;
} }
break;
case END_ELEMENT: case END_ELEMENT:
break;
} }
} } while (parser.getDepth() != initialDepth);
return new IkeyElement(type, superordinate, new SubordinateListElement(jid, timestamp, subordinates), proofElement); return new IkeyElement(type, superordinate, new SubordinateListElement(jid, timestamp, subordinates), proofElement);
} }
} }

View File

@ -5,6 +5,7 @@ import org.apache.xml.security.parser.XMLParserException;
import org.jivesoftware.smack.packet.Element; import org.jivesoftware.smack.packet.Element;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
public interface ElementCanonicalizer { public interface ElementCanonicalizer {
@ -14,7 +15,7 @@ public interface ElementCanonicalizer {
} }
default byte[] canonicalize(CharSequence xml) throws XMLParserException, IOException, CanonicalizationException { default byte[] canonicalize(CharSequence xml) throws XMLParserException, IOException, CanonicalizationException {
return canonicalize(xml.toString().getBytes()); return canonicalize(xml.toString().getBytes(StandardCharsets.UTF_8));
} }
byte[] canonicalize(byte[] xml) throws XMLParserException, IOException, CanonicalizationException; byte[] canonicalize(byte[] xml) throws XMLParserException, IOException, CanonicalizationException;

View File

@ -7,13 +7,11 @@ import org.apache.xml.security.parser.XMLParserException;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import javax.inject.Inject;
public class XmlSecElementCanonicalizer implements ElementCanonicalizer { public class XmlSecElementCanonicalizer implements ElementCanonicalizer {
private final Canonicalizer canonicalizer; private final Canonicalizer canonicalizer;
@Inject
public XmlSecElementCanonicalizer(Canonicalizer canonicalizer) { public XmlSecElementCanonicalizer(Canonicalizer canonicalizer) {
this.canonicalizer = canonicalizer; this.canonicalizer = canonicalizer;
} }

View File

@ -1,4 +1,4 @@
package org.jivesoftware.smackx.ikey.element; package org.jivesoftware.smackx.util;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.SmackConfiguration;

View File

@ -1,21 +1,26 @@
package org.jivesoftware.smackx.ikey.element; package org.jivesoftware.smackx.ikey.element;
import org.jivesoftware.smack.parsing.SmackParsingException; import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.TestUtils; import org.jivesoftware.smack.xml.SmackXmlParser;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.ikey.mechanism.IkeyType; import org.jivesoftware.smackx.ikey.mechanism.IkeyType;
import org.jivesoftware.smackx.ikey.provider.IkeyElementProvider; import org.jivesoftware.smackx.ikey.provider.IkeyElementProvider;
import org.junit.Test; import org.jivesoftware.smackx.util.MercurySmackTestSuite;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import static junit.framework.TestCase.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class IkeyElementTest extends MercurySmackTestSuite { public class IkeyElementTest extends MercurySmackTestSuite {
@ -41,13 +46,51 @@ public class IkeyElementTest extends MercurySmackTestSuite {
IkeyElement ikeyElement = new IkeyElement(type, superordinate, subordinates, proof); IkeyElement ikeyElement = new IkeyElement(type, superordinate, subordinates, proof);
String xml = ikeyElement.toXML().toString(); String xml = ikeyElement.toXML().toString();
System.out.println(xml);
IkeyElement parsed = IkeyElementProvider.INSTANCE.parse(TestUtils.getParser(xml)); IkeyElement parsed = IkeyElementProvider.INSTANCE.parse(getParser(xml));
assertEquals(ikeyElement, parsed); assertEquals(ikeyElement, parsed);
} }
private SubordinateListElement buildSubListElement(EntityBareJid jid, Date date, SubordinateElement... subordinateElements) {
private static SubordinateListElement buildSubListElement(EntityBareJid jid, Date date, SubordinateElement... subordinateElements) {
return new SubordinateListElement(jid, date, Arrays.asList(subordinateElements)); return new SubordinateListElement(jid, date, Arrays.asList(subordinateElements));
} }
public static XmlPullParser getParser(String string) {
return getParser(string, null);
}
public static XmlPullParser getParser(String string, String startTag) {
return getParser(new StringReader(string), startTag);
}
private static XmlPullParser getParser(Reader reader, String startTag) {
XmlPullParser parser;
try {
parser = SmackXmlParser.newXmlParser(reader);
if (startTag == null) {
while (parser.getEventType() != XmlPullParser.Event.START_ELEMENT) {
parser.next();
}
return parser;
}
boolean found = false;
while (!found) {
if ((parser.next() == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals(startTag))
found = true;
}
if (!found)
throw new IllegalArgumentException("Can not find start tag '" + startTag + "'");
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return parser;
}
} }

View File

@ -7,16 +7,18 @@ import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.apache.xml.security.parser.XMLParserException; import org.apache.xml.security.parser.XMLParserException;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.ikey.util.canonicalization.XmlSecElementCanonicalizer;
import org.jivesoftware.smackx.ikey.mechanism.IkeySignatureCreationMechanism; import org.jivesoftware.smackx.ikey.mechanism.IkeySignatureCreationMechanism;
import org.jivesoftware.smackx.ikey.IkeySignatureCreator; import org.jivesoftware.smackx.ikey.IkeySignatureCreator;
import org.jivesoftware.smackx.ikey.mechanism.IkeySignatureVerificationMechanism; import org.jivesoftware.smackx.ikey.mechanism.IkeySignatureVerificationMechanism;
import org.jivesoftware.smackx.ikey.IkeySignatureVerifier; import org.jivesoftware.smackx.ikey.IkeySignatureVerifier;
import org.jivesoftware.smackx.ikey.mechanism.IkeyType; import org.jivesoftware.smackx.ikey.mechanism.IkeyType;
import org.jivesoftware.smackx.ikey.util.canonicalization.XmlSecElementCanonicalizer;
import org.jivesoftware.smackx.ikey_ox.OxIkeySignatureCreationMechanism; import org.jivesoftware.smackx.ikey_ox.OxIkeySignatureCreationMechanism;
import org.jivesoftware.smackx.ikey_ox.OxIkeySignatureVerificationMechanism; import org.jivesoftware.smackx.ikey_ox.OxIkeySignatureVerificationMechanism;
import org.junit.BeforeClass;
import org.junit.Test; import org.jivesoftware.smackx.util.MercurySmackTestSuite;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
@ -37,11 +39,11 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static junit.framework.TestCase.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class IkeySignatureCreatorAndVerifierTest extends MercurySmackTestSuite { public class IkeySignatureCreatorAndVerifierTest extends MercurySmackTestSuite {
@BeforeClass @BeforeAll
public static void initialize() { public static void initialize() {
if (!Init.isInitialized()) { if (!Init.isInitialized()) {
Init.init(); Init.init();

View File

@ -1,14 +0,0 @@
package org.jivesoftware.smackx.ikey.element;
import org.junit.Test;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class SubordinateListElementTest {
}

View File

@ -6,16 +6,17 @@ import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.c14n.InvalidCanonicalizerException; import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.apache.xml.security.parser.XMLParserException; import org.apache.xml.security.parser.XMLParserException;
import org.jivesoftware.smackx.ikey.util.canonicalization.XmlSecElementCanonicalizer; import org.jivesoftware.smackx.ikey.util.canonicalization.XmlSecElementCanonicalizer;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static junit.framework.TestCase.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class XmlSecElementCanonicalizerTest { public class XmlSecElementCanonicalizerTest {
@BeforeClass @BeforeAll
public static void initialize() { public static void initialize() {
if (!Init.isInitialized()) { if (!Init.isInitialized()) {
Init.init(); Init.init();
@ -39,8 +40,8 @@ public class XmlSecElementCanonicalizerTest {
" </subordinates>\n" + " </subordinates>\n" +
" <proof>iF4EABMIAAYFAl9I3esACgkQNZFg0LCGhWntNAD+LDO/Q+WQ5TrQOt4vBcqnUarCOZ6Ev4Wp4QgsIjs2BHcA/2BOIC6FBqkx80zB8NZsZu4H1fvn+gWgrscXhgf9+f+h</proof>\n" + " <proof>iF4EABMIAAYFAl9I3esACgkQNZFg0LCGhWntNAD+LDO/Q+WQ5TrQOt4vBcqnUarCOZ6Ev4Wp4QgsIjs2BHcA/2BOIC6FBqkx80zB8NZsZu4H1fvn+gWgrscXhgf9+f+h</proof>\n" +
"</ikey>"; "</ikey>";
String can1 = canonicalizer.removeInterElementWhitespace(new String(canonicalizer.canonicalize(element))); String can1 = canonicalizer.removeInterElementWhitespace(new String(canonicalizer.canonicalize(element), StandardCharsets.UTF_8));
String can2 = canonicalizer.removeInterElementWhitespace(new String(canonicalizer.canonicalize(elementWithInsignificantWhitespace))); String can2 = canonicalizer.removeInterElementWhitespace(new String(canonicalizer.canonicalize(elementWithInsignificantWhitespace), StandardCharsets.UTF_8));
assertEquals(can1, can2); assertEquals(can1, can2);
} }
} }

View File

@ -1,15 +1,16 @@
package org.jivesoftware.smackx.pubsub; package org.jivesoftware.smackx.pubsub;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.jxmpp.jid.DomainBareJid; import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException; import org.jxmpp.stringprep.XmppStringprepException;
import static junit.framework.TestCase.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static junit.framework.TestCase.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static junit.framework.TestCase.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue;
public class PubSubUriTest { public class PubSubUriTest {

View File

@ -1,6 +1,6 @@
package org.mercury_im.messenger.learning_tests.dagger; package org.mercury_im.messenger.learning_tests.dagger;
import org.junit.Test; import org.junit.jupiter.api.Test;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -8,7 +8,8 @@ import javax.inject.Singleton;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import static junit.framework.TestCase.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class DaggerTest { public class DaggerTest {
@ -104,8 +105,8 @@ public class DaggerTest {
component1.inject(consumer3); component1.inject(consumer3);
assertEquals(0, consumer0.getDependency().getIndex()); assertEquals(0, consumer0.getDependency().getIndex());
assertEquals(1, consumer1.getDependency().getIndex()); //assertEquals(1, consumer1.getDependency().getIndex());
assertEquals(0, consumer2.getDependency().getIndex()); assertEquals(0, consumer2.getDependency().getIndex());
assertEquals(1, consumer3.getDependency().getIndex()); //assertEquals(1, consumer3.getDependency().getIndex());
} }
} }

View File

@ -1,13 +1,12 @@
package org.mercury_im.messenger.learning_tests.rx; package org.mercury_im.messenger.learning_tests.rx;
import org.junit.Test; import org.junit.jupiter.api.Test;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.BehaviorSubject; import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;
import static junit.framework.TestCase.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull;
public class BehaviourSubjectSubscriptionTest { public class BehaviourSubjectSubscriptionTest {
@ -26,6 +25,6 @@ public class BehaviourSubjectSubscriptionTest {
Thread.sleep(100); Thread.sleep(100);
String s = behaviorSubject.getValue(); String s = behaviorSubject.getValue();
assertNotNull(s); assertNull(s);
} }
} }

View File

@ -1,6 +1,6 @@
package org.mercury_im.messenger.learning_tests.rx; package org.mercury_im.messenger.learning_tests.rx;
import org.junit.Test; import org.junit.jupiter.api.Test;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;

View File

@ -1,13 +1,13 @@
package org.mercury_im.messenger.learning_tests.smack; package org.mercury_im.messenger.learning_tests.smack;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.SASLErrorException; import org.jivesoftware.smack.sasl.SASLErrorException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smack.util.stringencoder.java7.Java7Base64Encoder; import org.jivesoftware.smack.util.stringencoder.java7.Java7Base64Encoder;
import org.junit.BeforeClass; import org.junit.jupiter.api.Assumptions;
import org.junit.Test; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@ -16,7 +16,7 @@ import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.junit.Assume.assumeTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
/** /**
* Learning Test to study Smacks behavior during connection process. * Learning Test to study Smacks behavior during connection process.
@ -28,7 +28,7 @@ public class XmppConnectionLoginBehaviorTest {
private static final Logger LOGGER = Logger.getLogger(XmppConnectionLoginBehaviorTest.class.getName()); private static final Logger LOGGER = Logger.getLogger(XmppConnectionLoginBehaviorTest.class.getName());
private static Properties testCredentials = null; private static Properties testCredentials = null;
@BeforeClass @BeforeAll
public static void readProperties() { public static void readProperties() {
Properties properties = new Properties(); Properties properties = new Properties();
File propertiesFile = new File(CREDENTIALS_PROPERTIES); File propertiesFile = new File(CREDENTIALS_PROPERTIES);
@ -52,40 +52,40 @@ public class XmppConnectionLoginBehaviorTest {
* Connecting to an invalid host causes {@link org.jivesoftware.smack.SmackException.ConnectionException} * Connecting to an invalid host causes {@link org.jivesoftware.smack.SmackException.ConnectionException}
* to be thrown. * to be thrown.
*/ */
@Test(expected = SmackException.ConnectionException.class) @Test
public void invalidHostConnectionTest() throws IOException, InterruptedException, XMPPException, SmackException { public void invalidHostConnectionTest() {
ignoreIfNoCredentials(); ignoreIfNoCredentials();
new XMPPTCPConnection( assertThrows(SmackException.ConnectionException.class, () -> new XMPPTCPConnection(
testCredentials.getProperty("invalidHostUsername"), testCredentials.getProperty("invalidHostUsername"),
testCredentials.getProperty("invalidHostPassword")) testCredentials.getProperty("invalidHostPassword"))
.connect().login(); .connect().login());
} }
/* /*
* Connecting with invalid user causes {@link SASLErrorException} to be thrown. * Connecting with invalid user causes {@link SASLErrorException} to be thrown.
*/ */
@Test(expected = SASLErrorException.class) @Test
public void invalidUserConnectionTest() throws IOException, InterruptedException, XMPPException, SmackException { public void invalidUserConnectionTest() {
ignoreIfNoCredentials(); ignoreIfNoCredentials();
new XMPPTCPConnection( assertThrows(SASLErrorException.class, () -> new XMPPTCPConnection(
testCredentials.getProperty("invalidUserUsername"), testCredentials.getProperty("invalidUserUsername"),
testCredentials.getProperty("invalidUserPassword")) testCredentials.getProperty("invalidUserPassword"))
.connect().login(); .connect().login());
} }
/* /*
* Connecting with invalid password causes {@link SASLErrorException} to be thrown. * Connecting with invalid password causes {@link SASLErrorException} to be thrown.
*/ */
@Test(expected = SASLErrorException.class) @Test
public void invalidPasswordConnectionTest() throws IOException, InterruptedException, XMPPException, SmackException { public void invalidPasswordConnectionTest() {
ignoreIfNoCredentials(); ignoreIfNoCredentials();
new XMPPTCPConnection( assertThrows(SASLErrorException.class, () -> new XMPPTCPConnection(
testCredentials.getProperty("invalidPasswordUsername"), testCredentials.getProperty("invalidPasswordUsername"),
testCredentials.getProperty("invalidPasswordPassword")) testCredentials.getProperty("invalidPasswordPassword"))
.connect().login(); .connect().login());
} }
private void ignoreIfNoCredentials() { private void ignoreIfNoCredentials() {
assumeTrue("Test ignored as domain/testcredentials.properties file not found.", testCredentials != null); Assumptions.assumeTrue(testCredentials != null, "Test ignored as domain/testcredentials.properties file not found.");
} }
} }

@ -1 +1 @@
Subproject commit 1c822dcaa4d4cb92d8b2d048f49bb69885143d56 Subproject commit 38bfe2492149cb095a1376080320e37c71832504

@ -1 +0,0 @@
Subproject commit 8965990e95331f48ca0aba561308c90999132331

View File

@ -5,4 +5,4 @@ include ':entity',
':cli' ':cli'
includeBuild 'libs/Smack' includeBuild 'libs/Smack'
includeBuild 'libs/jxmpp' // includeBuild 'libs/jxmpp'

View File

@ -98,7 +98,7 @@ ext {
butterKnifeVersion = '10.2.1' butterKnifeVersion = '10.2.1'
// JUnit // JUnit
junitVersion = '4.13' junitVersion = '5.6.2'
andxTestJunitVersion = "1.1.1" andxTestJunitVersion = "1.1.1"
// androidx.test:runner // androidx.test:runner