From 72f2f6c59a173087cec2d3cc17ff59808f2f4a74 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Wed, 9 Aug 2017 20:46:13 +0200 Subject: [PATCH] Add JetElementTest --- .../smackx/jet/component/JetSecurity.java | 3 +- .../smackx/jet/JetElementTest.java | 97 +++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 smack-experimental/src/test/java/org/jivesoftware/smackx/jet/JetElementTest.java diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jet/component/JetSecurity.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jet/component/JetSecurity.java index 2e167d0d9..ac94dd6bd 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jet/component/JetSecurity.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jet/component/JetSecurity.java @@ -82,13 +82,12 @@ public class JetSecurity extends JingleSecurity { SmackException.NotConnectedException, SmackException.NoResponseException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException, InvalidKeyException, NoSuchPaddingException { byte[] keyAndIv = method.decryptJingleTransfer(sender, child); - LOGGER.log(Level.INFO, "Transported JET Key has length: " + keyAndIv.length); aesKey = AesGcmNoPadding.createDecryptionKey(cipherName, keyAndIv); } @Override public JetSecurityElement getElement() { - return new JetSecurityElement(getParent().getName(), cipherName, child); + return new JetSecurityElement(name, cipherName, child); } @Override diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/jet/JetElementTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/jet/JetElementTest.java new file mode 100644 index 000000000..d0952ab9b --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/jet/JetElementTest.java @@ -0,0 +1,97 @@ +/** + * + * 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.jet; + +import static junit.framework.TestCase.assertEquals; +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.test.util.SmackTestSuite; +import org.jivesoftware.smackx.ciphers.Aes128GcmNoPadding; +import org.jivesoftware.smackx.jet.component.JetSecurity; +import org.jivesoftware.smackx.jet.element.JetSecurityElement; + +import org.junit.Test; +import org.jxmpp.jid.FullJid; +import org.xml.sax.SAXException; + +public class JetElementTest extends SmackTestSuite { + + @Test + public void jetTest() throws InterruptedException, JingleEncryptionMethod.JingleEncryptionException, NoSuchAlgorithmException, SmackException.NotConnectedException, SmackException.NoResponseException, IOException, SAXException { + ExtensionElement child = new SecurityStub().encryptJingleTransfer(null, null); + JetSecurityElement element = new JetSecurityElement("content1", Aes128GcmNoPadding.NAMESPACE, child); + JetSecurity security = new JetSecurity(element); + assertEquals(SecurityStub.NAMESPACE, security.getMethodNamespace()); + assertEquals(Aes128GcmNoPadding.NAMESPACE, element.getCipherName()); + assertEquals(SecurityStub.NAMESPACE, element.getMethodNamespace()); + assertEquals("content1", element.getName()); + + String xml = "" + + "" + + ""; + assertXMLEqual(xml, security.getElement().toXML().toString()); + } + + private static class SecurityStub implements JingleEncryptionMethod { + public static final String NAMESPACE = "urn:xmpp:security-stub"; + + @Override + public ExtensionElement encryptJingleTransfer(FullJid recipient, byte[] keyData) throws JingleEncryptionException, InterruptedException, NoSuchAlgorithmException, SmackException.NotConnectedException, SmackException.NoResponseException { + return new ExtensionElement() { + @Override + public String getNamespace() { + return NAMESPACE; + } + + @Override + public String getElementName() { + return "security-stub"; + } + + @Override + public CharSequence toXML() { + return ""; + } + }; + } + + @Override + public byte[] decryptJingleTransfer(FullJid sender, ExtensionElement encryptionElement) throws JingleEncryptionException, InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { + return new byte[0]; + } + + @Override + public XMPPConnection getConnection() { + return null; + } + + @Override + public String getNamespace() { + return NAMESPACE; + } + } +}