2
0

testxml.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr.h"
  17. #include "apr_general.h"
  18. #include "apr_xml.h"
  19. #if APR_HAVE_STDLIB_H
  20. #include <stdlib.h> /* for exit() */
  21. #endif
  22. static const char *progname;
  23. static const char *usage = "%s [xmlfile]\nIt will create "
  24. "a dummy XML file if none is supplied";
  25. /*
  26. * If our platform knows about the tmpnam() external buffer size, create
  27. * a buffer to pass in. This is needed in a threaded environment, or
  28. * one that thinks it is (like HP-UX).
  29. */
  30. #ifdef L_tmpnam
  31. static char tname_buf[L_tmpnam];
  32. #else
  33. static char *tname_buf = NULL;
  34. #endif
  35. static apr_status_t create_dummy_file_error(apr_pool_t *p, apr_file_t **fd)
  36. {
  37. apr_status_t rv;
  38. char *tmpfile;
  39. int i;
  40. apr_off_t off = 0L;
  41. tmpfile = tmpnam(tname_buf);
  42. if ((tmpfile == NULL) || (*tmpfile == '\0')) {
  43. fprintf(stderr, "unable to generate temporary filename\n");
  44. if (errno == 0) {
  45. errno = ENOENT;
  46. }
  47. perror("tmpnam");
  48. return APR_ENOENT;
  49. }
  50. rv = apr_file_open(fd, tmpfile, APR_CREATE|APR_TRUNCATE|APR_DELONCLOSE|
  51. APR_READ|APR_WRITE|APR_EXCL, APR_OS_DEFAULT, p);
  52. if (rv != APR_SUCCESS)
  53. return rv;
  54. rv = apr_file_puts("<?xml version=\"1.0\" ?>\n<maryx>"
  55. "<had a=\"little\"/><lamb its='fleece "
  56. "was white as snow' />\n", *fd);
  57. if (rv != APR_SUCCESS)
  58. return rv;
  59. for (i = 0; i < 5000; i++) {
  60. rv = apr_file_puts("<hmm roast=\"lamb\" "
  61. "for=\"dinner\">yummy</hmm>\n", *fd);
  62. if (rv != APR_SUCCESS)
  63. return rv;
  64. }
  65. rv = apr_file_puts("</mary>\n", *fd);
  66. if (rv != APR_SUCCESS)
  67. return rv;
  68. return apr_file_seek(*fd, APR_SET, &off);
  69. }
  70. static apr_status_t create_dummy_file(apr_pool_t *p, apr_file_t **fd)
  71. {
  72. apr_status_t rv;
  73. char *tmpfile;
  74. int i;
  75. apr_off_t off = 0L;
  76. tmpfile = tmpnam(tname_buf);
  77. if ((tmpfile == NULL) || (*tmpfile == '\0')) {
  78. fprintf(stderr, "unable to generate temporary filename\n");
  79. if (errno == 0) {
  80. errno = ENOENT;
  81. }
  82. perror("tmpnam");
  83. return APR_ENOENT;
  84. }
  85. rv = apr_file_open(fd, tmpfile, APR_CREATE|APR_TRUNCATE|APR_DELONCLOSE|
  86. APR_READ|APR_WRITE|APR_EXCL, APR_OS_DEFAULT, p);
  87. if (rv != APR_SUCCESS)
  88. return rv;
  89. rv = apr_file_puts("<?xml version=\"1.0\" ?>\n<mary>"
  90. "<had a=\"little\"/><lamb its='fleece "
  91. "was white as snow' />\n", *fd);
  92. if (rv != APR_SUCCESS)
  93. return rv;
  94. for (i = 0; i < 5000; i++) {
  95. rv = apr_file_puts("<hmm roast=\"lamb\" "
  96. "for=\"dinner\">yummy</hmm>\n", *fd);
  97. if (rv != APR_SUCCESS)
  98. return rv;
  99. }
  100. rv = apr_file_puts("</mary>\n", *fd);
  101. if (rv != APR_SUCCESS)
  102. return rv;
  103. rv = apr_file_seek(*fd, APR_SET, &off);
  104. return rv;
  105. }
  106. static void dump_xml(apr_xml_elem *e, int level)
  107. {
  108. apr_xml_attr *a;
  109. apr_xml_elem *ec;
  110. printf("%d: element %s\n", level, e->name);
  111. if (e->attr) {
  112. a = e->attr;
  113. printf("%d:\tattrs\t", level);
  114. while (a) {
  115. printf("%s=%s\t", a->name, a->value);
  116. a = a->next;
  117. }
  118. printf("\n");
  119. }
  120. if (e->first_child) {
  121. ec = e->first_child;
  122. while (ec) {
  123. dump_xml(ec, level + 1);
  124. ec = ec->next;
  125. }
  126. }
  127. }
  128. static void oops(const char *s1, const char *s2, apr_status_t rv)
  129. {
  130. if (progname)
  131. fprintf(stderr, "%s: ", progname);
  132. fprintf(stderr, s1, s2);
  133. if (rv != APR_SUCCESS) {
  134. char buf[120];
  135. fprintf(stderr, " (%s)", apr_strerror(rv, buf, sizeof buf));
  136. }
  137. fprintf(stderr, "\n");
  138. exit(1);
  139. }
  140. int main(int argc, const char *const * argv)
  141. {
  142. apr_pool_t *pool;
  143. apr_file_t *fd;
  144. apr_xml_parser *parser;
  145. apr_xml_doc *doc;
  146. apr_status_t rv;
  147. char errbuf[2000];
  148. char errbufXML[2000];
  149. (void) apr_initialize();
  150. apr_pool_create(&pool, NULL);
  151. progname = argv[0];
  152. if (argc == 1) {
  153. rv = create_dummy_file(pool, &fd);
  154. if (rv != APR_SUCCESS) {
  155. oops("cannot create dummy file", "oops", rv);
  156. }
  157. }
  158. else {
  159. if (argc == 2) {
  160. rv = apr_file_open(&fd, argv[1], APR_READ, APR_OS_DEFAULT, pool);
  161. if (rv != APR_SUCCESS) {
  162. oops("cannot open: %s", argv[1], rv);
  163. }
  164. }
  165. else {
  166. oops("usage: %s", usage, 0);
  167. }
  168. }
  169. rv = apr_xml_parse_file(pool, &parser, &doc, fd, 2000);
  170. if (rv != APR_SUCCESS) {
  171. fprintf(stderr, "APR Error %s\nXML Error: %s\n",
  172. apr_strerror(rv, errbuf, sizeof(errbuf)),
  173. apr_xml_parser_geterror(parser, errbufXML, sizeof(errbufXML)));
  174. return rv;
  175. }
  176. dump_xml(doc->root, 0);
  177. apr_file_close(fd);
  178. if (argc == 1) {
  179. rv = create_dummy_file_error(pool, &fd);
  180. if (rv != APR_SUCCESS) {
  181. oops("cannot create error dummy file", "oops", rv);
  182. }
  183. rv = apr_xml_parse_file(pool, &parser, &doc, fd, 2000);
  184. if (rv != APR_SUCCESS) {
  185. fprintf(stdout, "APR Error %s\nXML Error: %s "
  186. "(EXPECTED) This is good.\n",
  187. apr_strerror(rv, errbuf, sizeof(errbuf)),
  188. apr_xml_parser_geterror(parser, errbufXML, sizeof(errbufXML)));
  189. rv = APR_SUCCESS; /* reset the return code, as the test is supposed to get this error */
  190. }
  191. else {
  192. fprintf(stderr, "Expected an error, but didn't get one ;( ");
  193. return APR_EGENERAL;
  194. }
  195. }
  196. apr_pool_destroy(pool);
  197. apr_terminate();
  198. return rv;
  199. }