Search in sources :

Example 11 with Item

use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.

the class RosterTest method testIgnoreInvalidFrom.

/**
     * 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 = (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));
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Test(org.junit.Test)

Example 12 with Item

use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.

the class RosterTest method initRoster.

/**
     * Initialize the roster according to the example in
     * <a href="http://xmpp.org/rfcs/rfc3921.html#roster-login"
     *     >RFC3921: Retrieving One's Roster on Login</a>.
     * 
     * @param connection the dummy connection of which the provided roster belongs to.
     * @param roster the roster (or buddy list) which should be initialized.
     * @throws SmackException 
     * @throws XmppStringprepException 
     */
private void initRoster() throws InterruptedException, SmackException, XmppStringprepException {
    roster.reload();
    while (true) {
        final Stanza sentPacket = connection.getSentPacket();
        if (sentPacket instanceof RosterPacket && ((IQ) sentPacket).getType() == Type.get) {
            // setup the roster get request
            final RosterPacket rosterRequest = (RosterPacket) sentPacket;
            assertSame("The <query/> element MUST NOT contain any <item/> child elements!", 0, rosterRequest.getRosterItemCount());
            // prepare the roster result
            final RosterPacket rosterResult = new RosterPacket();
            rosterResult.setTo(connection.getUser());
            rosterResult.setType(Type.result);
            rosterResult.setStanzaId(rosterRequest.getStanzaId());
            // prepare romeo's roster entry
            final Item romeo = new Item(JidCreate.entityBareFrom("romeo@example.net"), "Romeo");
            romeo.addGroupName("Friends");
            romeo.setItemType(ItemType.both);
            rosterResult.addRosterItem(romeo);
            // prepare mercutio's roster entry
            final Item mercutio = new Item(JidCreate.entityBareFrom("mercutio@example.com"), "Mercutio");
            mercutio.setItemType(ItemType.from);
            rosterResult.addRosterItem(mercutio);
            // prepare benvolio's roster entry
            final Item benvolio = new Item(JidCreate.entityBareFrom("benvolio@example.net"), "Benvolio");
            benvolio.setItemType(ItemType.both);
            rosterResult.addRosterItem(benvolio);
            // simulate receiving the roster result and exit the loop
            connection.processStanza(rosterResult);
            break;
        }
    }
    roster.waitUntilLoaded();
    rosterListener.waitUntilInvocationOrTimeout();
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Stanza(org.jivesoftware.smack.packet.Stanza) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ)

Example 13 with Item

use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.

the class RosterVersioningTest method populateStore.

private static void populateStore(RosterStore store) throws IOException {
    store.addEntry(new RosterPacket.Item(JidCreate.entityBareFrom("geoff@example.com"), "geoff hurley"), "");
    RosterPacket.Item item = new RosterPacket.Item(JidCreate.entityBareFrom("joe@example.com"), "joe stevens");
    item.addGroupName("friends");
    item.addGroupName("partners");
    store.addEntry(item, "");
    item = new RosterPacket.Item(JidCreate.entityBareFrom("higgins@example.com"), "higgins mcmann");
    item.addGroupName("all");
    item.addGroupName("friends");
    store.addEntry(item, "v96");
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item)

Example 14 with Item

use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.

the class RosterVersioningTest method testOtherVersionStored.

/**
     * Tests that a non-empty roster result empties the store.
     * @throws SmackException 
     * @throws XMPPException 
     * @throws XmppStringprepException 
     */
@Test(timeout = 5000)
public void testOtherVersionStored() throws XMPPException, SmackException, XmppStringprepException {
    Item vaglafItem = vaglafItem();
    // 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) {
        RosterPacket sentRP = (RosterPacket) sentPacket;
        RosterPacket answer = new RosterPacket();
        answer.setStanzaId(sentRP.getStanzaId());
        answer.setType(Type.result);
        answer.setTo(sentRP.getFrom());
        answer.setVersion("newVersion");
        answer.addRosterItem(vaglafItem);
        rosterListener.reset();
        connection.processStanza(answer);
        rosterListener.waitUntilInvocationOrTimeout();
    } else {
        assertTrue("Expected to get a RosterPacket ", false);
    }
    Roster roster = Roster.getInstanceFor(connection);
    assertEquals("Size of roster", 1, roster.getEntries().size());
    RosterEntry entry = roster.getEntry(vaglafItem.getJid());
    assertNotNull("Roster contains vaglaf entry", entry);
    assertEquals("vaglaf entry in roster equals the sent entry", vaglafItem, RosterEntry.toRosterItem(entry));
    RosterStore store = roster.getRosterStore();
    assertEquals("Size of store", 1, store.getEntries().size());
    Item item = store.getEntry(vaglafItem.getJid());
    assertNotNull("Store contains vaglaf entry");
    assertEquals("vaglaf entry in store equals the sent entry", vaglafItem, item);
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) DirectoryRosterStore(org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore) RosterStore(org.jivesoftware.smack.roster.rosterstore.RosterStore) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 15 with Item

use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.

the class RosterVersioningTest method testEqualVersionStored.

/**
     * Tests that receiving an empty roster result causes the roster to be populated
     * by all entries of the roster store.
     * @throws SmackException 
     * @throws XMPPException 
     */
@Test(timeout = 300000)
public void testEqualVersionStored() throws InterruptedException, IOException, XMPPException, SmackException {
    answerWithEmptyRosterResult();
    roster.waitUntilLoaded();
    Collection<RosterEntry> entries = roster.getEntries();
    assertSame("Size of the roster", 3, entries.size());
    HashSet<Item> items = new HashSet<Item>();
    for (RosterEntry entry : entries) {
        items.add(RosterEntry.toRosterItem(entry));
    }
    RosterStore store = DirectoryRosterStore.init(tmpFolder.newFolder());
    populateStore(store);
    assertEquals("Elements of the roster", new HashSet<Item>(store.getEntries()), items);
    for (RosterEntry entry : entries) {
        assertTrue("joe stevens".equals(entry.getName()) || "geoff hurley".equals(entry.getName()) || "higgins mcmann".equals(entry.getName()));
    }
    Collection<RosterGroup> groups = roster.getGroups();
    assertSame(3, groups.size());
    for (RosterGroup group : groups) {
        assertTrue("all".equals(group.getName()) || "friends".equals(group.getName()) || "partners".equals(group.getName()));
    }
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) DirectoryRosterStore(org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore) RosterStore(org.jivesoftware.smack.roster.rosterstore.RosterStore) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Item (org.jivesoftware.smack.roster.packet.RosterPacket.Item)19 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)16 Test (org.junit.Test)10 BareJid (org.jxmpp.jid.BareJid)8 ArrayList (java.util.ArrayList)3 DirectoryRosterStore (org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore)3 RosterStore (org.jivesoftware.smack.roster.rosterstore.RosterStore)3 File (java.io.File)2 HashSet (java.util.HashSet)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Collection (java.util.Collection)1 IQ (org.jivesoftware.smack.packet.IQ)1 Presence (org.jivesoftware.smack.packet.Presence)1 EntityBareJid (org.jxmpp.jid.EntityBareJid)1