1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 18:48:00 +01:00

Merge pull request #250 from vanitasvitae/fixEmeAdding

Fix ClassCastException in hasProtocol method
This commit is contained in:
Florian Schmaus 2018-07-21 14:24:15 +02:00 committed by GitHub
commit c4c15c3e0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 2 deletions

View file

@ -155,11 +155,12 @@ public class ExplicitMessageEncryptionElement implements ExtensionElement {
* @return true if message has EME element for that namespace, otherwise false
*/
public static boolean hasProtocol(Message message, String protocolNamespace) {
List<ExplicitMessageEncryptionElement> emeElements = message.getExtension(
List<ExtensionElement> extensionElements = message.getExtensions(
ExplicitMessageEncryptionElement.ELEMENT,
ExplicitMessageEncryptionElement.NAMESPACE);
for (ExplicitMessageEncryptionElement e : emeElements) {
for (ExtensionElement extensionElement : extensionElements) {
ExplicitMessageEncryptionElement e = (ExplicitMessageEncryptionElement) extensionElement;
if (e.getEncryptionNamespace().equals(protocolNamespace)) {
return true;
}

View file

@ -0,0 +1,77 @@
/**
*
* Copyright 2018 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.eme;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
import org.junit.Test;
public class ExplicitMessageEncryptionElementTest extends SmackTestSuite {
@Test
public void addToMessageTest() {
Message message = new Message();
// Check inital state (no elements)
assertNull(ExplicitMessageEncryptionElement.from(message));
assertFalse(ExplicitMessageEncryptionElement.hasProtocol(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl));
List<ExtensionElement> extensions = message.getExtensions();
assertEquals(0, extensions.size());
// Add OMEMO
ExplicitMessageEncryptionElement.set(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl);
extensions = message.getExtensions();
assertEquals(1, extensions.size());
assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl));
assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl.getNamespace()));
assertFalse(ExplicitMessageEncryptionElement.hasProtocol(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
assertFalse(ExplicitMessageEncryptionElement.hasProtocol(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0.getNamespace()));
ExplicitMessageEncryptionElement.set(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0);
extensions = message.getExtensions();
assertEquals(2, extensions.size());
assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl));
// Check, if adding additional OMEMO wont add another element
ExplicitMessageEncryptionElement.set(message,
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl);
extensions = message.getExtensions();
assertEquals(2, extensions.size());
}
}