aboutsummaryrefslogtreecommitdiff
path: root/external/miniupnpc/testigddescparse.c
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2015-12-30 09:56:25 +0200
committerRiccardo Spagni <ric@spagni.net>2015-12-30 09:56:25 +0200
commit7da99055891baaa453d7d575985bc9824cf5f47c (patch)
tree95471106647425de916ca4595c145ef0464ea624 /external/miniupnpc/testigddescparse.c
parentMerge pull request #575 (diff)
downloadmonero-7da99055891baaa453d7d575985bc9824cf5f47c.tar.xz
updated miniupnpc
Diffstat (limited to 'external/miniupnpc/testigddescparse.c')
-rw-r--r--external/miniupnpc/testigddescparse.c139
1 files changed, 131 insertions, 8 deletions
diff --git a/external/miniupnpc/testigddescparse.c b/external/miniupnpc/testigddescparse.c
index 1c0028a02..cb7e94ad6 100644
--- a/external/miniupnpc/testigddescparse.c
+++ b/external/miniupnpc/testigddescparse.c
@@ -1,8 +1,8 @@
-/* $Id: testigddescparse.c,v 1.4 2012/06/28 18:52:12 nanard Exp $ */
+/* $Id: testigddescparse.c,v 1.8 2015/02/08 08:46:06 nanard Exp $ */
/* Project : miniupnp
* http://miniupnp.free.fr/
* Author : Thomas Bernard
- * Copyright (c) 2008-2012 Thomas Bernard
+ * Copyright (c) 2008-2015 Thomas Bernard
* This software is subject to the conditions detailed in the
* LICENCE file provided in this distribution.
* */
@@ -13,11 +13,108 @@
#include "minixml.h"
#include "miniupnpc.h"
-int test_igd_desc_parse(char * buffer, int len)
+/* count number of differences */
+int compare_service(struct IGDdatas_service * s, FILE * f)
{
+ int n = 0;
+ char line[1024];
+
+ while(fgets(line, sizeof(line), f)) {
+ char * value;
+ char * equal;
+ char * name;
+ char * parsedvalue;
+ int l;
+ l = strlen(line);
+ while((l > 0) && ((line[l-1] == '\r') || (line[l-1] == '\n') || (line[l-1] == ' ')))
+ line[--l] = '\0';
+ if(l == 0)
+ break; /* end on blank line */
+ if(line[0] == '#')
+ continue; /* skip comments */
+ equal = strchr(line, '=');
+ if(equal == NULL) {
+ fprintf(stderr, "Warning, line does not contain '=' : %s\n", line);
+ continue;
+ }
+ *equal = '\0';
+ name = line;
+ while(*name == ' ' || *name == '\t')
+ name++;
+ l = strlen(name);
+ while((l > 0) && (name[l-1] == ' ' || name[l-1] == '\t'))
+ name[--l] = '\0';
+ value = equal + 1;
+ while(*value == ' ' || *value == '\t')
+ value++;
+ if(strcmp(name, "controlurl") == 0)
+ parsedvalue = s->controlurl;
+ else if(strcmp(name, "eventsuburl") == 0)
+ parsedvalue = s->eventsuburl;
+ else if(strcmp(name, "scpdurl") == 0)
+ parsedvalue = s->scpdurl;
+ else if(strcmp(name, "servicetype") == 0)
+ parsedvalue = s->servicetype;
+ else {
+ fprintf(stderr, "unknown field '%s'\n", name);
+ continue;
+ }
+ if(0 != strcmp(parsedvalue, value)) {
+ fprintf(stderr, "difference : '%s' != '%s'\n", parsedvalue, value);
+ n++;
+ }
+ }
+ return n;
+}
+
+int compare_igd(struct IGDdatas * p, FILE * f)
+{
+ int n = 0;
+ char line[1024];
+ struct IGDdatas_service * s;
+
+ while(fgets(line, sizeof(line), f)) {
+ char * colon;
+ int l = (int)strlen(line);
+ while((l > 0) && (line[l-1] == '\r' || (line[l-1] == '\n')))
+ line[--l] = '\0';
+ if(l == 0 || line[0] == '#')
+ continue; /* skip blank lines and comments */
+ colon = strchr(line, ':');
+ if(colon == NULL) {
+ fprintf(stderr, "Warning, no ':' : %s\n", line);
+ continue;
+ }
+ s = NULL;
+ *colon = '\0';
+ if(strcmp(line, "CIF") == 0)
+ s = &p->CIF;
+ else if(strcmp(line, "first") == 0)
+ s = &p->first;
+ else if(strcmp(line, "second") == 0)
+ s = &p->second;
+ else if(strcmp(line, "IPv6FC") == 0)
+ s = &p->IPv6FC;
+ else {
+ s = NULL;
+ fprintf(stderr, "*** unknown service '%s' ***\n", line);
+ n++;
+ continue;
+ }
+ n += compare_service(s, f);
+ }
+ if(n > 0)
+ fprintf(stderr, "*** %d difference%s ***\n", n, (n > 1) ? "s" : "");
+ return n;
+}
+
+int test_igd_desc_parse(char * buffer, int len, FILE * f)
+{
+ int n;
struct IGDdatas igd;
struct xmlparser parser;
struct UPNPUrls urls;
+
memset(&igd, 0, sizeof(struct IGDdatas));
memset(&parser, 0, sizeof(struct xmlparser));
parser.xmlstart = buffer;
@@ -27,13 +124,16 @@ int test_igd_desc_parse(char * buffer, int len)
parser.endeltfunc = IGDendelt;
parser.datafunc = IGDdata;
parsexml(&parser);
+#ifdef DEBUG
printIGD(&igd);
+#endif /* DEBUG */
GetUPNPUrls(&urls, &igd, "http://fake/desc/url/file.xml", 0);
printf("ipcondescURL='%s'\n", urls.ipcondescURL);
printf("controlURL='%s'\n", urls.controlURL);
printf("controlURL_CIF='%s'\n", urls.controlURL_CIF);
+ n = f ? compare_igd(&igd, f) : 0;
FreeUPNPUrls(&urls);
- return 0;
+ return n;
}
int main(int argc, char * * argv)
@@ -41,9 +141,9 @@ int main(int argc, char * * argv)
FILE * f;
char * buffer;
int len;
- int r = 0;
+ int r;
if(argc<2) {
- fprintf(stderr, "Usage: %s file.xml\n", argv[0]);
+ fprintf(stderr, "Usage: %s file.xml [file.values]\n", argv[0]);
return 1;
}
f = fopen(argv[1], "r");
@@ -55,10 +155,33 @@ int main(int argc, char * * argv)
len = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(len);
- fread(buffer, 1, len, f);
+ if(!buffer) {
+ fprintf(stderr, "Memory allocation error.\n");
+ fclose(f);
+ return 1;
+ }
+ r = (int)fread(buffer, 1, len, f);
+ if(r != len) {
+ fprintf(stderr, "Failed to read file %s. %d out of %d bytes.\n",
+ argv[1], r, len);
+ fclose(f);
+ free(buffer);
+ return 1;
+ }
fclose(f);
- r = test_igd_desc_parse(buffer, len);
+ f = NULL;
+ if(argc > 2) {
+ f = fopen(argv[2], "r");
+ if(!f) {
+ fprintf(stderr, "Cannot open %s for reading.\n", argv[2]);
+ free(buffer);
+ return 1;
+ }
+ }
+ r = test_igd_desc_parse(buffer, len, f);
free(buffer);
+ if(f)
+ fclose(f);
return r;
}