miniupnpc: add testportlistringparse

This commit is contained in:
Thomas Bernard 2014-11-01 11:18:26 +01:00
parent 27d4d10a3e
commit 63964448b6
3 changed files with 165 additions and 2 deletions

View File

@ -23,3 +23,5 @@ jnaerator-*.jar
miniupnpc.h.bak miniupnpc.h.bak
testupnpreplyparse testupnpreplyparse
validateupnpreplyparse validateupnpreplyparse
testportlistingparse
validateportlistingparse

View File

@ -1,4 +1,4 @@
# $Id: Makefile,v 1.110 2014/09/06 08:24:12 nanard Exp $ # $Id: Makefile,v 1.112 2014/11/01 10:14:42 nanard Exp $
# MiniUPnP Project # MiniUPnP Project
# http://miniupnp.free.fr/ # http://miniupnp.free.fr/
# http://miniupnp.tuxfamily.org/ # http://miniupnp.tuxfamily.org/
@ -106,6 +106,8 @@ TESTMINIWGETOBJS = miniwget.o testminiwget.o connecthostport.o receivedata.o
TESTUPNPREPLYPARSE = testupnpreplyparse.o minixml.o upnpreplyparse.o TESTUPNPREPLYPARSE = testupnpreplyparse.o minixml.o upnpreplyparse.o
TESTPORTLISTINGPARSE = testportlistingparse.o minixml.o portlistingparse.o
TESTIGDDESCPARSE = testigddescparse.o igd_desc_parse.o minixml.o \ TESTIGDDESCPARSE = testigddescparse.o igd_desc_parse.o minixml.o \
miniupnpc.o miniwget.o upnpcommands.o upnpreplyparse.o \ miniupnpc.o miniwget.o upnpcommands.o upnpreplyparse.o \
minisoap.o connecthostport.o receivedata.o \ minisoap.o connecthostport.o receivedata.o \
@ -139,7 +141,8 @@ all: $(LIBRARY) $(EXECUTABLES)
test: check test: check
check: validateminixml validateminiwget validateupnpreplyparse check: validateminixml validateminiwget validateupnpreplyparse \
validateportlistingparse
everything: all $(EXECUTABLES_ADDTESTS) everything: all $(EXECUTABLES_ADDTESTS)
@ -172,6 +175,11 @@ validateupnpreplyparse: testupnpreplyparse testupnpreplyparse.sh
./testupnpreplyparse.sh ./testupnpreplyparse.sh
touch $@ touch $@
validateportlistingparse: testportlistingparse
@echo "portlistingparse validation test"
./testportlistingparse
touch $@
clean: clean:
$(RM) $(LIBRARY) $(SHAREDLIBRARY) $(EXECUTABLES) $(OBJS) miniupnpcstrings.h $(RM) $(LIBRARY) $(SHAREDLIBRARY) $(EXECUTABLES) $(OBJS) miniupnpcstrings.h
$(RM) $(EXECUTABLES_ADDTESTS) $(RM) $(EXECUTABLES_ADDTESTS)
@ -251,6 +259,8 @@ testupnpreplyparse: $(TESTUPNPREPLYPARSE)
testigddescparse: $(TESTIGDDESCPARSE) testigddescparse: $(TESTIGDDESCPARSE)
testportlistingparse: $(TESTPORTLISTINGPARSE)
miniupnpcstrings.h: miniupnpcstrings.h.in updateminiupnpcstrings.sh VERSION miniupnpcstrings.h: miniupnpcstrings.h.in updateminiupnpcstrings.sh VERSION
$(SH) updateminiupnpcstrings.sh $(SH) updateminiupnpcstrings.sh

View File

@ -0,0 +1,151 @@
/* $Id: testportlistingparse.c,v 1.1 2014/11/01 10:14:42 nanard Exp $ */
/* Project : miniupnp
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* Author : Thomas Bernard
* Copyright (c) 2014 Thomas Bernard
* This software is subject to the conditions detailed in the
* LICENCE file provided in this distribution.
* */
#include <string.h>
#include <stdio.h>
#include "portlistingparse.h"
struct port_mapping {
unsigned int leasetime;
unsigned short externalport;
unsigned short internalport;
const char * remotehost;
const char * client;
const char * proto;
const char * desc;
unsigned char enabled;
};
/* return the number of differences */
int test(const char * portListingXml, int portListingXmlLen,
const struct port_mapping * ref, int count)
{
int i;
int r = 0;
struct PortMappingParserData data;
struct PortMapping * pm;
memset(&data, 0, sizeof(data));
ParsePortListing(portListingXml, portListingXmlLen, &data);
for(i = 0, pm = data.head.lh_first;
(pm != NULL) && (i < count);
i++, pm = pm->entries.le_next) {
printf("%2d %s %5hu->%s:%-5hu '%s' '%s' %u\n",
i, pm->protocol, pm->externalPort, pm->internalClient,
pm->internalPort,
pm->description, pm->remoteHost,
(unsigned)pm->leaseTime);
if(0 != strcmp(pm->protocol, ref[i].proto)) {
printf("protocol : '%s' != '%s'\n", pm->protocol, ref[i].proto);
r++;
}
if(pm->externalPort != ref[i].externalport) {
printf("externalPort : %hu != %hu\n",
pm->externalPort, ref[i].externalport);
r++;
}
if(0 != strcmp(pm->internalClient, ref[i].client)) {
printf("client : '%s' != '%s'\n",
pm->internalClient, ref[i].client);
r++;
}
if(pm->internalPort != ref[i].internalport) {
printf("internalPort : %hu != %hu\n",
pm->internalPort, ref[i].internalport);
r++;
}
if(0 != strcmp(pm->description, ref[i].desc)) {
printf("description : '%s' != '%s'\n",
pm->description, ref[i].desc);
r++;
}
if(0 != strcmp(pm->remoteHost, ref[i].remotehost)) {
printf("remoteHost : '%s' != '%s'\n",
pm->remoteHost, ref[i].remotehost);
r++;
}
if((unsigned)pm->leaseTime != ref[i].leasetime) {
printf("leaseTime : %u != %u\n",
(unsigned)pm->leaseTime, ref[i].leasetime);
r++;
}
if(pm->enabled != ref[i].enabled) {
printf("enabled : %d != %d\n",
(int)pm->enabled, (int)ref[i].enabled);
r++;
}
}
if((i != count) || (pm != NULL)) {
printf("count mismatch : i=%d count=%d pm=%p\n", i, count, pm);
r++;
}
FreePortListing(&data);
return r;
}
const char test_document[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<p:PortMappingList xmlns:p=\"urn:schemas-upnp-org:gw:WANIPConnection\"\n"
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
"xsi:schemaLocation=\"urn:schemas-upnp-org:gw:WANIPConnection "
"http://www.upnp.org/schemas/gw/WANIPConnection-v2.xsd\">\n"
" <p:PortMappingEntry>\n"
" <p:NewRemoteHost></p:NewRemoteHost>\n"
" <p:NewExternalPort>5002</p:NewExternalPort>\n"
" <p:NewProtocol>UDP</p:NewProtocol>\n"
" <p:NewInternalPort>4001</p:NewInternalPort>\n"
" <p:NewInternalClient>192.168.1.123</p:NewInternalClient>\n"
" <p:NewEnabled>1</p:NewEnabled>\n"
" <p:NewDescription>xxx</p:NewDescription>\n"
" <p:NewLeaseTime>0</p:NewLeaseTime>\n"
" </p:PortMappingEntry>\n"
" <p:PortMappingEntry>\n"
" <p:NewRemoteHost>202.233.2.1</p:NewRemoteHost>\n"
" <p:NewExternalPort>2345</p:NewExternalPort>\n"
" <p:NewProtocol>TCP</p:NewProtocol>\n"
" <p:NewInternalPort>2349</p:NewInternalPort>\n"
" <p:NewInternalClient>192.168.1.137</p:NewInternalClient>\n"
" <p:NewEnabled>1</p:NewEnabled>\n"
" <p:NewDescription>dooom</p:NewDescription>\n"
" <p:NewLeaseTime>346</p:NewLeaseTime>\n"
" </p:PortMappingEntry>\n"
" <p:PortMappingEntry>\n"
" <p:NewRemoteHost>134.231.2.11</p:NewRemoteHost>\n"
" <p:NewExternalPort>12345</p:NewExternalPort>\n"
" <p:NewProtocol>TCP</p:NewProtocol>\n"
" <p:NewInternalPort>12345</p:NewInternalPort>\n"
" <p:NewInternalClient>192.168.1.137</p:NewInternalClient>\n"
" <p:NewEnabled>1</p:NewEnabled>\n"
" <p:NewDescription>dooom A</p:NewDescription>\n"
" <p:NewLeaseTime>347</p:NewLeaseTime>\n"
" </p:PortMappingEntry>\n"
"</p:PortMappingList>";
#define PORT_MAPPINGS_COUNT 3
const struct port_mapping port_mappings[PORT_MAPPINGS_COUNT] = {
{347, 12345, 12345, "134.231.2.11", "192.168.1.137", "TCP", "dooom A", 1},
{346, 2345, 2349, "202.233.2.1", "192.168.1.137", "TCP", "dooom", 1},
{0, 5002, 4001, "", "192.168.1.123", "UDP", "xxx", 1}
};
/* --- main --- */
int main(void)
{
int r;
r = test(test_document, sizeof(test_document) - 1,
port_mappings, PORT_MAPPINGS_COUNT);
if(r == 0) {
printf("test of portlistingparse OK\n");
return 0;
} else {
printf("test FAILED (%d differences counted)\n", r);
return 1;
}
}