Annotation of embedaddon/miniupnpc/src/testportlistingparse.c, revision 1.1.1.1

1.1       misho       1: /* $Id: testportlistingparse.c,v 1.2 2014/11/01 10:37:32 nanard Exp $ */
                      2: /* Project : miniupnp
                      3:  * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
                      4:  * Author : Thomas Bernard
                      5:  * Copyright (c) 2014 Thomas Bernard
                      6:  * This software is subject to the conditions detailed in the
                      7:  * LICENCE file provided in this distribution.
                      8:  * */
                      9: 
                     10: #include <string.h>
                     11: #include <stdio.h>
                     12: #include "portlistingparse.h"
                     13: 
                     14: struct port_mapping {
                     15:        unsigned int leasetime;
                     16:        unsigned short externalport;
                     17:        unsigned short internalport;
                     18:        const char * remotehost;
                     19:        const char * client;
                     20:        const char * proto;
                     21:        const char * desc;
                     22:        unsigned char enabled;
                     23: };
                     24: 
                     25: /* return the number of differences */
                     26: int test(const char * portListingXml, int portListingXmlLen,
                     27:          const struct port_mapping * ref, int count)
                     28: {
                     29:        int i;
                     30:        int r = 0;
                     31:        struct PortMappingParserData data;
                     32:        struct PortMapping * pm;
                     33: 
                     34:        memset(&data, 0, sizeof(data));
                     35:        ParsePortListing(portListingXml, portListingXmlLen, &data);
                     36:        for(i = 0, pm = data.l_head;
                     37:            (pm != NULL) && (i < count);
                     38:            i++, pm = pm->l_next) {
                     39:                printf("%2d %s %5hu->%s:%-5hu '%s' '%s' %u\n",
                     40:                       i, pm->protocol, pm->externalPort, pm->internalClient,
                     41:                       pm->internalPort,
                     42:                       pm->description, pm->remoteHost,
                     43:                       (unsigned)pm->leaseTime);
                     44:                if(0 != strcmp(pm->protocol, ref[i].proto)) {
                     45:                        printf("protocol : '%s' != '%s'\n", pm->protocol, ref[i].proto);
                     46:                        r++;
                     47:                }
                     48:                if(pm->externalPort != ref[i].externalport) {
                     49:                        printf("externalPort : %hu != %hu\n",
                     50:                               pm->externalPort, ref[i].externalport);
                     51:                        r++;
                     52:                }
                     53:                if(0 != strcmp(pm->internalClient, ref[i].client)) {
                     54:                        printf("client : '%s' != '%s'\n",
                     55:                               pm->internalClient, ref[i].client);
                     56:                        r++;
                     57:                }
                     58:                if(pm->internalPort != ref[i].internalport) {
                     59:                        printf("internalPort : %hu != %hu\n",
                     60:                               pm->internalPort, ref[i].internalport);
                     61:                        r++;
                     62:                }
                     63:                if(0 != strcmp(pm->description, ref[i].desc)) {
                     64:                        printf("description : '%s' != '%s'\n",
                     65:                               pm->description, ref[i].desc);
                     66:                        r++;
                     67:                }
                     68:                if(0 != strcmp(pm->remoteHost, ref[i].remotehost)) {
                     69:                        printf("remoteHost : '%s' != '%s'\n",
                     70:                               pm->remoteHost, ref[i].remotehost);
                     71:                        r++;
                     72:                }
                     73:                if((unsigned)pm->leaseTime != ref[i].leasetime) {
                     74:                        printf("leaseTime : %u != %u\n",
                     75:                               (unsigned)pm->leaseTime, ref[i].leasetime);
                     76:                        r++;
                     77:                }
                     78:                if(pm->enabled != ref[i].enabled) {
                     79:                        printf("enabled : %d != %d\n",
                     80:                               (int)pm->enabled, (int)ref[i].enabled);
                     81:                        r++;
                     82:                }
                     83:        }
                     84:        if((i != count) || (pm != NULL)) {
                     85:                printf("count mismatch : i=%d count=%d pm=%p\n", i, count, pm);
                     86:                r++;
                     87:        }
                     88:        FreePortListing(&data);
                     89:        return r;
                     90: }
                     91: 
                     92: const char test_document[] =
                     93: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                     94: "<p:PortMappingList xmlns:p=\"urn:schemas-upnp-org:gw:WANIPConnection\"\n"
                     95: "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
                     96: "xsi:schemaLocation=\"urn:schemas-upnp-org:gw:WANIPConnection "
                     97: "http://www.upnp.org/schemas/gw/WANIPConnection-v2.xsd\">\n"
                     98: " <p:PortMappingEntry>\n"
                     99: "  <p:NewRemoteHost></p:NewRemoteHost>\n"
                    100: "  <p:NewExternalPort>5002</p:NewExternalPort>\n"
                    101: "  <p:NewProtocol>UDP</p:NewProtocol>\n"
                    102: "  <p:NewInternalPort>4001</p:NewInternalPort>\n"
                    103: "  <p:NewInternalClient>192.168.1.123</p:NewInternalClient>\n"
                    104: "  <p:NewEnabled>1</p:NewEnabled>\n"
                    105: "  <p:NewDescription>xxx</p:NewDescription>\n"
                    106: "  <p:NewLeaseTime>0</p:NewLeaseTime>\n"
                    107: " </p:PortMappingEntry>\n"
                    108: " <p:PortMappingEntry>\n"
                    109: "  <p:NewRemoteHost>202.233.2.1</p:NewRemoteHost>\n"
                    110: "  <p:NewExternalPort>2345</p:NewExternalPort>\n"
                    111: "  <p:NewProtocol>TCP</p:NewProtocol>\n"
                    112: "  <p:NewInternalPort>2349</p:NewInternalPort>\n"
                    113: "  <p:NewInternalClient>192.168.1.137</p:NewInternalClient>\n"
                    114: "  <p:NewEnabled>1</p:NewEnabled>\n"
                    115: "  <p:NewDescription>dooom</p:NewDescription>\n"
                    116: "  <p:NewLeaseTime>346</p:NewLeaseTime>\n"
                    117: " </p:PortMappingEntry>\n"
                    118: " <p:PortMappingEntry>\n"
                    119: "  <p:NewRemoteHost>134.231.2.11</p:NewRemoteHost>\n"
                    120: "  <p:NewExternalPort>12345</p:NewExternalPort>\n"
                    121: "  <p:NewProtocol>TCP</p:NewProtocol>\n"
                    122: "  <p:NewInternalPort>12345</p:NewInternalPort>\n"
                    123: "  <p:NewInternalClient>192.168.1.137</p:NewInternalClient>\n"
                    124: "  <p:NewEnabled>1</p:NewEnabled>\n"
                    125: "  <p:NewDescription>dooom A</p:NewDescription>\n"
                    126: "  <p:NewLeaseTime>347</p:NewLeaseTime>\n"
                    127: " </p:PortMappingEntry>\n"
                    128: "</p:PortMappingList>";
                    129: 
                    130: #define PORT_MAPPINGS_COUNT 3
                    131: const struct port_mapping port_mappings[PORT_MAPPINGS_COUNT] = {
                    132: {347, 12345, 12345, "134.231.2.11", "192.168.1.137", "TCP", "dooom A", 1},
                    133: {346, 2345, 2349, "202.233.2.1", "192.168.1.137", "TCP", "dooom", 1},
                    134: {0, 5002, 4001, "", "192.168.1.123", "UDP", "xxx", 1}
                    135: };
                    136: 
                    137: /* --- main --- */
                    138: int main(void)
                    139: {
                    140:        int r;
                    141:        r = test(test_document, sizeof(test_document) - 1,
                    142:                 port_mappings, PORT_MAPPINGS_COUNT);
                    143:        if(r == 0) {
                    144:                printf("test of portlistingparse OK\n");
                    145:                return 0;
                    146:        } else {
                    147:                printf("test FAILED (%d differences counted)\n", r);
                    148:                return 1;
                    149:        }
                    150: }
                    151: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>