/** * Returns the first stanza that's sent through {@link #sendStanza(Stanza)} * and that has not been returned by earlier calls to this method. * * @return a sent packet. */ public <P extends TopLevelStreamElement> P getSentPacket() { return getSentPacket(5 * 60); }
private void answerWithEmptyRosterResult() { // We expect that the roster request is the only packet sent. This is not part of the specification, // but a shortcut in the test implementation. Stanza sentPacket = connection.getSentPacket(); if (sentPacket instanceof RosterPacket) { final IQ emptyIQ = IQ.createResultIQ((RosterPacket) sentPacket); connection.processStanza(emptyIQ); } else { assertTrue("Expected to get a RosterPacket ", false); } }
@Test public void verifyForm() throws Exception { FileTransferNegotiator fileNeg = FileTransferNegotiator.getInstanceFor(connection); try { fileNeg.negotiateOutgoingTransfer(JidTestUtil.DUMMY_AT_EXAMPLE_ORG, "streamid", "file", 1024, null, 10); } catch (NoResponseException e) { // We do not expect an answer. This unit test only checks the request sent. } Stanza packet = connection.getSentPacket(); String xml = packet.toXML(null).toString(); assertTrue(xml.indexOf("var='stream-method' type='list-single'") != -1); } }
@Test public void checkSendingPing() throws InterruptedException, SmackException, IOException, XMPPException { DummyConnection dummyCon = getAuthenticatedDummyConnection(); PingManager pinger = PingManager.getInstanceFor(dummyCon); try { pinger.ping(DUMMY_AT_EXAMPLE_ORG); } catch (SmackException e) { // Ignore the fact the server won't answer for this unit test. } Stanza sentPacket = dummyCon.getSentPacket(); assertTrue(sentPacket instanceof Ping); }
try { while (true) { final Stanza packet = connection.getSentPacket(); if (packet instanceof RosterPacket && ((IQ) packet).getType() == Type.set) { final RosterPacket rosterRequest = (RosterPacket) packet;
@Test public void testScramSha1Mechanism() throws NotConnectedException, SmackException, InterruptedException { final DummyConnection con = new DummyConnection(); SCRAMSHA1Mechanism mech = new SCRAMSHA1Mechanism() { @Override public String getRandomAscii() { this.connection = con; return "fyko+d2lbbFgONRv9qkxdawL"; } }; mech.authenticate(USERNAME, "unusedFoo", JidTestUtil.DOMAIN_BARE_JID_1, PASSWORD, null, null); AuthMechanism authMechanism = con.getSentPacket(); assertEquals(SCRAMSHA1Mechanism.NAME, authMechanism.getMechanism()); assertEquals(CLIENT_FIRST_MESSAGE, saslLayerString(authMechanism.getAuthenticationText())); mech.challengeReceived(Base64.encode(SERVER_FIRST_MESSAGE), false); Response response = con.getSentPacket(); assertEquals(CLIENT_FINAL_MESSAGE, saslLayerString(response.getAuthenticationText())); mech.challengeReceived(Base64.encode(SERVER_FINAL_MESSAGE), true); mech.checkIfSuccessfulOrThrow(); }
try { while (true) { final Stanza packet = connection.getSentPacket(); if (packet instanceof RosterPacket && ((IQ) packet).getType() == Type.set) { final RosterPacket rosterRequest = (RosterPacket) packet;
@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()); }
Stanza sentPacket = connection.getSentPacket(); if (sentPacket instanceof RosterPacket) { RosterPacket sentRP = (RosterPacket) sentPacket;
@Test public void checkProvider() throws Exception { XMLBuilder xml = XMLBuilder.create("iq"); xml.a("from", "romeo@montague.net/orchard") .a("id", "last2") .a("to", "juliet@capulet.com/balcony") .a("type", "get") .e("query") .namespace(LastActivity.NAMESPACE); DummyConnection c = new DummyConnection(); c.connect(); IQ lastRequest = PacketParserUtils.parseStanza(xml.asString()); assertTrue(lastRequest instanceof LastActivity); c.processStanza(lastRequest); Stanza reply = c.getSentPacket(); assertTrue(reply instanceof LastActivity); LastActivity l = (LastActivity) reply; assertEquals("last2", l.getStanzaId()); assertEquals(IQ.Type.result, l.getType()); } }
@Test public void receiptManagerAutoReplyTest() throws Exception { DummyConnection c = new DummyConnection(); c.connect(); DeliveryReceiptManager drm = DeliveryReceiptManager.getInstanceFor(c); drm.setAutoReceiptMode(AutoReceiptMode.always); assertEquals(AutoReceiptMode.always, drm.getAutoReceiptMode()); // test auto-receipts Message m = new Message(JidCreate.from("julia@capulet.com"), Message.Type.normal); m.setFrom(JidCreate.from("romeo@montague.com")); m.setStanzaId("test-receipt-request"); DeliveryReceiptRequest.addTo(m); // the DRM will send a reply-packet c.processStanza(m); Stanza reply = c.getSentPacket(); DeliveryReceipt r = DeliveryReceipt.from((Message) reply); assertThat("romeo@montague.com", equalsCharSequence(reply.getTo())); assertEquals("test-receipt-request", r.getId()); } }
/** * Tests that roster pushes with invalid from are ignored. * @throws XmppStringprepException * * @see <a href="http://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push">RFC 6121, Section 2.1.6</a> */ @Test public void testIgnoreInvalidFrom() throws XmppStringprepException { final BareJid spammerJid = JidCreate.entityBareFrom("spam@example.com"); RosterPacket packet = new RosterPacket(); packet.setType(Type.set); packet.setTo(connection.getUser()); packet.setFrom(JidCreate.entityBareFrom("mallory@example.com")); packet.addRosterItem(new Item(spammerJid, "Cool products!")); final String requestId = packet.getStanzaId(); // Simulate receiving the roster push connection.processStanza(packet); // Smack should reply with an error IQ ErrorIQ errorIQ = connection.getSentPacket(); assertEquals(requestId, errorIQ.getStanzaId()); assertEquals(Condition.service_unavailable, errorIQ.getError().getCondition()); assertNull("Contact was added to roster", Roster.getInstanceFor(connection).getEntry(spammerJid)); }
Stanza replyPacket = con.getSentPacket(); assertTrue(replyPacket instanceof Version);
roster.reload(); while (true) { final Stanza sentPacket = connection.getSentPacket(); if (sentPacket instanceof RosterPacket && ((IQ) sentPacket).getType() == Type.get) {