1
\$\begingroup\$

I am implementing chat app using XMPP.

Here is my XML Parser function which tells me whether given XML is file transfer offer or not:

XMPPIQ+FileTransfer Extension->

- (BOOL)isFileTransferOffer {
DDLogVerbose(@"");
if (![self isIQHasJingleAction:@"session-initiate"]) {
    return NO;
}
NSXMLElement *jingle = [self elementForName:@"jingle" xmlns:XMPPJingleNamespace];
NSString *sid = [jingle attributeStringValueForName:@"sid"];
if (!sid) {
    DDLogError(@"No sid found");
    return NO;
}
NSXMLElement *content = [jingle elementForName:@"content"];
if (!content) {
    DDLogError(@"No content node found");
    return NO;
}
NSXMLElement *description = [content elementForName:@"description" xmlns:XMPPJingleFTNamespace];
if (!description) {
    DDLogError(@"No description node found");
    return NO;
}
NSXMLElement *offer = [description elementForName:@"offer"];
if (!offer) {
    DDLogError(@"No offer node found");
    return NO;
}
NSXMLElement *file = [offer elementForName:@"file"];
if (!file) {
    DDLogError(@"No file node found");
    return NO;
}
if (![self isValidFileInfoPresent:file]) {
    DDLogError(@"File info not found OR invalid");
    return NO;
}
if (![XMPPIQ isValidTransportPresent:content]) {
    DDLogError(@"No transport found OR is invalid");
    return NO;
}
return YES;
}

I call this function on received XMPPIQ to see if it is a file transfer offer.

Here is offer:

<iq from="abc@host.com/mobile" id="79507898-1E1B-486B-A554-6831EF391F2F" to="xyz@host.com/mobile" type="set">
<jingle action="session-initiate" initiator="abc@host.com/mobile" sid="5F561305-1D71-4E77-A689-2CC15929D353" xmlns="urn:xmpp:jingle:1">
    <content creator="initiator" name="F3835290-BAA3-4B46-B30B-32BA6C9DC402">
        <description xmlns="urn:xmpp:jingle:apps:file-transfer:3">
            <offer>
                <file>
                    <name extension="jpeg"/>
                    <size>463847</size>
                    <file-type>Image</file-type>
                </file>
            </offer>
        </description>
        <transport sid="2B1BE934-4636-414E-BE21-3519CB8A1B03" xmlns="urn:xmpp:jingle:transports:s5b:1">
            <candidate cid="A3A2AD11-6A4B-4608-879A-66B0A0E36D8F" host="192.168.1.7" jid="abc@host.com/mobile" port="23085" priority="720896" type="direct"/>
            <candidate cid="17FF309A-60B4-4F73-A730-6102B697F032" host="host.com" jid="proxy.host.com" port="7777" priority="720895" type="proxy" xmlns="urn:xmpp:jingle:transports:s5b:1"/>
        </transport>
    </content>
</jingle>
</iq>

I have written some tests to check whether my above function is working fine or not. Tests:

func testFileTransferOfferCheck() {
    var testIqStr = "<iq xmlns=\"jabber:client\"/>"
    var testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //no set iq
    testIqStr = "<iq from=\"abc@host.com/mobile\" id=\"CF5D44EF-4F81-4987-BBC1-1A596F51E2B4\" to=\"xyz@host.com/mobile\" type=\"wrongtype\"></iq>"
    testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //no jingle element
    testIqStr = "<iq from=\"abc@host.com/mobile\" id=\"CF5D44EF-4F81-4987-BBC1-1A596F51E2B4\" to=\"xyz@host.com/mobile\" type=\"set\"></iq>"
    testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //no session-initiate action
    testIqStr = "<iq from=\"abc@host.com/mobile\" id=\"CF5D44EF-4F81-4987-BBC1-1A596F51E2B4\" to=\"xyz@host.com/mobile\" type=\"set\"><jingle " + /*action=\"session-initiate\"*/ "initiator=\"abc@host.com/mobile\" sid=\"FB0C1B2A-854A-4688-88B8-B20D2903805D\" xmlns=\"urn:xmpp:jingle:1\"></jingle></iq>"
    testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //no content
    testIqStr = "<iq from=\"abc@host.com/mobile\" id=\"CF5D44EF-4F81-4987-BBC1-1A596F51E2B4\" to=\"xyz@host.com/mobile\" type=\"set\"><jingle action=\"session-initiate\" initiator=\"abc@host.com/mobile\" sid=\"FB0C1B2A-854A-4688-88B8-B20D2903805D\" xmlns=\"urn:xmpp:jingle:1\"></jingle></iq>"
    testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //no description
    testIqStr = "<iq from=\"abc@host.com/mobile\" id=\"CF5D44EF-4F81-4987-BBC1-1A596F51E2B4\" to=\"xyz@host.com/mobile\" type=\"set\"><jingle action=\"session-initiate\" initiator=\"abc@host.com/mobile\" sid=\"FB0C1B2A-854A-4688-88B8-B20D2903805D\" xmlns=\"urn:xmpp:jingle:1\"><content creator=\"initiator\" name=\"B04A20C1-2C8A-4F7A-95C3-3D65FF1EF79A\"></content></jingle></iq>"
    testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //no offer
    testIqStr = "<iq from=\"abc@host.com/mobile\" id=\"CF5D44EF-4F81-4987-BBC1-1A596F51E2B4\" to=\"xyz@host.com/mobile\" type=\"set\"><jingle action=\"session-initiate\" initiator=\"abc@host.com/mobile\" sid=\"FB0C1B2A-854A-4688-88B8-B20D2903805D\" xmlns=\"urn:xmpp:jingle:1\"><content creator=\"initiator\" name=\"B04A20C1-2C8A-4F7A-95C3-3D65FF1EF79A\"><description xmlns=\"urn:xmpp:jingle:apps:file-transfer:3\"></description></content></jingle></iq>"
    testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //no file
    testIqStr = "<iq from=\"abc@host.com/mobile\" id=\"CF5D44EF-4F81-4987-BBC1-1A596F51E2B4\" to=\"xyz@host.com/mobile\" type=\"set\"><jingle action=\"session-initiate\" initiator=\"abc@host.com/mobile\" sid=\"FB0C1B2A-854A-4688-88B8-B20D2903805D\" xmlns=\"urn:xmpp:jingle:1\"><content creator=\"initiator\" name=\"B04A20C1-2C8A-4F7A-95C3-3D65FF1EF79A\"><description xmlns=\"urn:xmpp:jingle:apps:file-transfer:3\"><offer></offer></description></content></jingle></iq>"
    testIq = TestsUtils.getXMPPIQ(fromString: testIqStr)
    XCTAssertFalse(testIq.isFileTransferOffer())

    //and so on ...
}

It is working fine and testing my code accurately.
As we can see in above test code, I have to write so much of code and there are lot of chances to mis-type testIqStr for each test.
Also, I am having many such parser functions and need to write so much bulky tests.

Is there any better way to write tests for such type of XML parser function?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.