Skip to content

Commit

Permalink
[unittest] Additional test that asserts CAPS dataform ordering
Browse files Browse the repository at this point in the history
XEP-0115 defines that any dataforms in the disco#info stanza is ordered prior to the computation of the verification string. This commit adds a test that verifies that this is done by Smack.
  • Loading branch information
guusdk committed Jun 25, 2024
1 parent 4f09244 commit 720d666
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@

public class EntityCapsManagerTest extends SmackTestSuite {

/**
* <a href="https://xmpp.org/extensions/xep-0115.html#ver-gen-simple">XEP-
* 0115 Simple Generation Example</a>.
* @throws XmppStringprepException if the provided string is invalid.
*/
@Test
public void testSimpleGenerationExample() throws XmppStringprepException {
DiscoverInfo di = createSimpleSamplePacket();

CapsVersionAndHash versionAndHash = EntityCapsManager.generateVerificationString(di, StringUtils.SHA1);
assertEquals("QgayPKawpkPSDYmwT/WM94uAlu0=", versionAndHash.version);
}

/**
* Asserts that the order in which data forms are present in the disco/info does not affect the calculated
* verification string, as the XEP mandates that these are ordered by FORM_TYPE (i.e., by the XML character data of
* the <value/> element).
* @throws XmppStringprepException if the provided string is invalid.
*/
@Test
public void testReversedDataFormOrder() throws XmppStringprepException {
final DiscoverInfoBuilder builderA = createSimpleSampleBuilder();
builderA.addExtension(createSampleServerInfoDataForm()); // This works, as the underlying MultiMap maintains insertion-order.
builderA.addExtension(createSampleSoftwareInfoDataForm());

final DiscoverInfoBuilder builderB = createSimpleSampleBuilder();
builderB.addExtension(createSampleSoftwareInfoDataForm());
builderB.addExtension(createSampleServerInfoDataForm());

CapsVersionAndHash versionAndHashA = EntityCapsManager.generateVerificationString(builderA.build(), StringUtils.SHA1);
CapsVersionAndHash versionAndHashB = EntityCapsManager.generateVerificationString(builderB.build(), StringUtils.SHA1);

assertEquals(versionAndHashA.version, versionAndHashB.version);
}

/**
* <a href="http://xmpp.org/extensions/xep-0115.html#ver-gen-complex">XEP-
* 0115 Complex Generation Example</a>.
Expand Down Expand Up @@ -142,6 +177,41 @@ private static DataForm createSampleSoftwareInfoDataForm() {
return df.build();
}

private static DataForm createSampleServerInfoDataForm() {
DataForm.Builder df = DataForm.builder(DataForm.Type.result);

{
TextMultiFormField.Builder ff = FormField.textMultiBuilder("admin-addresses");
ff.addValue("xmpp:admin@example.org");
ff.addValue("mailto:admin@example.com");
df.addField(ff.build());
}

{
TextSingleFormField.Builder ff = FormField.hiddenBuilder("FORM_TYPE");
ff.setValue("http://jabber.org/network/serverinfo");
df.addField(ff.build());
}

return df.build();
}

private static DiscoverInfoBuilder createSimpleSampleBuilder() throws XmppStringprepException {
DiscoverInfoBuilder di = DiscoverInfo.builder("disco1");
di.ofType(IQ.Type.result);

di.addIdentity(new DiscoverInfo.Identity("client", "pc", "Exodus 0.9.1"));
di.addFeature("http://jabber.org/protocol/disco#info");
di.addFeature("http://jabber.org/protocol/disco#items");
di.addFeature("http://jabber.org/protocol/muc");
di.addFeature("http://jabber.org/protocol/caps");

return di;
}
private static DiscoverInfo createSimpleSamplePacket() throws XmppStringprepException {
return createSimpleSampleBuilder().build();
}

private static DiscoverInfo createComplexSamplePacket() throws XmppStringprepException {
DiscoverInfoBuilder di = DiscoverInfo.builder("disco1");
di.from(JidCreate.from("benvolio@capulet.lit/230193"));
Expand Down

0 comments on commit 720d666

Please sign in to comment.