testportlistingparse.c 4.7 KB

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