1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-08 06:01:07 +01:00

Add support for XEP-0232: Software Information

By making use of an extended data format, service discovery responses
can be used to constitute software information.
Solves SMACK-853.
This commit is contained in:
Aditya Borikar 2020-05-14 16:48:07 +05:30
parent bdedf5a0a2
commit 17ca4c541b
9 changed files with 762 additions and 0 deletions

View file

@ -0,0 +1,143 @@
/**
*
* Copyright 2020 Aditya Borikar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.softwareinfo;
import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.net.URI;
import java.net.URISyntaxException;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.mediaelement.element.MediaElement;
import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.TextSingleFormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jivesoftware.smackx.xdata.packet.DataForm.Type;
import org.junit.jupiter.api.Test;
public class SoftwareInfoFormTest extends SmackTestSuite {
private final String xml =
"<x xmlns='jabber:x:data' type='result'>" +
"<field var='FORM_TYPE' type='hidden'>" +
"<value>urn:xmpp:dataforms:softwareinfo</value>" +
"</field>" +
"<field var='icon'>" +
"<media xmlns='urn:xmpp:media-element' height='80' width='290'>" +
"<uri type='image/jpeg'>" +
"http://www.shakespeare.lit/clients/exodus.jpg" +
"</uri>" +
"<uri type='image/jpeg'>" +
"cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org" +
"</uri>" +
"</media>" +
"</field>" +
"<field var='os'>" +
"<value>Windows</value>" +
"</field>" +
"<field var='os_version'>" +
"<value>XP</value>" +
"</field>" +
"<field var='software'>" +
"<value>Exodus</value>" +
"</field>" +
"<field var='software_version'>" +
"<value>0.9.1</value>" +
"</field>" +
"</x>";
@Test
public void softwareInfoBuilderTest() throws URISyntaxException {
SoftwareInfoForm softwareInfoForm = createSoftwareInfoForm();
assertXmlSimilar(xml, softwareInfoForm.getDataForm().toXML());
softwareInfoForm = createSoftwareInfoFormUsingDataForm();
assertXmlSimilar(xml, softwareInfoForm.getDataForm().toXML());
}
@Test
public void getInfoFromSoftwareInfoFormTest() throws URISyntaxException {
SoftwareInfoForm softwareInfoForm = createSoftwareInfoForm();
assertEquals("Windows", softwareInfoForm.getOS());
assertEquals("XP", softwareInfoForm.getOSVersion());
assertEquals("Exodus", softwareInfoForm.getSoftwareName());
assertEquals("0.9.1", softwareInfoForm.getSoftwareVersion());
assertXmlSimilar(createMediaElement().toXML(), softwareInfoForm.getIcon().toXML());
}
@Test
public void faultySoftwareInfoFormsTest() {
DataForm.Builder dataFormbuilder = DataForm.builder(Type.result);
TextSingleFormField formField = FormField.buildHiddenFormType("faulty_formtype");
dataFormbuilder.addField(formField);
assertThrows(IllegalArgumentException.class, () -> {
SoftwareInfoForm.getBuilder().setDataForm(dataFormbuilder.build()).build();
});
DataForm.Builder builderWithoutFormType = DataForm.builder(Type.result);
assertThrows(IllegalArgumentException.class, () -> {
SoftwareInfoForm.getBuilder().setDataForm(builderWithoutFormType.build()).build();
});
}
public static SoftwareInfoForm createSoftwareInfoFormUsingDataForm() throws URISyntaxException {
DataForm.Builder dataFormBuilder = DataForm.builder(Type.result);
TextSingleFormField formField = FormField.buildHiddenFormType(SoftwareInfoForm.FORM_TYPE);
dataFormBuilder.addField(formField);
dataFormBuilder.addField(FormField.builder("icon")
.addFormFieldChildElement(createMediaElement())
.build());
dataFormBuilder.addField(FormField.builder("os")
.setValue("Windows")
.build());
dataFormBuilder.addField(FormField.builder("os_version")
.setValue("XP")
.build());
dataFormBuilder.addField(FormField.builder("software")
.setValue("Exodus")
.build());
dataFormBuilder.addField(FormField.builder("software_version")
.setValue("0.9.1")
.build());
SoftwareInfoForm softwareInfoForm = SoftwareInfoForm.getBuilder().setDataForm(dataFormBuilder.build()).build();
return softwareInfoForm;
}
public static SoftwareInfoForm createSoftwareInfoForm() throws URISyntaxException {
return SoftwareInfoForm.getBuilder()
.setIcon(createMediaElement())
.setOS("Windows")
.setOSVersion("XP")
.setSoftware("Exodus")
.setSoftwareVersion("0.9.1")
.build();
}
public static MediaElement createMediaElement() throws URISyntaxException {
return MediaElement.builder()
.addUri(new URI("http://www.shakespeare.lit/clients/exodus.jpg"), "image/jpeg")
.addUri(new URI("cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org"), "image/jpeg")
.setHeightAndWidth(80, 290)
.build();
}
}

View file

@ -0,0 +1,102 @@
/**
*
* Copyright 2020 Aditya Borikar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.softwareinfo;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.mediaelement.element.MediaElement;
import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jivesoftware.smackx.xdata.packet.DataForm.Type;
import org.jivesoftware.util.ConnectionUtils;
import org.jivesoftware.util.Protocol;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.EntityFullJid;
import org.jxmpp.jid.JidTestUtil;
public class SoftwareInfoManagerTest {
private static final EntityFullJid initiatorJID = JidTestUtil.DUMMY_AT_EXAMPLE_ORG_SLASH_DUMMYRESOURCE;
private XMPPConnection connection;
private Protocol protocol;
@BeforeEach
public void setup() throws XMPPException, SmackException, InterruptedException {
protocol = new Protocol();
connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
}
@Test
public void softwareInfoManagerTest() throws IOException, XmlPullParserException, SmackParsingException, URISyntaxException {
SoftwareInfoManager manager = SoftwareInfoManager.getInstanceFor(connection);
manager.publishSoftwareInformationForm(buildSoftwareInfoFormUsingBuilder());
manager.publishSoftwareInformationForm(buildSoftwareInfoFromDataForm());
}
public static SoftwareInfoForm buildSoftwareInfoFormUsingBuilder() throws URISyntaxException {
SoftwareInfoForm.Builder builder = SoftwareInfoForm.getBuilder();
MediaElement mediaElement = createMediaElement();
builder.setIcon(mediaElement);
builder.setOS("Windows");
builder.setOSVersion("XP");
builder.setSoftware("Exodus");
builder.setSoftwareVersion("0.9.1");
return builder.build();
}
public static SoftwareInfoForm buildSoftwareInfoFromDataForm() throws URISyntaxException {
DataForm.Builder dataFormBuilder = DataForm.builder(Type.result);
dataFormBuilder.addField(FormField.buildHiddenFormType(SoftwareInfoForm.FORM_TYPE));
dataFormBuilder.addField(FormField.builder("icon")
.addFormFieldChildElement(createMediaElement())
.build());
dataFormBuilder.addField(FormField.builder("os")
.setValue("Windows")
.build());
dataFormBuilder.addField(FormField.builder("os_version")
.setValue("XP")
.build());
dataFormBuilder.addField(FormField.builder("software")
.setValue("Exodus")
.build());
dataFormBuilder.addField(FormField.builder("software_version")
.setValue("0.9.1")
.build());
SoftwareInfoForm softwareInfoForm = SoftwareInfoForm.getBuilder()
.setDataForm(dataFormBuilder.build())
.build();
return softwareInfoForm;
}
public static MediaElement createMediaElement() throws URISyntaxException {
return MediaElement.builder()
.addUri(new MediaElement.Uri(new URI("http://example.org"), "test-type"))
.setHeightAndWidth(16, 16)
.build();
}
}