XmlSchemaValidatorTests.cs 14 KB

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