XmlSchemaValidatorTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // XmlSchemaValidatorTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2008 Novell Inc.
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using NUnit.Framework;
  15. using MonoTests.Helpers;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlSchemaValidatorTests
  20. {
  21. void Validate (string xml, string xsd)
  22. {
  23. XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
  24. XmlReaderSettings settings = new XmlReaderSettings ();
  25. settings.ValidationType = ValidationType.Schema;
  26. settings.Schemas.Add (schema);
  27. XmlReader reader = XmlReader.Create (new StringReader (xml), settings);
  28. while (reader.Read ())
  29. ;
  30. }
  31. [Test]
  32. public void XsdAnyToSkipAttributeValidation ()
  33. {
  34. // bug #358408
  35. XmlSchemaSet schemas = new XmlSchemaSet ();
  36. schemas.Add (null, TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/358408.xsd"));
  37. XmlSchemaValidator v = new XmlSchemaValidator (
  38. new NameTable (),
  39. schemas,
  40. new XmlNamespaceManager (new NameTable ()),
  41. XmlSchemaValidationFlags.ProcessIdentityConstraints);
  42. v.Initialize ();
  43. v.ValidateWhitespace (" ");
  44. XmlSchemaInfo info = new XmlSchemaInfo ();
  45. ArrayList list = new ArrayList ();
  46. v.ValidateElement ("configuration", "", info, null, null, null, null);
  47. v.GetUnspecifiedDefaultAttributes (list);
  48. v.ValidateEndOfAttributes (info);
  49. v.ValidateWhitespace (" ");
  50. v.ValidateElement ("host", "", info, null, null, null, null);
  51. v.ValidateAttribute ("auto-start", "", "true", info);
  52. list.Clear ();
  53. v.GetUnspecifiedDefaultAttributes (list);
  54. v.ValidateEndOfAttributes (info);
  55. v.ValidateEndElement (null);//info);
  56. v.ValidateWhitespace (" ");
  57. v.ValidateElement ("service-managers", "", info, null, null, null, null);
  58. list.Clear ();
  59. v.GetUnspecifiedDefaultAttributes (list);
  60. v.ValidateEndOfAttributes (info);
  61. v.ValidateWhitespace (" ");
  62. v.ValidateElement ("service-manager", "", info, null, null, null, null);
  63. list.Clear ();
  64. v.GetUnspecifiedDefaultAttributes (list);
  65. v.ValidateEndOfAttributes (info);
  66. v.ValidateWhitespace (" ");
  67. v.ValidateElement ("foo", "", info, null, null, null, null);
  68. v.ValidateAttribute ("bar", "", "", info);
  69. }
  70. [Test]
  71. public void SkipInvolved () // bug #422581
  72. {
  73. XmlReader schemaReader = XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/422581.xsd"));
  74. XmlSchema schema = XmlSchema.Read (schemaReader, null);
  75. XmlReaderSettings settings = new XmlReaderSettings ();
  76. settings.ValidationType = ValidationType.Schema;
  77. settings.Schemas.Add (schema);
  78. XmlReader reader = XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/422581.xml"), settings);
  79. while (reader.Read ());
  80. }
  81. [Test]
  82. public void Bug433774 ()
  83. {
  84. string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  85. <xs:element name='Root'>
  86. <xs:complexType>
  87. <xs:sequence></xs:sequence>
  88. <xs:attribute name='version' type='xs:string' fixed='3' />
  89. </xs:complexType>
  90. </xs:element>
  91. </xs:schema>";
  92. XmlDocument doc = new XmlDocument ();
  93. doc.LoadXml ("<Root version='3' xmlns='urn:foo'/>");
  94. XmlSchemaSet schemaSet = new XmlSchemaSet();
  95. schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
  96. doc.Schemas = schemaSet;
  97. XmlNode root = doc.DocumentElement;
  98. doc.Validate (null, root);
  99. }
  100. [Test]
  101. [ExpectedException (typeof (XmlSchemaValidationException))]
  102. public void Bug435206 ()
  103. {
  104. string xsd = @"<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  105. <xs:element name='myDoc'>
  106. <xs:complexType>
  107. <xs:attribute name='foo' type='xs:unsignedLong' use='required' />
  108. <xs:attribute name='bar' type='xs:dateTime' use='required' />
  109. </xs:complexType>
  110. </xs:element>
  111. </xs:schema>";
  112. string xml = @"<myDoc foo='12' bar='January 1st 1900'/>";
  113. Validate (xml, xsd);
  114. }
  115. [Test]
  116. public void Bug469713 ()
  117. {
  118. string xsd = @"<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  119. <xs:element name='Message'>
  120. <xs:complexType>
  121. <xs:all>
  122. <xs:element name='MyDateTime' nillable='true' type='xs:dateTime' />
  123. </xs:all>
  124. </xs:complexType>
  125. </xs:element>
  126. </xs:schema>";
  127. string xml = @"<Message xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='test.xsd'>
  128. <MyDateTime xsi:nil='true'></MyDateTime>
  129. </Message>";
  130. Validate (xml, xsd);
  131. }
  132. [Test]
  133. public void Bug496192_496205 ()
  134. {
  135. using (var xmlr = new StreamReader (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/496192.xml")))
  136. using (var xsdr = new StreamReader (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/496192.xsd")))
  137. Validate (xmlr.ReadToEnd (), xsdr.ReadToEnd ());
  138. }
  139. [Test]
  140. public void Bug501666 ()
  141. {
  142. string xsd = @"
  143. <xs:schema id='Settings'
  144. targetNamespace='foo'
  145. xmlns='foo'
  146. xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  147. <xs:element name='Settings' type='Settings'/>
  148. <xs:complexType name='Settings'>
  149. <xs:attribute name='port' type='PortNumber' use='required'/>
  150. </xs:complexType>
  151. <xs:simpleType name='PortNumber'>
  152. <xs:restriction base='xs:positiveInteger'>
  153. <xs:minInclusive value='1'/>
  154. <xs:maxInclusive value='65535'/>
  155. </xs:restriction>
  156. </xs:simpleType>
  157. </xs:schema>";
  158. string xml = @"<Settings port='1337' xmlns='foo'/>";
  159. XmlDocument doc = new XmlDocument ();
  160. doc.LoadXml (xml);
  161. doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
  162. doc.Validate (null);
  163. }
  164. public void Bug502251 ()
  165. {
  166. string xsd = @"
  167. <xs:schema id='foo' targetNamespace='foo'
  168. elementFormDefault='qualified'
  169. xmlns='foo'
  170. xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  171. <xs:group name='LayoutElementTypes'>
  172. <xs:choice>
  173. <xs:element name='Rows' type='Rows' />
  174. <xs:element name='Conditional' type='Conditional' />
  175. </xs:choice>
  176. </xs:group>
  177. <xs:complexType name='Element' abstract='true'>
  178. <xs:attribute name='id' type='xs:ID' use='optional'/>
  179. </xs:complexType>
  180. <xs:complexType name='SingleChildElement' abstract='true'>
  181. <xs:complexContent>
  182. <xs:extension base='Element'>
  183. <xs:group ref='LayoutElementTypes' minOccurs='1' maxOccurs='1' />
  184. </xs:extension>
  185. </xs:complexContent>
  186. </xs:complexType>
  187. <xs:complexType name='Rows'>
  188. <xs:complexContent>
  189. <xs:extension base='Element'>
  190. <xs:sequence minOccurs='1' maxOccurs='unbounded'>
  191. <xs:element name='Row' type='Row' />
  192. </xs:sequence>
  193. </xs:extension>
  194. </xs:complexContent>
  195. </xs:complexType>
  196. <xs:complexType name='Row'>
  197. <xs:complexContent>
  198. <xs:extension base='SingleChildElement'>
  199. </xs:extension>
  200. </xs:complexContent>
  201. </xs:complexType>
  202. <xs:complexType name='Conditional'>
  203. <xs:complexContent>
  204. <xs:extension base='Element'>
  205. </xs:extension>
  206. </xs:complexContent>
  207. </xs:complexType>
  208. <xs:complexType name='Layout'>
  209. <xs:complexContent>
  210. <xs:extension base='SingleChildElement'>
  211. </xs:extension>
  212. </xs:complexContent>
  213. </xs:complexType>
  214. <xs:element name='Layout' type='Layout' />
  215. </xs:schema>";
  216. XmlDocument doc = new XmlDocument ();
  217. doc.LoadXml (@"<Layout xmlns='foo'>
  218. <Rows>
  219. <Row><Conditional/></Row>
  220. </Rows>
  221. </Layout>");
  222. XmlSchema schema = XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null);
  223. doc.Schemas.Add (schema);
  224. doc.Validate (null);
  225. }
  226. [Test]
  227. public void Bug557452 ()
  228. {
  229. string xsd = @"
  230. <xs:schema id='Settings'
  231. targetNamespace='foo'
  232. xmlns='foo'
  233. xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  234. <xs:element name='Settings' type='Settings'/>
  235. <xs:complexType name='Settings'>
  236. <xs:attribute name='port' type='PortNumber' use='required'/>
  237. </xs:complexType>
  238. <xs:simpleType name='PortNumber'>
  239. <xs:restriction base='xs:decimal'>
  240. <xs:minInclusive value='1'/>
  241. <xs:maxInclusive value='65535'/>
  242. </xs:restriction>
  243. </xs:simpleType>
  244. </xs:schema>";
  245. string xml = @"<Settings port='1337' xmlns='foo'/>";
  246. XmlDocument doc = new XmlDocument ();
  247. doc.LoadXml (xml);
  248. doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
  249. doc.Validate (null);
  250. }
  251. [Test]
  252. public void Bug584664 ()
  253. {
  254. Validate (File.ReadAllText (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/584664a.xml")), File.ReadAllText (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/584664a.xsd")));
  255. Validate (File.ReadAllText (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/584664b.xml")), File.ReadAllText (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/584664b.xsd")));
  256. }
  257. [Test]
  258. public void MultipleMissingIds ()
  259. {
  260. var schema = XmlSchema.Read (new StringReader (@"<?xml version=""1.0"" encoding=""utf-8""?>
  261. <xs:schema targetNamespace=""urn:multiple-ids"" elementFormDefault=""qualified"" xmlns=""urn:multiple-ids"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
  262. <xs:element name=""root"">
  263. <xs:complexType>
  264. <xs:sequence minOccurs=""0"" maxOccurs=""unbounded"">
  265. <xs:element name=""item"">
  266. <xs:complexType>
  267. <xs:attribute name=""id"" type=""xs:ID"" />
  268. <xs:attribute name=""parent"" type=""xs:IDREF"" />
  269. </xs:complexType>
  270. </xs:element>
  271. </xs:sequence>
  272. </xs:complexType>
  273. </xs:element>
  274. </xs:schema>"), null);
  275. var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
  276. <root xmlns=""urn:multiple-ids"">
  277. <item id=""id2"" parent=""id1"" />
  278. <item id=""id3"" parent=""id1"" />
  279. <item id=""id1"" parent=""id1"" />
  280. </root>";
  281. var document = new XmlDocument ();
  282. document.LoadXml (xml);
  283. document.Schemas = new XmlSchemaSet ();
  284. document.Schemas.Add (schema);
  285. document.Validate (null);
  286. }
  287. [Test]
  288. public void FacetsOnBaseSimpleContentRestriction ()
  289. {
  290. XmlReaderSettings settings = new XmlReaderSettings ();
  291. settings.Schemas.Add (null, TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/595947.xsd"));
  292. settings.ValidationType = ValidationType.Schema;
  293. settings.Schemas.Compile ();
  294. Validate ("TEST 1.1", 1, "0123456789", "0123456789", settings, false);
  295. Validate ("TEST 1.2", 1, "0123456789***", "0123456789", settings, true);
  296. Validate ("TEST 1.3", 1, "0123456789", "0123456789***", settings, true);
  297. Validate ("TEST 2.1", 2, "0123456789", "0123456789", settings, false);
  298. Validate ("TEST 2.2", 2, "0123456789***", "0123456789", settings, true);
  299. Validate ("TEST 2.3", 2, "0123456789", "0123456789***", settings, true);
  300. Validate ("TEST 3.1", 3, "0123456789", "0123456789", settings, false);
  301. Validate ("TEST 3.2", 3, "0123456789***", "0123456789", settings, true);
  302. Validate ("TEST 3.3", 3, "0123456789", "0123456789***", settings, true);
  303. }
  304. void Validate (string testName, int testNumber, string idValue, string elementValue, XmlReaderSettings settings, bool shouldFail)
  305. {
  306. string content = string.Format ("<MyTest{0} Id=\"{1}\">{2}</MyTest{0}>", testNumber, idValue, elementValue);
  307. try
  308. {
  309. XmlReader reader = XmlReader.Create (new StringReader (content), settings);
  310. XmlDocument document = new XmlDocument ();
  311. document.Load (reader);
  312. document.Validate (null);
  313. } catch (Exception) {
  314. if (!shouldFail)
  315. throw;
  316. return;
  317. }
  318. if (shouldFail)
  319. Assert.Fail (testName + " should fail");
  320. }
  321. [Test]
  322. public void Bug676993 ()
  323. {
  324. Validate (File.ReadAllText (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/676993.xml")), File.ReadAllText (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/676993.xsd")));
  325. }
  326. [Test]
  327. public void Bug10245 ()
  328. {
  329. string xsd = @"
  330. <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo'>
  331. <xs:element name='root'>
  332. <xs:complexType>
  333. <xs:attribute name='d' default='v' use='optional' />
  334. </xs:complexType>
  335. </xs:element>
  336. </xs:schema>";
  337. string xml = "<root xmlns='urn:foo' />";
  338. var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
  339. xrs.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null));
  340. var xr = XmlReader.Create (new StringReader (xml), xrs);
  341. xr.Read ();
  342. bool more;
  343. Assert.AreEqual (2, xr.AttributeCount, "#1");
  344. int i = 0;
  345. for (more = xr.MoveToFirstAttribute (); more; more = xr.MoveToNextAttribute ())
  346. i++;
  347. Assert.AreEqual (2, i, "#2");
  348. }
  349. [Test]
  350. public void Bug12035 ()
  351. {
  352. string xml = @"<UserSettings
  353. xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  354. xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  355. xmlns='http://schema/test'><Enabled>false</Enabled><Time xsi:nil='true' /></UserSettings>";
  356. string xsd = @"<?xml version='1.0' encoding='utf-8'?>
  357. <xs:schema
  358. targetNamespace='http://schema/test'
  359. xmlns='http://schema/test'
  360. xmlns:xs='http://www.w3.org/2001/XMLSchema'
  361. elementFormDefault='qualified'>
  362. <xs:element name='UserSettings'>
  363. <xs:complexType>
  364. <xs:sequence>
  365. <xs:element name='Enabled' type='xs:boolean' />
  366. <xs:element name='Time' type='CoarseTime' nillable='true' />
  367. </xs:sequence>
  368. </xs:complexType>
  369. </xs:element>
  370. <xs:complexType name='CoarseTime'>
  371. <xs:sequence>
  372. <xs:element name='Hours' type='xs:int' />
  373. </xs:sequence>
  374. </xs:complexType>
  375. </xs:schema>";
  376. var schema = XmlSchema.Read (new StringReader (xsd), null);
  377. var schemaSet = new XmlSchemaSet ();
  378. schemaSet.Add (schema);
  379. var xmlReaderSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
  380. xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
  381. xmlReaderSettings.Schemas.Add (schemaSet);
  382. using (var configStream = new StringReader (xml)) {
  383. using (var validatingReader = XmlReader.Create (configStream, xmlReaderSettings)) {
  384. // Read the XML, throwing an exception if a validation error occurs
  385. while (validatingReader.Read()) {
  386. }
  387. }
  388. }
  389. }
  390. [Test]
  391. public void IgnoresInvalidBaseUri ()
  392. {
  393. var source = new StringReader (@"<?xml version='1.0' encoding='utf-8'?><Test></Test>");
  394. var readerSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
  395. var reader = XmlReader.Create (source, readerSettings, "invalidBaseUri");
  396. Assert.IsNotNull (reader);
  397. }
  398. }
  399. }