xmltest.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. #include "tinyxml2.h"
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <ctime>
  5. #if defined( _MSC_VER )
  6. #include <crtdbg.h>
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <windows.h>
  9. _CrtMemState startMemState;
  10. _CrtMemState endMemState;
  11. #endif
  12. using namespace tinyxml2;
  13. int gPass = 0;
  14. int gFail = 0;
  15. bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
  16. {
  17. bool pass = !strcmp( expected, found );
  18. if ( pass )
  19. printf ("[pass]");
  20. else
  21. printf ("[fail]");
  22. if ( !echo )
  23. printf (" %s\n", testString);
  24. else
  25. printf (" %s [%s][%s]\n", testString, expected, found);
  26. if ( pass )
  27. ++gPass;
  28. else
  29. ++gFail;
  30. return pass;
  31. }
  32. bool XMLTest( const char* testString, int expected, int found, bool echo=true )
  33. {
  34. bool pass = ( expected == found );
  35. if ( pass )
  36. printf ("[pass]");
  37. else
  38. printf ("[fail]");
  39. if ( !echo )
  40. printf (" %s\n", testString);
  41. else
  42. printf (" %s [%d][%d]\n", testString, expected, found);
  43. if ( pass )
  44. ++gPass;
  45. else
  46. ++gFail;
  47. return pass;
  48. }
  49. void NullLineEndings( char* p )
  50. {
  51. while( p && *p ) {
  52. if ( *p == '\n' || *p == '\r' ) {
  53. *p = 0;
  54. return;
  55. }
  56. ++p;
  57. }
  58. }
  59. // Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
  60. int example_1()
  61. {
  62. XMLDocument doc;
  63. doc.LoadFile( "dream.xml" );
  64. return doc.ErrorID();
  65. }
  66. // Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
  67. int example_2()
  68. {
  69. static const char* xml = "<element/>";
  70. XMLDocument doc;
  71. doc.Parse( xml );
  72. return doc.ErrorID();
  73. }
  74. int example_3()
  75. {
  76. static const char* xml =
  77. "<?xml version=\"1.0\"?>"
  78. "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
  79. "<PLAY>"
  80. "<TITLE>A Midsummer Night's Dream</TITLE>"
  81. "</PLAY>";
  82. XMLDocument doc;
  83. doc.Parse( xml );
  84. XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
  85. const char* title = titleElement->GetText();
  86. printf( "Name of play (1): %s\n", title );
  87. XMLText* textNode = titleElement->FirstChild()->ToText();
  88. title = textNode->Value();
  89. printf( "Name of play (2): %s\n", title );
  90. return doc.ErrorID();
  91. }
  92. int main( int /*argc*/, const char ** /*argv*/ )
  93. {
  94. #if defined( _MSC_VER ) && defined( DEBUG )
  95. _CrtMemCheckpoint( &startMemState );
  96. #endif
  97. #if defined(_MSC_VER)
  98. #pragma warning ( push )
  99. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  100. #endif
  101. FILE* fp = fopen( "dream.xml", "r" );
  102. if ( !fp ) {
  103. printf( "Error opening test file 'dream.xml'.\n"
  104. "Is your working directory the same as where \n"
  105. "the xmltest.cpp and dream.xml file are?\n\n"
  106. #if defined( _MSC_VER )
  107. "In windows Visual Studio you may need to set\n"
  108. "Properties->Debugging->Working Directory to '..'\n"
  109. #endif
  110. );
  111. exit( 1 );
  112. }
  113. fclose( fp );
  114. #if defined(_MSC_VER)
  115. #pragma warning ( pop )
  116. #endif
  117. XMLTest( "Example-1", 0, example_1() );
  118. XMLTest( "Example-2", 0, example_2() );
  119. XMLTest( "Example-3", 0, example_3() );
  120. /* ------ Example 2: Lookup information. ---- */
  121. {
  122. static const char* test[] = { "<element />",
  123. "<element></element>",
  124. "<element><subelement/></element>",
  125. "<element><subelement></subelement></element>",
  126. "<element><subelement><subsub/></subelement></element>",
  127. "<!--comment beside elements--><element><subelement></subelement></element>",
  128. "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
  129. "<element attrib1='foo' attrib2=\"bar\" ></element>",
  130. "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
  131. "<element>Text inside element.</element>",
  132. "<element><b></b></element>",
  133. "<element>Text inside and <b>bolded</b> in the element.</element>",
  134. "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
  135. "<element>This &amp; That.</element>",
  136. "<element attrib='This&lt;That' />",
  137. 0
  138. };
  139. for( int i=0; test[i]; ++i ) {
  140. XMLDocument doc;
  141. doc.Parse( test[i] );
  142. doc.Print();
  143. printf( "----------------------------------------------\n" );
  144. }
  145. }
  146. #if 1
  147. {
  148. static const char* test = "<!--hello world\n"
  149. " line 2\r"
  150. " line 3\r\n"
  151. " line 4\n\r"
  152. " line 5\r-->";
  153. XMLDocument doc;
  154. doc.Parse( test );
  155. doc.Print();
  156. }
  157. {
  158. static const char* test = "<element>Text before.</element>";
  159. XMLDocument doc;
  160. doc.Parse( test );
  161. XMLElement* root = doc.FirstChildElement();
  162. XMLElement* newElement = doc.NewElement( "Subelement" );
  163. root->InsertEndChild( newElement );
  164. doc.Print();
  165. }
  166. {
  167. XMLDocument* doc = new XMLDocument();
  168. static const char* test = "<element><sub/></element>";
  169. doc->Parse( test );
  170. delete doc;
  171. }
  172. {
  173. // Test: Programmatic DOM
  174. // Build:
  175. // <element>
  176. // <!--comment-->
  177. // <sub attrib="1" />
  178. // <sub attrib="2" />
  179. // <sub attrib="3" >& Text!</sub>
  180. // <element>
  181. XMLDocument* doc = new XMLDocument();
  182. XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
  183. XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
  184. for( int i=0; i<3; ++i ) {
  185. sub[i]->SetAttribute( "attrib", i );
  186. }
  187. element->InsertEndChild( sub[2] );
  188. XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
  189. element->InsertAfterChild( comment, sub[0] );
  190. element->InsertAfterChild( sub[0], sub[1] );
  191. sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
  192. doc->Print();
  193. XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
  194. XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
  195. XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
  196. XMLTest( "Programmatic DOM", "& Text!",
  197. doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
  198. // And now deletion:
  199. element->DeleteChild( sub[2] );
  200. doc->DeleteNode( comment );
  201. element->FirstChildElement()->SetAttribute( "attrib", true );
  202. element->LastChildElement()->DeleteAttribute( "attrib" );
  203. XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
  204. int value = 10;
  205. int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
  206. XMLTest( "Programmatic DOM", result, XML_NO_ATTRIBUTE );
  207. XMLTest( "Programmatic DOM", value, 10 );
  208. doc->Print();
  209. XMLPrinter streamer;
  210. doc->Print( &streamer );
  211. printf( "%s", streamer.CStr() );
  212. delete doc;
  213. }
  214. {
  215. // Test: Dream
  216. // XML1 : 1,187,569 bytes in 31,209 allocations
  217. // XML2 : 469,073 bytes in 323 allocations
  218. //int newStart = gNew;
  219. XMLDocument doc;
  220. doc.LoadFile( "dream.xml" );
  221. doc.SaveFile( "dreamout.xml" );
  222. doc.PrintError();
  223. XMLTest( "Dream", "xml version=\"1.0\"",
  224. doc.FirstChild()->ToDeclaration()->Value() );
  225. XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  226. XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  227. doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
  228. XMLTest( "Dream", "And Robin shall restore amends.",
  229. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  230. XMLTest( "Dream", "And Robin shall restore amends.",
  231. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  232. XMLDocument doc2;
  233. doc2.LoadFile( "dreamout.xml" );
  234. XMLTest( "Dream-out", "xml version=\"1.0\"",
  235. doc2.FirstChild()->ToDeclaration()->Value() );
  236. XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  237. XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  238. doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
  239. XMLTest( "Dream-out", "And Robin shall restore amends.",
  240. doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  241. //gNewTotal = gNew - newStart;
  242. }
  243. {
  244. const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
  245. "<passages count=\"006\" formatversion=\"20020620\">\n"
  246. " <wrong error>\n"
  247. "</passages>";
  248. XMLDocument doc;
  249. doc.Parse( error );
  250. XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
  251. }
  252. {
  253. const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
  254. XMLDocument doc;
  255. doc.Parse( str );
  256. XMLElement* ele = doc.FirstChildElement();
  257. int iVal, result;
  258. double dVal;
  259. result = ele->QueryDoubleAttribute( "attr0", &dVal );
  260. XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
  261. XMLTest( "Query attribute: int as double", (int)dVal, 1 );
  262. result = ele->QueryDoubleAttribute( "attr1", &dVal );
  263. XMLTest( "Query attribute: double as double", (int)dVal, 2 );
  264. result = ele->QueryIntAttribute( "attr1", &iVal );
  265. XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
  266. XMLTest( "Query attribute: double as int", iVal, 2 );
  267. result = ele->QueryIntAttribute( "attr2", &iVal );
  268. XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
  269. result = ele->QueryIntAttribute( "bar", &iVal );
  270. XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
  271. }
  272. {
  273. const char* str = "<doc/>";
  274. XMLDocument doc;
  275. doc.Parse( str );
  276. XMLElement* ele = doc.FirstChildElement();
  277. int iVal;
  278. double dVal;
  279. ele->SetAttribute( "str", "strValue" );
  280. ele->SetAttribute( "int", 1 );
  281. ele->SetAttribute( "double", -1.0 );
  282. const char* cStr = ele->Attribute( "str" );
  283. ele->QueryIntAttribute( "int", &iVal );
  284. ele->QueryDoubleAttribute( "double", &dVal );
  285. XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
  286. XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
  287. XMLTest( "Attribute round trip. int.", 1, iVal );
  288. XMLTest( "Attribute round trip. double.", -1, (int)dVal );
  289. }
  290. {
  291. XMLDocument doc;
  292. doc.LoadFile( "utf8test.xml" );
  293. // Get the attribute "value" from the "Russian" element and check it.
  294. XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
  295. const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
  296. 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
  297. XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
  298. const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
  299. 0xd1U, 0x81U, 0xd1U, 0x81U,
  300. 0xd0U, 0xbaU, 0xd0U, 0xb8U,
  301. 0xd0U, 0xb9U, 0 };
  302. const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
  303. XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
  304. XMLTest( "UTF-8: Browsing russian element name.",
  305. russianText,
  306. text->Value() );
  307. // Now try for a round trip.
  308. doc.SaveFile( "utf8testout.xml" );
  309. // Check the round trip.
  310. char savedBuf[256];
  311. char verifyBuf[256];
  312. int okay = 0;
  313. #if defined(_MSC_VER)
  314. #pragma warning ( push )
  315. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  316. #endif
  317. FILE* saved = fopen( "utf8testout.xml", "r" );
  318. FILE* verify = fopen( "utf8testverify.xml", "r" );
  319. #if defined(_MSC_VER)
  320. #pragma warning ( pop )
  321. #endif
  322. if ( saved && verify )
  323. {
  324. okay = 1;
  325. while ( fgets( verifyBuf, 256, verify ) )
  326. {
  327. fgets( savedBuf, 256, saved );
  328. NullLineEndings( verifyBuf );
  329. NullLineEndings( savedBuf );
  330. if ( strcmp( verifyBuf, savedBuf ) )
  331. {
  332. printf( "verify:%s<\n", verifyBuf );
  333. printf( "saved :%s<\n", savedBuf );
  334. okay = 0;
  335. break;
  336. }
  337. }
  338. }
  339. if ( saved )
  340. fclose( saved );
  341. if ( verify )
  342. fclose( verify );
  343. XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
  344. }
  345. // --------GetText()-----------
  346. {
  347. const char* str = "<foo>This is text</foo>";
  348. XMLDocument doc;
  349. doc.Parse( str );
  350. const XMLElement* element = doc.RootElement();
  351. XMLTest( "GetText() normal use.", "This is text", element->GetText() );
  352. str = "<foo><b>This is text</b></foo>";
  353. doc.Parse( str );
  354. element = doc.RootElement();
  355. XMLTest( "GetText() contained element.", element->GetText() == 0, true );
  356. }
  357. // ---------- CDATA ---------------
  358. {
  359. const char* str = "<xmlElement>"
  360. "<![CDATA["
  361. "I am > the rules!\n"
  362. "...since I make symbolic puns"
  363. "]]>"
  364. "</xmlElement>";
  365. XMLDocument doc;
  366. doc.Parse( str );
  367. doc.Print();
  368. XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
  369. "I am > the rules!\n...since I make symbolic puns",
  370. false );
  371. }
  372. // ----------- CDATA -------------
  373. {
  374. const char* str = "<xmlElement>"
  375. "<![CDATA["
  376. "<b>I am > the rules!</b>\n"
  377. "...since I make symbolic puns"
  378. "]]>"
  379. "</xmlElement>";
  380. XMLDocument doc;
  381. doc.Parse( str );
  382. doc.Print();
  383. XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
  384. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  385. false );
  386. }
  387. // InsertAfterChild causes crash.
  388. {
  389. // InsertBeforeChild and InsertAfterChild causes crash.
  390. XMLDocument doc;
  391. XMLElement* parent = doc.NewElement( "Parent" );
  392. doc.InsertFirstChild( parent );
  393. XMLElement* childText0 = doc.NewElement( "childText0" );
  394. XMLElement* childText1 = doc.NewElement( "childText1" );
  395. XMLNode* childNode0 = parent->InsertEndChild( childText0 );
  396. XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
  397. XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
  398. }
  399. {
  400. // Entities not being written correctly.
  401. // From Lynn Allen
  402. const char* passages =
  403. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  404. "<passages count=\"006\" formatversion=\"20020620\">"
  405. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  406. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
  407. "</passages>";
  408. XMLDocument doc;
  409. doc.Parse( passages );
  410. XMLElement* psg = doc.RootElement()->FirstChildElement();
  411. const char* context = psg->Attribute( "context" );
  412. const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
  413. XMLTest( "Entity transformation: read. ", expected, context, true );
  414. #if defined(_MSC_VER)
  415. #pragma warning ( push )
  416. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  417. #endif
  418. FILE* textfile = fopen( "textfile.txt", "w" );
  419. #if defined(_MSC_VER)
  420. #pragma warning ( pop )
  421. #endif
  422. if ( textfile )
  423. {
  424. XMLPrinter streamer( textfile );
  425. psg->Accept( &streamer );
  426. fclose( textfile );
  427. }
  428. #if defined(_MSC_VER)
  429. #pragma warning ( push )
  430. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  431. #endif
  432. textfile = fopen( "textfile.txt", "r" );
  433. #if defined(_MSC_VER)
  434. #pragma warning ( pop )
  435. #endif
  436. TIXMLASSERT( textfile );
  437. if ( textfile )
  438. {
  439. char buf[ 1024 ];
  440. fgets( buf, 1024, textfile );
  441. XMLTest( "Entity transformation: write. ",
  442. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  443. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
  444. buf, false );
  445. }
  446. fclose( textfile );
  447. }
  448. {
  449. // Suppress entities.
  450. const char* passages =
  451. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  452. "<passages count=\"006\" formatversion=\"20020620\">"
  453. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
  454. "</passages>";
  455. XMLDocument doc( false );
  456. doc.Parse( passages );
  457. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
  458. "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
  459. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
  460. "Crazy &ttk;" );
  461. doc.Print();
  462. }
  463. {
  464. const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
  465. XMLDocument doc;
  466. doc.Parse( test );
  467. XMLTest( "dot in names", doc.Error(), 0);
  468. XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
  469. XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
  470. }
  471. {
  472. const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
  473. XMLDocument doc;
  474. doc.Parse( test );
  475. XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
  476. XMLTest( "Entity with one digit.",
  477. text->Value(), "1.1 Start easy ignore fin thickness\n",
  478. false );
  479. }
  480. {
  481. // DOCTYPE not preserved (950171)
  482. //
  483. const char* doctype =
  484. "<?xml version=\"1.0\" ?>"
  485. "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
  486. "<!ELEMENT title (#PCDATA)>"
  487. "<!ELEMENT books (title,authors)>"
  488. "<element />";
  489. XMLDocument doc;
  490. doc.Parse( doctype );
  491. doc.SaveFile( "test7.xml" );
  492. doc.DeleteChild( doc.RootElement() );
  493. doc.LoadFile( "test7.xml" );
  494. doc.Print();
  495. const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
  496. XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
  497. }
  498. {
  499. // Comments do not stream out correctly.
  500. const char* doctype =
  501. "<!-- Somewhat<evil> -->";
  502. XMLDocument doc;
  503. doc.Parse( doctype );
  504. XMLComment* comment = doc.FirstChild()->ToComment();
  505. XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
  506. }
  507. {
  508. // Double attributes
  509. const char* doctype = "<element attr='red' attr='blue' />";
  510. XMLDocument doc;
  511. doc.Parse( doctype );
  512. XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
  513. doc.PrintError();
  514. }
  515. {
  516. // Embedded null in stream.
  517. const char* doctype = "<element att\0r='red' attr='blue' />";
  518. XMLDocument doc;
  519. doc.Parse( doctype );
  520. XMLTest( "Embedded null throws error.", true, doc.Error() );
  521. }
  522. {
  523. // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
  524. const char* str = " ";
  525. XMLDocument doc;
  526. doc.Parse( str );
  527. XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
  528. }
  529. {
  530. // Low entities
  531. XMLDocument doc;
  532. doc.Parse( "<test>&#x0e;</test>" );
  533. const char result[] = { 0x0e, 0 };
  534. XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
  535. doc.Print();
  536. }
  537. {
  538. // Attribute values with trailing quotes not handled correctly
  539. XMLDocument doc;
  540. doc.Parse( "<foo attribute=bar\" />" );
  541. XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
  542. }
  543. {
  544. // [ 1663758 ] Failure to report error on bad XML
  545. XMLDocument xml;
  546. xml.Parse("<x>");
  547. XMLTest("Missing end tag at end of input", xml.Error(), true);
  548. xml.Parse("<x> ");
  549. XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
  550. xml.Parse("<x></y>");
  551. XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
  552. }
  553. {
  554. // [ 1475201 ] TinyXML parses entities in comments
  555. XMLDocument xml;
  556. xml.Parse("<!-- declarations for <head> & <body> -->"
  557. "<!-- far &amp; away -->" );
  558. XMLNode* e0 = xml.FirstChild();
  559. XMLNode* e1 = e0->NextSibling();
  560. XMLComment* c0 = e0->ToComment();
  561. XMLComment* c1 = e1->ToComment();
  562. XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
  563. XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
  564. }
  565. {
  566. XMLDocument xml;
  567. xml.Parse( "<Parent>"
  568. "<child1 att=''/>"
  569. "<!-- With this comment, child2 will not be parsed! -->"
  570. "<child2 att=''/>"
  571. "</Parent>" );
  572. xml.Print();
  573. int count = 0;
  574. for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
  575. ele;
  576. ele = ele->NextSibling() )
  577. {
  578. ++count;
  579. }
  580. XMLTest( "Comments iterate correctly.", 3, count );
  581. }
  582. {
  583. // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
  584. unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
  585. buf[60] = 239;
  586. buf[61] = 0;
  587. XMLDocument doc;
  588. doc.Parse( (const char*)buf);
  589. }
  590. {
  591. // bug 1827248 Error while parsing a little bit malformed file
  592. // Actually not malformed - should work.
  593. XMLDocument xml;
  594. xml.Parse( "<attributelist> </attributelist >" );
  595. XMLTest( "Handle end tag whitespace", false, xml.Error() );
  596. }
  597. {
  598. // This one must not result in an infinite loop
  599. XMLDocument xml;
  600. xml.Parse( "<infinite>loop" );
  601. XMLTest( "Infinite loop test.", true, true );
  602. }
  603. #endif
  604. {
  605. const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
  606. XMLDocument doc;
  607. doc.Parse( pub );
  608. XMLDocument clone;
  609. for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
  610. XMLNode* copy = node->ShallowClone( &clone );
  611. clone.InsertEndChild( copy );
  612. }
  613. clone.Print();
  614. int count=0;
  615. const XMLNode* a=clone.FirstChild();
  616. const XMLNode* b=doc.FirstChild();
  617. for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
  618. ++count;
  619. XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
  620. }
  621. XMLTest( "Clone and Equal", 4, count );
  622. }
  623. {
  624. // This shouldn't crash.
  625. XMLDocument doc;
  626. if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
  627. {
  628. doc.PrintError();
  629. }
  630. XMLTest( "Error in snprinf handling.", true, doc.Error() );
  631. }
  632. // ----------- Performance tracking --------------
  633. {
  634. #if defined( _MSC_VER )
  635. __int64 start, end, freq;
  636. QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
  637. #endif
  638. #if defined(_MSC_VER)
  639. #pragma warning ( push )
  640. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  641. #endif
  642. FILE* fp = fopen( "dream.xml", "r" );
  643. #if defined(_MSC_VER)
  644. #pragma warning ( pop )
  645. #endif
  646. fseek( fp, 0, SEEK_END );
  647. long size = ftell( fp );
  648. fseek( fp, 0, SEEK_SET );
  649. char* mem = new char[size+1];
  650. fread( mem, size, 1, fp );
  651. fclose( fp );
  652. mem[size] = 0;
  653. #if defined( _MSC_VER )
  654. QueryPerformanceCounter( (LARGE_INTEGER*) &start );
  655. #else
  656. clock_t cstart = clock();
  657. #endif
  658. static const int COUNT = 10;
  659. for( int i=0; i<COUNT; ++i ) {
  660. XMLDocument doc;
  661. doc.Parse( mem );
  662. }
  663. #if defined( _MSC_VER )
  664. QueryPerformanceCounter( (LARGE_INTEGER*) &end );
  665. #else
  666. clock_t cend = clock();
  667. #endif
  668. delete [] mem;
  669. static const char* note =
  670. #ifdef DEBUG
  671. "DEBUG";
  672. #else
  673. "Release";
  674. #endif
  675. #if defined( _MSC_VER )
  676. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
  677. #else
  678. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
  679. #endif
  680. }
  681. #if defined( _MSC_VER ) && defined( DEBUG )
  682. _CrtMemCheckpoint( &endMemState );
  683. //_CrtMemDumpStatistics( &endMemState );
  684. _CrtMemState diffMemState;
  685. _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
  686. _CrtMemDumpStatistics( &diffMemState );
  687. //printf( "new total=%d\n", gNewTotal );
  688. #endif
  689. printf ("\nPass %d, Fail %d\n", gPass, gFail);
  690. return 0;
  691. }