| 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.push; |
| 19 | + | |
| 20 | + | import org.junit.After; |
| 21 | + | import org.junit.Before; |
| 22 | + | import org.junit.FixMethodOrder; |
| 23 | + | import org.junit.Test; |
| 24 | + | import org.junit.runners.MethodSorters; |
| 25 | + | import tigase.component.PacketWriter; |
| 26 | + | import tigase.component.responses.AsyncCallback; |
| 27 | + | import tigase.db.TigaseDBException; |
| 28 | + | import tigase.eventbus.EventBus; |
| 29 | + | import tigase.eventbus.EventBusFactory; |
| 30 | + | import tigase.kernel.core.Kernel; |
| 31 | + | import tigase.server.Packet; |
| 32 | + | import tigase.server.PolicyViolationException; |
| 33 | + | import tigase.util.stringprep.TigaseStringprepException; |
| 34 | + | import tigase.xml.Element; |
| 35 | + | import tigase.xmpp.*; |
| 36 | + | import tigase.xmpp.impl.MessageDeliveryLogic; |
| 37 | + | import tigase.xmpp.impl.PresenceState; |
| 38 | + | import tigase.xmpp.impl.ProcessorTestCase; |
| 39 | + | import tigase.xmpp.impl.roster.RosterAbstract; |
| 40 | + | import tigase.xmpp.impl.roster.RosterFactory; |
| 41 | + | import tigase.xmpp.jid.BareJID; |
| 42 | + | import tigase.xmpp.jid.JID; |
| 43 | + | |
| 44 | + | import java.util.ArrayDeque; |
| 45 | + | import java.util.Collection; |
| 46 | + | import java.util.HashMap; |
| 47 | + | import java.util.UUID; |
| 48 | + | |
| 49 | + | import static org.junit.Assert.*; |
| 50 | + | |
| 51 | + | @FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 52 | + | public class PushPresenceTest |
| 53 | + | extends ProcessorTestCase { |
| 54 | + | |
| 55 | + | private RosterAbstract rosterUtil; |
| 56 | + | private PushPresence pushPresence; |
| 57 | + | private BareJID userJid; |
| 58 | + | private BareJID buddyJid; |
| 59 | + | |
| 60 | + | @Before |
| 61 | + | @Override |
| 62 | + | public void setUp() throws Exception { |
| 63 | + | super.setUp(); |
| 64 | + | registerLocalBeans(getKernel()); |
| 65 | + | rosterUtil = RosterFactory.newRosterInstance(RosterFactory.ROSTER_IMPL_PROP_VAL); |
| 66 | + | rosterUtil.setEventBus(getInstance(EventBus.class)); |
| 67 | + | userJid = BareJID.bareJIDInstanceNS(UUID.randomUUID().toString(), "domain.com"); |
| 68 | + | buddyJid = BareJID.bareJIDInstanceNS(UUID.randomUUID().toString(), "domain.com"); |
| 69 | + | pushPresence = getInstance(PushPresence.class); |
| 70 | + | pushPresence.setRosterUtil(rosterUtil); |
| 71 | + | } |
| 72 | + | |
| 73 | + | @After |
| 74 | + | public void tearDown() throws Exception { |
| 75 | + | getInstance(PushPresence.class).beforeUnregister(); |
| 76 | + | super.tearDown(); |
| 77 | + | } |
| 78 | + | |
| 79 | + | @Test |
| 80 | + | public void testSendingPresence_OnPushEnable_NoSubscription() throws TigaseStringprepException, TigaseDBException, NotAuthorizedException { |
| 81 | + | TestPacketWriter writer = getInstance(TestPacketWriter.class); |
| 82 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 83 | + | PushNotifications pushNotifications = getInstance(PushNotifications.class); |
| 84 | + | XMPPResourceConnection session = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(userJid, "setter")); |
| 85 | + | pushNotifications.enableNotifications(session, JID.jidInstance("push.localhost"), "test-node", new Element("enable"), null, writer::write); |
| 86 | + | assertTrue(pushPresence.isPushAvailable(userJid)); |
| 87 | + | assertEquals(0, writer.getQueue().size()); |
| 88 | + | } |
| 89 | + | |
| 90 | + | @Test |
| 91 | + | public void testSendingPresence_OnPushEnable_WithSubscription() |
| 92 | + | throws TigaseStringprepException, TigaseDBException, NotAuthorizedException, PolicyViolationException, |
| 93 | + | NoConnectionIdException { |
| 94 | + | TestPacketWriter writer = getInstance(TestPacketWriter.class); |
| 95 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 96 | + | PushNotifications pushNotifications = getInstance(PushNotifications.class); |
| 97 | + | XMPPResourceConnection buddySession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(buddyJid, "setter")); |
| 98 | + | XMPPResourceConnection userSession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(userJid, "setter")); |
| 99 | + | |
| 100 | + | updateRoster(userSession, JID.jidInstance(buddyJid), RosterAbstract.SubscriptionType.both); |
| 101 | + | updateRoster(buddySession, JID.jidInstance(userJid), RosterAbstract.SubscriptionType.both); |
| 102 | + | |
| 103 | + | pushNotifications.enableNotifications(userSession, JID.jidInstance("push.localhost"), "test-node", new Element("enable"), null, writer::write); |
| 104 | + | assertTrue(pushPresence.isPushAvailable(userJid)); |
| 105 | + | assertEquals(1, writer.getQueue().size()); |
| 106 | + | assertPresenceAway(writer.getQueue().poll()); |
| 107 | + | } |
| 108 | + | |
| 109 | + | @Test |
| 110 | + | public void testSendingPresence_OnPushDisable_NoSubscription() throws TigaseStringprepException, TigaseDBException, NotAuthorizedException { |
| 111 | + | TestPacketWriter writer = getInstance(TestPacketWriter.class); |
| 112 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 113 | + | PushNotifications pushNotifications = getInstance(PushNotifications.class); |
| 114 | + | XMPPResourceConnection session = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(userJid, "setter")); |
| 115 | + | pushNotifications.enableNotifications(session, JID.jidInstance("push.localhost"), "test-node", new Element("enable"), null, writer::write); |
| 116 | + | assertTrue(pushPresence.isPushAvailable(userJid)); |
| 117 | + | assertEquals(0, writer.getQueue().size()); |
| 118 | + | |
| 119 | + | pushNotifications.disableNotifications(session, userJid, JID.jidInstance("push.localhost"), "test-node", writer::write); |
| 120 | + | |
| 121 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 122 | + | assertEquals(0, writer.getQueue().size()); |
| 123 | + | } |
| 124 | + | |
| 125 | + | @Test |
| 126 | + | public void testSendingPresence_OnPushDisable_WithSubscription() |
| 127 | + | throws TigaseStringprepException, TigaseDBException, NotAuthorizedException, PolicyViolationException, |
| 128 | + | NoConnectionIdException, InterruptedException { |
| 129 | + | TestPacketWriter writer = getInstance(TestPacketWriter.class); |
| 130 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 131 | + | PushNotifications pushNotifications = getInstance(PushNotifications.class); |
| 132 | + | |
| 133 | + | XMPPResourceConnection buddySession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(buddyJid, "setter")); |
| 134 | + | XMPPResourceConnection userSession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(userJid, "setter")); |
| 135 | + | |
| 136 | + | pushNotifications.enableNotifications(userSession, JID.jidInstance("push.localhost"), "test-node", new Element("enable"), null, writer::write); |
| 137 | + | assertTrue(pushPresence.isPushAvailable(userJid)); |
| 138 | + | |
| 139 | + | updateRoster(userSession, JID.jidInstance(buddyJid), RosterAbstract.SubscriptionType.both); |
| 140 | + | updateRoster(buddySession, JID.jidInstance(userJid), RosterAbstract.SubscriptionType.both); |
| 141 | + | |
| 142 | + | System.out.println("packet writer " + writer); |
| 143 | + | assertEquals(1, writer.getQueue().size()); |
| 144 | + | assertEquals(1, writer.getQueue().size()); |
| 145 | + | assertPresenceAway(writer.getQueue().poll()); |
| 146 | + | |
| 147 | + | pushNotifications.disableNotifications(userSession, userJid, JID.jidInstance("push.localhost"), "test-node", writer::write); |
| 148 | + | assertEquals(1, writer.getQueue().size()); |
| 149 | + | assertPresenceUnavailable(writer.getQueue().poll()); |
| 150 | + | } |
| 151 | + | |
| 152 | + | @Test |
| 153 | + | public void testSendingPresence_OnPresenceProbe_NoSubscription() |
| 154 | + | throws TigaseStringprepException, TigaseDBException, XMPPException { |
| 155 | + | TestPacketWriter writer = getInstance(TestPacketWriter.class); |
| 156 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 157 | + | PushNotifications pushNotifications = getInstance(PushNotifications.class); |
| 158 | + | XMPPResourceConnection session = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(userJid, "setter")); |
| 159 | + | pushNotifications.enableNotifications(session, JID.jidInstance("push.localhost"), "test-node", new Element("enable"), null, writer::write); |
| 160 | + | assertTrue(pushPresence.isPushAvailable(userJid)); |
| 161 | + | assertEquals(0, writer.getQueue().size()); |
| 162 | + | |
| 163 | + | PresenceState presenceState = getInstance(PresenceState.class); |
| 164 | + | Packet probe = Packet.packetInstance(new Element("presence").withAttribute("type", StanzaType.probe.name()) |
| 165 | + | .withAttribute("from", buddyJid.toString()) |
| 166 | + | .withAttribute("to", userJid.toString())); |
| 167 | + | presenceState.process(probe, null, null, writer.getQueue(), new HashMap<>()); |
| 168 | + | assertEquals(0, writer.getQueue().size()); |
| 169 | + | } |
| 170 | + | |
| 171 | + | @Test |
| 172 | + | public void testSendingPresence_OnSubscriptionAndOnPresenceProbe_WithSubscription() |
| 173 | + | throws TigaseStringprepException, TigaseDBException, XMPPException, PolicyViolationException { |
| 174 | + | TestPacketWriter writer = getInstance(TestPacketWriter.class); |
| 175 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 176 | + | PushNotifications pushNotifications = getInstance(PushNotifications.class); |
| 177 | + | XMPPResourceConnection userSession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(userJid, "setter")); |
| 178 | + | pushNotifications.enableNotifications(userSession, JID.jidInstance("push.localhost"), "test-node", new Element("enable"), null, writer::write); |
| 179 | + | XMPPResourceConnection buddySession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(buddyJid, "setter")); |
| 180 | + | |
| 181 | + | updateRoster(userSession, JID.jidInstance(buddyJid), RosterAbstract.SubscriptionType.both); |
| 182 | + | updateRoster(buddySession, JID.jidInstance(userJid), RosterAbstract.SubscriptionType.both); |
| 183 | + | |
| 184 | + | assertTrue(pushPresence.isPushAvailable(userJid)); |
| 185 | + | // add user roster subscription triggers sending presence |
| 186 | + | assertEquals(1, writer.getQueue().size()); |
| 187 | + | assertPresenceAway(writer.getQueue().poll()); |
| 188 | + | assertEquals(0, writer.getQueue().size()); |
| 189 | + | |
| 190 | + | PresenceState presenceState = getInstance(PresenceState.class); |
| 191 | + | Packet probe = Packet.packetInstance(new Element("presence").withAttribute("type", StanzaType.probe.name()) |
| 192 | + | .withAttribute("from", buddyJid.toString()) |
| 193 | + | .withAttribute("to", userJid.toString())); |
| 194 | + | presenceState.process(probe, null, null, writer.getQueue(), new HashMap<>()); |
| 195 | + | assertEquals(1, writer.getQueue().size()); |
| 196 | + | assertPresenceAway(writer.getQueue().poll()); |
| 197 | + | } |
| 198 | + | |
| 199 | + | @Test |
| 200 | + | public void testSendingPresence_OnSubscriptionAndOnRemovingSubscription() |
| 201 | + | throws TigaseStringprepException, TigaseDBException, XMPPException, PolicyViolationException { |
| 202 | + | TestPacketWriter writer = getInstance(TestPacketWriter.class); |
| 203 | + | assertFalse(pushPresence.isPushAvailable(userJid)); |
| 204 | + | PushNotifications pushNotifications = getInstance(PushNotifications.class); |
| 205 | + | XMPPResourceConnection userSession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(userJid, "setter")); |
| 206 | + | pushNotifications.enableNotifications(userSession, JID.jidInstance("push.localhost"), "test-node", new Element("enable"), null, writer::write); |
| 207 | + | XMPPResourceConnection buddySession = getSession(JID.jidInstance(UUID.randomUUID().toString(), "domain", null), JID.jidInstance(buddyJid, "setter")); |
| 208 | + | |
| 209 | + | updateRoster(userSession, JID.jidInstance(buddyJid), RosterAbstract.SubscriptionType.both); |
| 210 | + | updateRoster(buddySession, JID.jidInstance(userJid), RosterAbstract.SubscriptionType.both); |
| 211 | + | |
| 212 | + | assertTrue(pushPresence.isPushAvailable(userJid)); |
| 213 | + | // add user roster subscription triggers sending presence |
| 214 | + | System.out.println("packet writer " + writer + " - in test"); |
| 215 | + | assertEquals(1, writer.getQueue().size()); |
| 216 | + | assertPresenceAway(writer.getQueue().poll()); |
| 217 | + | assertEquals(0, writer.getQueue().size()); |
| 218 | + | |
| 219 | + | updateRoster(userSession, JID.jidInstance(buddyJid), RosterAbstract.SubscriptionType.none); |
| 220 | + | updateRoster(buddySession, JID.jidInstance(userJid), RosterAbstract.SubscriptionType.none); |
| 221 | + | |
| 222 | + | assertEquals(1, writer.getQueue().size()); |
| 223 | + | assertPresenceUnavailable(writer.getQueue().poll()); |
| 224 | + | } |
| 225 | + | |
| 226 | + | private void assertPresenceUnavailable(Packet presence) { |
| 227 | + | assertEquals("presence", presence.getElemName()); |
| 228 | + | assertEquals(StanzaType.unavailable, presence.getType()); |
| 229 | + | } |
| 230 | + | |
| 231 | + | private void assertPresenceAway(Packet presence) { |
| 232 | + | assertEquals("presence", presence.getElemName()); |
| 233 | + | assertEquals(null, presence.getType()); |
| 234 | + | assertEquals(JID.jidInstance(buddyJid), presence.getStanzaTo()); |
| 235 | + | assertEquals("xa", presence.getElemCDataStaticStr(new String[] { "presence", "show"})); |
| 236 | + | } |
| 237 | + | |
| 238 | + | private void updateRoster(XMPPResourceConnection session, JID buddy, RosterAbstract.SubscriptionType subscriptionType) |
| 239 | + | throws TigaseDBException, NotAuthorizedException, PolicyViolationException, NoConnectionIdException { |
| 240 | + | rosterUtil.addBuddy(session, buddy, null, null, null); |
| 241 | + | rosterUtil.setBuddySubscription(session, subscriptionType, buddy); |
| 242 | + | Element new_buddy = rosterUtil.getBuddyItem(session, buddy); |
| 243 | + | rosterUtil.updateBuddyChange(session, new ArrayDeque<>(), new_buddy); |
| 244 | + | try { |
| 245 | + | Thread.sleep(1000); |
| 246 | + | } catch (Throwable ex) {} |
| 247 | + | } |
| 248 | + | |
| 249 | + | protected void registerLocalBeans(Kernel kernel) { |
| 250 | + | super.registerBeans(kernel); |
| 251 | + | |
| 252 | + | kernel.registerBean("eventBus").asInstance(EventBusFactory.getInstance()).exportable().exec(); |
| 253 | + | kernel.registerBean("sess-man").asInstance(this.getSessionManagerHandler()).setActive(true).exportable().exec();//.asClass(DummySessionManager.class).setActive(true).exportable().exec(); |
| 254 | + | kernel.registerBean(MessageDeliveryLogic.class).setActive(true).exportable().exec(); |
| 255 | + | kernel.registerBean("msgRepository").asClass(PushNotificationsTest.MsgRepositoryIfcImpl.class).exportable().exec(); |
| 256 | + | kernel.registerBean(PushNotifications.class).setActive(true).exec(); |
| 257 | + | kernel.registerBean("writer").asInstance(new TestPacketWriter()).exec(); |
| 258 | + | kernel.registerBean(PushPresence.class).setActive(true).exec(); |
| 259 | + | kernel.registerBean(PresenceState.class).setActive(true).exec(); |
| 260 | + | System.out.println("registering local beans done."); |
| 261 | + | } |
| 262 | + | |
| 263 | + | protected static class TestPacketWriter implements PacketWriter { |
| 264 | + | |
| 265 | + | private final ArrayDeque<Packet> queue = new ArrayDeque<>(); |
| 266 | + | |
| 267 | + | public TestPacketWriter() { |
| 268 | + | System.out.println("creating new instance " + this + "...."); |
| 269 | + | } |
| 270 | + | |
| 271 | + | @Override |
| 272 | + | public synchronized void write(Collection<Packet> packets) { |
| 273 | + | queue.addAll(packets); |
| 274 | + | } |
| 275 | + | |
| 276 | + | @Override |
| 277 | + | public synchronized void write(Packet packet) { |
| 278 | + | queue.add(packet); |
| 279 | + | } |
| 280 | + | |
| 281 | + | @Override |
| 282 | + | public synchronized void write(Packet packet, AsyncCallback callback) { |
| 283 | + | queue.add(packet); |
| 284 | + | } |
| 285 | + | |
| 286 | + | public synchronized ArrayDeque<Packet> getQueue() { |
| 287 | + | return queue; |
| 288 | + | } |
| 289 | + | } |
| 290 | + | } |