| 1 | + | /* |
| 2 | + | * Tigase XMPP Server - The instant messaging server |
| 3 | + | * Copyright (C) 2004 Tigase, Inc. (office@tigase.com) |
| 4 | + | * |
| 5 | + | * This program is free software: you can redistribute it and/or modify |
| 6 | + | * it under the terms of the GNU Affero General Public License as published by |
| 7 | + | * the Free Software Foundation, version 3 of the License. |
| 8 | + | * |
| 9 | + | * This program is distributed in the hope that it will be useful, |
| 10 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + | * GNU Affero General Public License for more details. |
| 13 | + | * |
| 14 | + | * You should have received a copy of the GNU Affero General Public License |
| 15 | + | * along with this program. Look for COPYING file in the top folder. |
| 16 | + | * If not, see http://www.gnu.org/licenses/. |
| 17 | + | */ |
| 18 | + | package tigase.xmpp.impl; |
| 19 | + | |
| 20 | + | import org.junit.Before; |
| 21 | + | import org.junit.Test; |
| 22 | + | import tigase.kernel.core.Kernel; |
| 23 | + | import tigase.server.Packet; |
| 24 | + | import tigase.vhosts.DummyVHostManager; |
| 25 | + | import tigase.xml.Element; |
| 26 | + | import tigase.xmpp.jid.BareJID; |
| 27 | + | import tigase.xmpp.jid.JID; |
| 28 | + | |
| 29 | + | import java.util.UUID; |
| 30 | + | |
| 31 | + | import static org.junit.Assert.assertFalse; |
| 32 | + | import static org.junit.Assert.assertTrue; |
| 33 | + | |
| 34 | + | /** |
| 35 | + | * @author andrzej |
| 36 | + | */ |
| 37 | + | public class MessageDeliveryLogicTest |
| 38 | + | extends ProcessorTestCase { |
| 39 | + | |
| 40 | + | final static String domain = "example.com"; |
| 41 | + | private MessageDeliveryLogic messageDeliveryLogic; |
| 42 | + | |
| 43 | + | @Override |
| 44 | + | protected void registerBeans(Kernel kernel) { |
| 45 | + | super.registerBeans(kernel); |
| 46 | + | kernel.registerBean(MessageDeliveryLogic.class).exec(); |
| 47 | + | } |
| 48 | + | |
| 49 | + | @Before |
| 50 | + | @Override |
| 51 | + | public void setUp() throws Exception { |
| 52 | + | super.setUp(); |
| 53 | + | messageDeliveryLogic = getInstance(MessageDeliveryLogic.class); |
| 54 | + | var vHostManager = getInstance(DummyVHostManager.class); |
| 55 | + | vHostManager.addVhost(domain); |
| 56 | + | } |
| 57 | + | |
| 58 | + | @Test |
| 59 | + | public void testProcessingErrorMessageBareJidOffline() throws Exception { |
| 60 | + | var destinationUserJid = BareJID.bareJIDInstance("user1", domain); |
| 61 | + | var packetElelement = new Element("message", new String[]{"type", "from", "to"}, |
| 62 | + | new String[]{"error", "remote-user@test.com/res1", |
| 63 | + | destinationUserJid.toString()}); |
| 64 | + | var packet = Packet.packetInstance(packetElelement); |
| 65 | + | final boolean block = messageDeliveryLogic.preProcessFilter(packet, null); |
| 66 | + | assertTrue("BareJID: No Available or Connected Resources > 8.5.2.2.1. Message: drop message", block); |
| 67 | + | } |
| 68 | + | |
| 69 | + | @Test |
| 70 | + | public void testProcessingErrorMessageBareJidOnline() throws Exception { |
| 71 | + | var destinationUserJid = BareJID.bareJIDInstance("user1", domain); |
| 72 | + | var fullDestinationJid = JID.jidInstance(destinationUserJid, "res1"); |
| 73 | + | var session = getSession(JID.jidInstance("c2s@example.com/" + UUID.randomUUID()), |
| 74 | + | fullDestinationJid); |
| 75 | + | |
| 76 | + | var packetElelement = new Element("message", new String[]{"type", "from", "to"}, |
| 77 | + | new String[]{"error", "remote-user@test.com/res1", |
| 78 | + | destinationUserJid.toString()}); |
| 79 | + | var packet = Packet.packetInstance(packetElelement); |
| 80 | + | final boolean block = messageDeliveryLogic.preProcessFilter(packet, session); |
| 81 | + | assertTrue("BareJID: Available or Connected Resources > 8.5.2.1.1. Message: drop message", block); |
| 82 | + | } |
| 83 | + | |
| 84 | + | @Test |
| 85 | + | public void testProcessingErrorMessageFullJidOnlineWithMatchingResource() throws Exception { |
| 86 | + | var destinationUserJid = BareJID.bareJIDInstance("user1", domain); |
| 87 | + | var fullDestinationJid = JID.jidInstance(destinationUserJid, "res1"); |
| 88 | + | var session = getSession(JID.jidInstance("c2s@example.com/" + UUID.randomUUID()), |
| 89 | + | fullDestinationJid); |
| 90 | + | |
| 91 | + | var packetElelement = new Element("message", new String[]{"type", "from", "to"}, |
| 92 | + | new String[]{"error", "remote-user@test.com/res1", |
| 93 | + | fullDestinationJid.toString()}); |
| 94 | + | var packet = Packet.packetInstance(packetElelement); |
| 95 | + | final boolean block = messageDeliveryLogic.preProcessFilter(packet, session); |
| 96 | + | assertFalse("FullJID: 8.5.3.1. Resource Matches. Message: deliver message", block); |
| 97 | + | } |
| 98 | + | |
| 99 | + | @Test |
| 100 | + | public void testProcessingErrorMessageFullJidOnlineWithouthMatchingResource() throws Exception { |
| 101 | + | var destinationUserJid = BareJID.bareJIDInstance("user1", domain); |
| 102 | + | var fullDestinationJidPacketTo = JID.jidInstance(destinationUserJid, "res1"); |
| 103 | + | var fullDestinationJidSession = JID.jidInstance(destinationUserJid, "res2"); |
| 104 | + | var session = getSession(JID.jidInstance("c2s@example.com/" + UUID.randomUUID()), |
| 105 | + | fullDestinationJidSession); |
| 106 | + | |
| 107 | + | var packetElelement = new Element("message", new String[]{"type", "from", "to"}, |
| 108 | + | new String[]{"error", "remote-user@test.com/res1", |
| 109 | + | fullDestinationJidPacketTo.toString()}); |
| 110 | + | var packet = Packet.packetInstance(packetElelement); |
| 111 | + | final boolean block = messageDeliveryLogic.preProcessFilter(packet, session); |
| 112 | + | assertTrue("FullJID: No Resource Matches > 8.5.3.2.1. Message: drop message", block); |
| 113 | + | } |
| 114 | + | |
| 115 | + | @Test |
| 116 | + | public void testProcessingErrorMessagePubSubPayload() throws Exception { |
| 117 | + | |
| 118 | + | var destinationChannelJid = JID.jidInstance("channel", "mix." + domain, UUID.randomUUID().toString()); |
| 119 | + | var session = getSession(JID.jidInstance("c2s@example.com/" + UUID.randomUUID()), |
| 120 | + | JID.jidInstance("user1@example.com/res1")); |
| 121 | + | |
| 122 | + | var packetElelement = new Element("message", new String[]{"type", "from", "to"}, |
| 123 | + | new String[]{"error", "remote-user@test.com/res1", |
| 124 | + | destinationChannelJid.toString()}); |
| 125 | + | var packet = Packet.packetInstance(packetElelement); |
| 126 | + | boolean block = messageDeliveryLogic.preProcessFilter(packet, session); |
| 127 | + | assertFalse("Message error addressed to PubSub/MIX should be correctly forwarded to the component", block); |
| 128 | + | |
| 129 | + | var destinationChannelFullJid = BareJID.bareJIDInstance("channel", "mix." + domain); |
| 130 | + | packetElelement = new Element("message", new String[]{"type", "from", "to"}, |
| 131 | + | new String[]{"error", "remote-user@test.com/res1", |
| 132 | + | destinationChannelFullJid.toString()}); |
| 133 | + | packet = Packet.packetInstance(packetElelement); |
| 134 | + | block = messageDeliveryLogic.preProcessFilter(packet, session); |
| 135 | + | assertFalse("Message error addressed to PubSub/MIX should be correctly forwarded to the component", block); |
| 136 | + | } |
| 137 | + | } |
| 138 | + | |