1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-01 18:52:08 +01:00

Fix ClassCastException in hasProtocol method

This commit is contained in:
Paul Schaub 2018-07-04 16:26:52 +02:00
parent 8011ba96bb
commit 29e2a633d8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
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 * @return true if message has EME element for that namespace, otherwise false
*/ */
public static boolean hasProtocol(Message message, String protocolNamespace) { public static boolean hasProtocol(Message message, String protocolNamespace) {
List<ExplicitMessageEncryptionElement> emeElements = message.getExtension( List<ExtensionElement> extensionElements = message.getExtensions(
ExplicitMessageEncryptionElement.ELEMENT, ExplicitMessageEncryptionElement.ELEMENT,
ExplicitMessageEncryptionElement.NAMESPACE); ExplicitMessageEncryptionElement.NAMESPACE);
for (ExplicitMessageEncryptionElement e : emeElements) { for (ExtensionElement extensionElement : extensionElements) {
ExplicitMessageEncryptionElement e = (ExplicitMessageEncryptionElement) extensionElement;
if (e.getEncryptionNamespace().equals(protocolNamespace)) { if (e.getEncryptionNamespace().equals(protocolNamespace)) {
return true; 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());
}
}