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));
}
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();
}
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");
}
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);
}
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()));
}
}
Aggregations