Search in sources :

Example 1 with OmemoHeaderElement_VAxolotl

use of org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl in project Smack by igniterealtime.

the class OmemoVAxolotlElementTest method serializationTest.

@Test
public void serializationTest() throws Exception {
    byte[] payload = "This is payload.".getBytes(StandardCharsets.UTF_8);
    int keyId1 = 8;
    int keyId2 = 33333;
    byte[] keyData1 = "KEYDATA".getBytes(StandardCharsets.UTF_8);
    byte[] keyData2 = "DATAKEY".getBytes(StandardCharsets.UTF_8);
    int sid = 12131415;
    byte[] iv = OmemoMessageBuilder.generateIv();
    ArrayList<OmemoKeyElement> keys = new ArrayList<>();
    keys.add(new OmemoKeyElement(keyData1, keyId1));
    keys.add(new OmemoKeyElement(keyData2, keyId2, true));
    OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(sid, keys, iv);
    OmemoElement_VAxolotl element = new OmemoElement_VAxolotl(header, payload);
    String expected = "<encrypted xmlns='eu.siacs.conversations.axolotl'>" + "<header sid='12131415'>" + "<key rid='8'>" + Base64.encodeToString(keyData1) + "</key>" + "<key prekey='true' rid='33333'>" + Base64.encodeToString(keyData2) + "</key>" + "<iv>" + Base64.encodeToString(iv) + "</iv>" + "</header>" + "<payload>" + Base64.encodeToString(payload) + "</payload>" + "</encrypted>";
    String actual = element.toXML().toString();
    assertEquals("Serialized xml of OmemoElement must match.", expected, actual);
    OmemoElement_VAxolotl parsed = new OmemoVAxolotlProvider().parse(TestUtils.getParser(actual));
    assertEquals("Parsed OmemoElement must equal the original.", element.toXML().toString(), parsed.toXML().toString());
}
Also used : OmemoElement_VAxolotl(org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl) OmemoVAxolotlProvider(org.jivesoftware.smackx.omemo.provider.OmemoVAxolotlProvider) OmemoKeyElement(org.jivesoftware.smackx.omemo.element.OmemoKeyElement) OmemoHeaderElement_VAxolotl(org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with OmemoHeaderElement_VAxolotl

use of org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl in project Smack by igniterealtime.

the class OmemoVAxolotlProvider method parse.

@Override
public OmemoElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    int sid = -1;
    ArrayList<OmemoKeyElement> keys = new ArrayList<>();
    byte[] iv = null;
    byte[] payload = null;
    outerloop: while (true) {
        XmlPullParser.Event tag = parser.next();
        switch(tag) {
            case START_ELEMENT:
                String name = parser.getName();
                switch(name) {
                    case OmemoHeaderElement.ELEMENT:
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            if (parser.getAttributeName(i).equals(OmemoHeaderElement.ATTR_SID)) {
                                sid = Integer.parseInt(parser.getAttributeValue(i));
                            }
                        }
                        break;
                    case OmemoKeyElement.ELEMENT:
                        boolean prekey = false;
                        int rid = -1;
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            if (parser.getAttributeName(i).equals(OmemoKeyElement.ATTR_PREKEY)) {
                                prekey = Boolean.parseBoolean(parser.getAttributeValue(i));
                            } else if (parser.getAttributeName(i).equals(OmemoKeyElement.ATTR_RID)) {
                                rid = Integer.parseInt(parser.getAttributeValue(i));
                            }
                        }
                        keys.add(new OmemoKeyElement(Base64.decode(parser.nextText()), rid, prekey));
                        break;
                    case OmemoHeaderElement.ATTR_IV:
                        iv = Base64.decode(parser.nextText());
                        break;
                    case ATTR_PAYLOAD:
                        payload = Base64.decode(parser.nextText());
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    OmemoHeaderElement_VAxolotl header = new OmemoHeaderElement_VAxolotl(sid, keys, iv);
    return new OmemoElement_VAxolotl(header, payload);
}
Also used : OmemoElement_VAxolotl(org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl) OmemoKeyElement(org.jivesoftware.smackx.omemo.element.OmemoKeyElement) OmemoHeaderElement_VAxolotl(org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 OmemoElement_VAxolotl (org.jivesoftware.smackx.omemo.element.OmemoElement_VAxolotl)2 OmemoHeaderElement_VAxolotl (org.jivesoftware.smackx.omemo.element.OmemoHeaderElement_VAxolotl)2 OmemoKeyElement (org.jivesoftware.smackx.omemo.element.OmemoKeyElement)2 OmemoVAxolotlProvider (org.jivesoftware.smackx.omemo.provider.OmemoVAxolotlProvider)1 Test (org.junit.Test)1