XmlSchemaValidatorTests.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // XmlSchemaValidatorTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2008 Novell Inc.
  8. //
  9. #if NET_2_0
  10. using System;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using NUnit.Framework;
  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, "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 ("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 ("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 ("Test/XmlFiles/496192.xml"))
  136. using (var xsdr = new StreamReader ("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. }
  252. }
  253. #endif