testparser.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * testparser.c: Additional parser tests
  3. *
  4. * See Copyright for the status of this software.
  5. */
  6. #include <libxml/parser.h>
  7. #include <libxml/HTMLparser.h>
  8. static int
  9. testStandaloneWithEncoding(void) {
  10. xmlDocPtr doc;
  11. const char *str =
  12. "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
  13. "<doc></doc>\n";
  14. int err = 0;
  15. xmlResetLastError();
  16. doc = xmlReadDoc(BAD_CAST str, NULL, "UTF-8", 0);
  17. if (doc == NULL) {
  18. fprintf(stderr, "xmlReadDoc failed\n");
  19. err = 1;
  20. }
  21. xmlFreeDoc(doc);
  22. return err;
  23. }
  24. #ifdef LIBXML_PUSH_ENABLED
  25. static int
  26. testHugePush(void) {
  27. xmlParserCtxtPtr ctxt;
  28. int err, i;
  29. ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
  30. /*
  31. * Push parse a document larger than XML_MAX_LOOKUP_LIMIT
  32. * (10,000,000 bytes). This mainly tests whether shrinking the
  33. * buffer works when push parsing.
  34. */
  35. xmlParseChunk(ctxt, "<doc>", 5, 0);
  36. for (i = 0; i < 1000000; i++)
  37. xmlParseChunk(ctxt, "<elem>text</elem>", 17, 0);
  38. xmlParseChunk(ctxt, "</doc>", 6, 1);
  39. err = ctxt->wellFormed ? 0 : 1;
  40. xmlFreeDoc(ctxt->myDoc);
  41. xmlFreeParserCtxt(ctxt);
  42. return err;
  43. }
  44. static int
  45. testHugeEncodedChunk(void) {
  46. xmlBufferPtr buf;
  47. xmlChar *chunk;
  48. xmlParserCtxtPtr ctxt;
  49. int err, i;
  50. /*
  51. * Test the push parser with a built-in encoding handler like ISO-8859-1
  52. * and a chunk larger than the initial decoded buffer (currently 4 KB).
  53. */
  54. buf = xmlBufferCreate();
  55. xmlBufferCat(buf,
  56. BAD_CAST "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
  57. xmlBufferCat(buf, BAD_CAST "<doc><!-- ");
  58. for (i = 0; i < 2000; i++)
  59. xmlBufferCat(buf, BAD_CAST "0123456789");
  60. xmlBufferCat(buf, BAD_CAST " --></doc>");
  61. chunk = xmlBufferDetach(buf);
  62. xmlBufferFree(buf);
  63. ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
  64. xmlParseChunk(ctxt, (char *) chunk, xmlStrlen(chunk), 0);
  65. xmlParseChunk(ctxt, NULL, 0, 1);
  66. err = ctxt->wellFormed ? 0 : 1;
  67. xmlFreeDoc(ctxt->myDoc);
  68. xmlFreeParserCtxt(ctxt);
  69. xmlFree(chunk);
  70. return err;
  71. }
  72. #endif
  73. #if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_PUSH_ENABLED)
  74. static int
  75. testHtmlPushWithEncoding(void) {
  76. htmlParserCtxtPtr ctxt;
  77. htmlDocPtr doc;
  78. htmlNodePtr node;
  79. int err = 0;
  80. ctxt = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL,
  81. XML_CHAR_ENCODING_UTF8);
  82. htmlParseChunk(ctxt, "-\xC3\xA4-", 4, 1);
  83. doc = ctxt->myDoc;
  84. if (!xmlStrEqual(doc->encoding, BAD_CAST "UTF-8")) {
  85. fprintf(stderr, "testHtmlPushWithEncoding failed\n");
  86. err = 1;
  87. }
  88. node = xmlDocGetRootElement(doc)->children->children->children;
  89. if (!xmlStrEqual(node->content, BAD_CAST "-\xC3\xA4-")) {
  90. fprintf(stderr, "testHtmlPushWithEncoding failed\n");
  91. err = 1;
  92. }
  93. xmlFreeDoc(doc);
  94. htmlFreeParserCtxt(ctxt);
  95. return err;
  96. }
  97. #endif
  98. int
  99. main(void) {
  100. int err = 0;
  101. err |= testStandaloneWithEncoding();
  102. #ifdef LIBXML_PUSH_ENABLED
  103. err |= testHugePush();
  104. err |= testHugeEncodedChunk();
  105. #endif
  106. #if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_PUSH_ENABLED)
  107. err |= testHtmlPushWithEncoding();
  108. #endif
  109. return err;
  110. }