protected final void initializeAsResultFor(IQ request) { if (!(request.getType() == Type.get || request.getType() == Type.set)) { throw new IllegalArgumentException( "IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML(null)); } setStanzaId(request.getStanzaId()); setFrom(request.getTo()); setTo(request.getFrom()); setType(Type.result); }
/** * If no listeners are registered for incoming In-Band Bytestream requests, all request should * be rejected with an error. * * @throws Exception should not happen */ @Test public void shouldRespondWithError() throws Exception { // run the listener with the initiation packet initiationListener.handleIQRequest(initBytestream); // wait because packet is processed in an extra thread Thread.sleep(200); // capture reply to the In-Band Bytestream open request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.not_acceptable, argument.getValue().getError().getCondition()); }
/** * If listener for a specific user is registered it should not be notified on incoming requests * from other users. * * @throws Exception should not happen */ @Test public void shouldNotInvokeListenerForUser() throws Exception { // add listener for request of user "other_initiator" InBandBytestreamListener listener = mock(InBandBytestreamListener.class); byteStreamManager.addIncomingBytestreamListener(listener, JidCreate.from("other_" + initiatorJID)); // run the listener with the initiation packet initiationListener.handleIQRequest(initBytestream); // wait because packet is processed in an extra thread Thread.sleep(200); // assert listener is not called ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class); verify(listener, never()).incomingBytestreamRequest(byteStreamRequest.capture()); // capture reply to the In-Band Bytestream open request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.not_acceptable, argument.getValue().getError().getCondition()); }
/** * Open request with a block size that exceeds the maximum block size should be replied with an * resource-constraint error. * * @throws Exception should not happen */ @Test public void shouldRejectRequestWithTooBigBlockSize() throws Exception { byteStreamManager.setMaximumBlockSize(1024); // run the listener with the initiation packet initiationListener.handleIQRequest(initBytestream); // wait because packet is processed in an extra thread Thread.sleep(200); // capture reply to the In-Band Bytestream open request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.resource_constraint, argument.getValue().getError().getCondition()); }
/** * If no listeners are registered for incoming SOCKS5 Bytestream requests, all request should be * rejected with an error. * * @throws Exception should not happen */ @Test public void shouldRespondWithError() throws Exception { // run the listener with the initiation packet initiationListener.handleIQRequest(initBytestream); // capture reply to the SOCKS5 Bytestream initiation ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection, timeout(TIMEOUT)).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.not_acceptable, argument.getValue().getError().getCondition()); }
/** * If listener for a specific user is registered it should not be notified on incoming requests * from other users. * * @throws Exception should not happen */ @Test public void shouldNotInvokeListenerForUser() throws Exception { // add listener for request of user "other_initiator" Socks5BytestreamListener listener = mock(Socks5BytestreamListener.class); byteStreamManager.addIncomingBytestreamListener(listener, JidCreate.from("other_" + initiatorJID)); // run the listener with the initiation packet initiationListener.handleIQRequest(initBytestream); // assert listener is not called ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class); verify(listener, never()).incomingBytestreamRequest(byteStreamRequest.capture()); // capture reply to the SOCKS5 Bytestream initiation ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection, timeout(TIMEOUT)).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.not_acceptable, argument.getValue().getError().getCondition()); }
@Override public IQ handleIQRequest(IQ iqRequest) { if (!enabled) return IQ.createErrorResponse(iqRequest, Condition.not_acceptable); LastActivity message = new LastActivity(); message.setType(IQ.Type.result); message.setTo(iqRequest.getFrom()); message.setFrom(iqRequest.getTo()); message.setStanzaId(iqRequest.getStanzaId()); message.setLastActivity(getIdleTime()); return message; } });
/** * Test reject() method. * @throws NotConnectedException * @throws InterruptedException */ @Test public void shouldReplyWithErrorIfRequestIsRejected() throws NotConnectedException, InterruptedException { InBandBytestreamRequest ibbRequest = new InBandBytestreamRequest( byteStreamManager, initBytestream); // reject request ibbRequest.reject(); // capture reply to the In-Band Bytestream open request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.not_acceptable, argument.getValue().getError().getCondition()); }
/** * Convenience method to create a new {@link Type#error IQ.Type.error} IQ * based on a {@link Type#get IQ.Type.get} or {@link Type#set IQ.Type.set} * IQ. The new stanza will be initialized with:<ul> * <li>The sender set to the recipient of the originating IQ. * <li>The recipient set to the sender of the originating IQ. * <li>The type set to {@link Type#error IQ.Type.error}. * <li>The id set to the id of the originating IQ. * <li>The child element contained in the associated originating IQ. * <li>The provided {@link StanzaError XMPPError}. * </ul> * * @param request the {@link Type#get IQ.Type.get} or {@link Type#set IQ.Type.set} IQ packet. * @param error the error to associate with the created IQ packet. * @throws IllegalArgumentException if the IQ stanza does not have a type of * {@link Type#get IQ.Type.get} or {@link Type#set IQ.Type.set}. * @return a new {@link Type#error IQ.Type.error} IQ based on the originating IQ. */ public static ErrorIQ createErrorResponse(final IQ request, final StanzaError.Builder error) { if (!(request.getType() == Type.get || request.getType() == Type.set)) { throw new IllegalArgumentException( "IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML(null)); } final ErrorIQ result = new ErrorIQ(error); result.setStanzaId(request.getStanzaId()); result.setFrom(request.getTo()); result.setTo(request.getFrom()); error.setStanza(result); return result; }
/** * Test accept() method. * * @throws Exception should not happen */ @Test public void shouldReturnSessionIfRequestIsAccepted() throws Exception { InBandBytestreamRequest ibbRequest = new InBandBytestreamRequest( byteStreamManager, initBytestream); // accept request InBandBytestreamSession session = ibbRequest.accept(); // capture reply to the In-Band Bytestream open request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct acknowledgment packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.result, argument.getValue().getType()); assertNotNull(session); assertNotNull(session.getInputStream()); assertNotNull(session.getOutputStream()); }
/** * If a close request to an unknown session is received it should be replied * with an <item-not-found/> error. * * @throws Exception should not happen */ @Test public void shouldReplyErrorIfSessionIsUnknown() throws Exception { // mock connection XMPPConnection connection = mock(XMPPConnection.class); // initialize InBandBytestreamManager to get the CloseListener InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); // get the CloseListener from InBandByteStreamManager CloseListener closeListener = Whitebox.getInternalState(byteStreamManager, CloseListener.class); Close close = new Close("unknownSessionId"); close.setFrom(initiatorJID); close.setTo(targetJID); closeListener.handleIQRequest(close); // wait because packet is processed in an extra thread Thread.sleep(200); // capture reply to the In-Band Bytestream close request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.item_not_found, argument.getValue().getError().getCondition()); }
if (!iq.getTo().equals(me)) { return false;
assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.item_not_found,
throw new IllegalArgumentException("IQ must be a request IQ, i.e. of type 'get' or 'set'."); to = iqPacket.getTo(); local = conn.getUser(); if (local == null) {
/** * Test creating a simple and empty IQ response. * @throws XmppStringprepException */ @Test public void testGeneratingSimpleResponse() throws XmppStringprepException { final IQ request = new TestIQ(ELEMENT, NAMESPACE); request.setFrom(JidCreate.from("sender@test/Smack")); request.setTo(JidCreate.from("receiver@test/Smack")); final IQ result = IQ.createResultIQ(request); assertEquals(IQ.Type.result, result.getType()); assertNotNull(result.getStanzaId()); assertEquals(request.getStanzaId(), result.getStanzaId()); assertEquals(request.getFrom(), result.getTo()); assertEquals(request.getTo(), result.getFrom()); assertEquals("", result.getChildElementXML().toString()); }
@Test public void checkProvider() throws Exception { // @formatter:off String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>" + "<ping xmlns='urn:xmpp:ping'/>" + "</iq>"; // @formatter:on DummyConnection con = new DummyConnection(); con.connect(); // Enable ping for this connection PingManager.getInstanceFor(con); IQ pingRequest = PacketParserUtils.parseStanza(control); assertTrue(pingRequest instanceof Ping); con.processStanza(pingRequest); Stanza pongPacket = con.getSentPacket(); assertTrue(pongPacket instanceof IQ); IQ pong = (IQ) pongPacket; assertThat("capulet.lit", equalsCharSequence(pong.getTo())); assertEquals("s2c1", pong.getStanzaId()); assertEquals(IQ.Type.result, pong.getType()); }
jout.setFrom(iq.getTo()); } else { jout.setFrom(me);
/** * Test creating a error response based on an IQ request. * @throws XmppStringprepException */ @Test public void testGeneratingValidErrorResponse() throws XmppStringprepException { final StanzaError.Builder error = StanzaError.getBuilder(StanzaError.Condition.bad_request); final IQ request = new TestIQ(ELEMENT, NAMESPACE); request.setType(IQ.Type.set); request.setFrom(JidCreate.from("sender@test/Smack")); request.setTo(JidCreate.from("receiver@test/Smack")); final IQ result = IQ.createErrorResponse(request, error); assertEquals(IQ.Type.error, result.getType()); assertNotNull(result.getStanzaId()); assertEquals(request.getStanzaId(), result.getStanzaId()); assertEquals(request.getFrom(), result.getTo()); assertEquals(error.build().toXML(), result.getError().toXML()); // TODO this test was never valid // assertEquals(CHILD_ELEMENT, result.getChildElementXML()); }
if (responseIQ != null) responseIQ.setFrom(iq.getTo()); responseIQ.setStanzaId(iq.getStanzaId()); responseIQ.setTo(iq.getFrom());
protected final void initializeAsResultFor(IQ request) { if (!(request.getType() == Type.get || request.getType() == Type.set)) { throw new IllegalArgumentException( "IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML(null)); } setStanzaId(request.getStanzaId()); setFrom(request.getTo()); setTo(request.getFrom()); setType(Type.result); }