testigddescparse.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* $Id: testigddescparse.c,v 1.10 2015/08/06 09:55:24 nanard Exp $ */
  2. /* Project : miniupnp
  3. * http://miniupnp.free.fr/
  4. * Author : Thomas Bernard
  5. * Copyright (c) 2008-2015 Thomas Bernard
  6. * This software is subject to the conditions detailed in the
  7. * LICENCE file provided in this distribution.
  8. * */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "igd_desc_parse.h"
  13. #include "minixml.h"
  14. #include "miniupnpc.h"
  15. /* count number of differences */
  16. int compare_service(struct IGDdatas_service * s, FILE * f)
  17. {
  18. int n = 0;
  19. char line[1024];
  20. while(fgets(line, sizeof(line), f)) {
  21. char * value;
  22. char * equal;
  23. char * name;
  24. char * parsedvalue;
  25. int l;
  26. l = strlen(line);
  27. while((l > 0) && ((line[l-1] == '\r') || (line[l-1] == '\n') || (line[l-1] == ' ')))
  28. line[--l] = '\0';
  29. if(l == 0)
  30. break; /* end on blank line */
  31. if(line[0] == '#')
  32. continue; /* skip comments */
  33. equal = strchr(line, '=');
  34. if(equal == NULL) {
  35. fprintf(stderr, "Warning, line does not contain '=' : %s\n", line);
  36. continue;
  37. }
  38. *equal = '\0';
  39. name = line;
  40. while(*name == ' ' || *name == '\t')
  41. name++;
  42. l = strlen(name);
  43. while((l > 0) && (name[l-1] == ' ' || name[l-1] == '\t'))
  44. name[--l] = '\0';
  45. value = equal + 1;
  46. while(*value == ' ' || *value == '\t')
  47. value++;
  48. if(strcmp(name, "controlurl") == 0)
  49. parsedvalue = s->controlurl;
  50. else if(strcmp(name, "eventsuburl") == 0)
  51. parsedvalue = s->eventsuburl;
  52. else if(strcmp(name, "scpdurl") == 0)
  53. parsedvalue = s->scpdurl;
  54. else if(strcmp(name, "servicetype") == 0)
  55. parsedvalue = s->servicetype;
  56. else {
  57. fprintf(stderr, "unknown field '%s'\n", name);
  58. continue;
  59. }
  60. if(0 != strcmp(parsedvalue, value)) {
  61. fprintf(stderr, "difference : '%s' != '%s'\n", parsedvalue, value);
  62. n++;
  63. }
  64. }
  65. return n;
  66. }
  67. int compare_igd(struct IGDdatas * p, FILE * f)
  68. {
  69. int n = 0;
  70. char line[1024];
  71. struct IGDdatas_service * s;
  72. while(fgets(line, sizeof(line), f)) {
  73. char * colon;
  74. int l = (int)strlen(line);
  75. while((l > 0) && (line[l-1] == '\r' || (line[l-1] == '\n')))
  76. line[--l] = '\0';
  77. if(l == 0 || line[0] == '#')
  78. continue; /* skip blank lines and comments */
  79. colon = strchr(line, ':');
  80. if(colon == NULL) {
  81. fprintf(stderr, "Warning, no ':' : %s\n", line);
  82. continue;
  83. }
  84. s = NULL;
  85. *colon = '\0';
  86. if(strcmp(line, "CIF") == 0)
  87. s = &p->CIF;
  88. else if(strcmp(line, "first") == 0)
  89. s = &p->first;
  90. else if(strcmp(line, "second") == 0)
  91. s = &p->second;
  92. else if(strcmp(line, "IPv6FC") == 0)
  93. s = &p->IPv6FC;
  94. else {
  95. s = NULL;
  96. fprintf(stderr, "*** unknown service '%s' ***\n", line);
  97. n++;
  98. continue;
  99. }
  100. n += compare_service(s, f);
  101. }
  102. if(n > 0)
  103. fprintf(stderr, "*** %d difference%s ***\n", n, (n > 1) ? "s" : "");
  104. return n;
  105. }
  106. int test_igd_desc_parse(char * buffer, int len, FILE * f)
  107. {
  108. int n;
  109. struct IGDdatas igd;
  110. struct xmlparser parser;
  111. struct UPNPUrls urls;
  112. memset(&igd, 0, sizeof(struct IGDdatas));
  113. memset(&parser, 0, sizeof(struct xmlparser));
  114. parser.xmlstart = buffer;
  115. parser.xmlsize = len;
  116. parser.data = &igd;
  117. parser.starteltfunc = IGDstartelt;
  118. parser.endeltfunc = IGDendelt;
  119. parser.datafunc = IGDdata;
  120. parsexml(&parser);
  121. #ifdef DEBUG
  122. printIGD(&igd);
  123. #endif /* DEBUG */
  124. GetUPNPUrls(&urls, &igd, "http://fake/desc/url/file.xml", 0);
  125. printf("ipcondescURL='%s'\n", urls.ipcondescURL);
  126. printf("controlURL='%s'\n", urls.controlURL);
  127. printf("controlURL_CIF='%s'\n", urls.controlURL_CIF);
  128. n = f ? compare_igd(&igd, f) : 0;
  129. FreeUPNPUrls(&urls);
  130. return n;
  131. }
  132. int main(int argc, char * * argv)
  133. {
  134. FILE * f;
  135. char * buffer;
  136. int len;
  137. int r;
  138. if(argc<2) {
  139. fprintf(stderr, "Usage: %s file.xml [file.values]\n", argv[0]);
  140. return 1;
  141. }
  142. f = fopen(argv[1], "r");
  143. if(!f) {
  144. fprintf(stderr, "Cannot open %s for reading.\n", argv[1]);
  145. return 1;
  146. }
  147. fseek(f, 0, SEEK_END);
  148. len = ftell(f);
  149. fseek(f, 0, SEEK_SET);
  150. buffer = malloc(len);
  151. if(!buffer) {
  152. fprintf(stderr, "Memory allocation error.\n");
  153. fclose(f);
  154. return 1;
  155. }
  156. r = (int)fread(buffer, 1, len, f);
  157. if(r != len) {
  158. fprintf(stderr, "Failed to read file %s. %d out of %d bytes.\n",
  159. argv[1], r, len);
  160. fclose(f);
  161. free(buffer);
  162. return 1;
  163. }
  164. fclose(f);
  165. f = NULL;
  166. if(argc > 2) {
  167. f = fopen(argv[2], "r");
  168. if(!f) {
  169. fprintf(stderr, "Cannot open %s for reading.\n", argv[2]);
  170. free(buffer);
  171. return 1;
  172. }
  173. }
  174. r = test_igd_desc_parse(buffer, len, f);
  175. free(buffer);
  176. if(f)
  177. fclose(f);
  178. return r;
  179. }