2 // XmlSchemaValidatorTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2008 Novell Inc.
12 using System.Collections;
15 using System.Xml.Schema;
16 using NUnit.Framework;
18 namespace MonoTests.System.Xml
21 public class XmlSchemaValidatorTests
23 void Validate (string xml, string xsd)
25 XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
26 XmlReaderSettings settings = new XmlReaderSettings ();
27 settings.ValidationType = ValidationType.Schema;
28 settings.Schemas.Add (schema);
29 XmlReader reader = XmlReader.Create (new StringReader (xml), settings);
30 while (reader.Read ())
35 public void XsdAnyToSkipAttributeValidation ()
38 XmlSchemaSet schemas = new XmlSchemaSet ();
39 schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd");
40 XmlSchemaValidator v = new XmlSchemaValidator (
43 new XmlNamespaceManager (new NameTable ()),
44 XmlSchemaValidationFlags.ProcessIdentityConstraints);
46 v.ValidateWhitespace (" ");
47 XmlSchemaInfo info = new XmlSchemaInfo ();
48 ArrayList list = new ArrayList ();
50 v.ValidateElement ("configuration", "", info, null, null, null, null);
51 v.GetUnspecifiedDefaultAttributes (list);
52 v.ValidateEndOfAttributes (info);
54 v.ValidateWhitespace (" ");
56 v.ValidateElement ("host", "", info, null, null, null, null);
57 v.ValidateAttribute ("auto-start", "", "true", info);
59 v.GetUnspecifiedDefaultAttributes (list);
60 v.ValidateEndOfAttributes (info);
61 v.ValidateEndElement (null);//info);
63 v.ValidateWhitespace (" ");
65 v.ValidateElement ("service-managers", "", info, null, null, null, null);
67 v.GetUnspecifiedDefaultAttributes (list);
68 v.ValidateEndOfAttributes (info);
70 v.ValidateWhitespace (" ");
72 v.ValidateElement ("service-manager", "", info, null, null, null, null);
74 v.GetUnspecifiedDefaultAttributes (list);
75 v.ValidateEndOfAttributes (info);
77 v.ValidateWhitespace (" ");
79 v.ValidateElement ("foo", "", info, null, null, null, null);
80 v.ValidateAttribute ("bar", "", "", info);
84 public void SkipInvolved () // bug #422581
86 XmlReader schemaReader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xsd");
87 XmlSchema schema = XmlSchema.Read (schemaReader, null);
88 XmlReaderSettings settings = new XmlReaderSettings ();
89 settings.ValidationType = ValidationType.Schema;
90 settings.Schemas.Add (schema);
91 XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xml", settings);
92 while (reader.Read ());
96 public void Bug433774 ()
98 string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
99 <xs:element name='Root'>
101 <xs:sequence></xs:sequence>
102 <xs:attribute name='version' type='xs:string' fixed='3' />
106 XmlDocument doc = new XmlDocument ();
107 doc.LoadXml ("<Root version='3' xmlns='urn:foo'/>");
108 XmlSchemaSet schemaSet = new XmlSchemaSet();
109 schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
110 doc.Schemas = schemaSet;
111 XmlNode root = doc.DocumentElement;
112 doc.Validate (null, root);
116 [ExpectedException (typeof (XmlSchemaValidationException))]
117 public void Bug435206 ()
119 string xsd = @"<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
120 <xs:element name='myDoc'>
122 <xs:attribute name='foo' type='xs:unsignedLong' use='required' />
123 <xs:attribute name='bar' type='xs:dateTime' use='required' />
127 string xml = @"<myDoc foo='12' bar='January 1st 1900'/>";
132 public void Bug469713 ()
134 string xsd = @"<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
135 <xs:element name='Message'>
138 <xs:element name='MyDateTime' nillable='true' type='xs:dateTime' />
143 string xml = @"<Message xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='test.xsd'>
144 <MyDateTime xsi:nil='true'></MyDateTime>
150 public void Bug496192_496205 ()
152 using (var xmlr = new StreamReader ("Test/XmlFiles/496192.xml"))
153 using (var xsdr = new StreamReader ("Test/XmlFiles/496192.xsd"))
154 Validate (xmlr.ReadToEnd (), xsdr.ReadToEnd ());
158 public void Bug501666 ()
161 <xs:schema id='Settings'
162 targetNamespace='foo'
164 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
166 <xs:element name='Settings' type='Settings'/>
168 <xs:complexType name='Settings'>
169 <xs:attribute name='port' type='PortNumber' use='required'/>
172 <xs:simpleType name='PortNumber'>
173 <xs:restriction base='xs:positiveInteger'>
174 <xs:minInclusive value='1'/>
175 <xs:maxInclusive value='65535'/>
180 string xml = @"<Settings port='1337' xmlns='foo'/>";
182 XmlDocument doc = new XmlDocument ();
184 doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
188 public void Bug502251 ()
191 <xs:schema id='foo' targetNamespace='foo'
192 elementFormDefault='qualified'
194 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
196 <xs:group name='LayoutElementTypes'>
198 <xs:element name='Rows' type='Rows' />
199 <xs:element name='Conditional' type='Conditional' />
203 <xs:complexType name='Element' abstract='true'>
204 <xs:attribute name='id' type='xs:ID' use='optional'/>
207 <xs:complexType name='SingleChildElement' abstract='true'>
209 <xs:extension base='Element'>
210 <xs:group ref='LayoutElementTypes' minOccurs='1' maxOccurs='1' />
215 <xs:complexType name='Rows'>
217 <xs:extension base='Element'>
218 <xs:sequence minOccurs='1' maxOccurs='unbounded'>
219 <xs:element name='Row' type='Row' />
225 <xs:complexType name='Row'>
227 <xs:extension base='SingleChildElement'>
232 <xs:complexType name='Conditional'>
234 <xs:extension base='Element'>
239 <xs:complexType name='Layout'>
241 <xs:extension base='SingleChildElement'>
246 <xs:element name='Layout' type='Layout' />
249 XmlDocument doc = new XmlDocument ();
250 doc.LoadXml (@"<Layout xmlns='foo'>
252 <Row><Conditional/></Row>
256 XmlSchema schema = XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null);
258 doc.Schemas.Add (schema);
263 public void Bug557452 ()
266 <xs:schema id='Settings'
267 targetNamespace='foo'
269 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
271 <xs:element name='Settings' type='Settings'/>
273 <xs:complexType name='Settings'>
274 <xs:attribute name='port' type='PortNumber' use='required'/>
277 <xs:simpleType name='PortNumber'>
278 <xs:restriction base='xs:decimal'>
279 <xs:minInclusive value='1'/>
280 <xs:maxInclusive value='65535'/>
285 string xml = @"<Settings port='1337' xmlns='foo'/>";
287 XmlDocument doc = new XmlDocument ();
289 doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
294 public void Bug584664 ()
296 Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664a.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664a.xsd"));
297 Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664b.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664b.xsd"));
301 public void MultipleMissingIds ()
303 var schema = XmlSchema.Read (new StringReader (@"<?xml version=""1.0"" encoding=""utf-8""?>
304 <xs:schema targetNamespace=""urn:multiple-ids"" elementFormDefault=""qualified"" xmlns=""urn:multiple-ids"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
305 <xs:element name=""root"">
307 <xs:sequence minOccurs=""0"" maxOccurs=""unbounded"">
308 <xs:element name=""item"">
310 <xs:attribute name=""id"" type=""xs:ID"" />
311 <xs:attribute name=""parent"" type=""xs:IDREF"" />
317 </xs:schema>"), null);
318 var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
319 <root xmlns=""urn:multiple-ids"">
320 <item id=""id2"" parent=""id1"" />
321 <item id=""id3"" parent=""id1"" />
322 <item id=""id1"" parent=""id1"" />
324 var document = new XmlDocument ();
325 document.LoadXml (xml);
326 document.Schemas = new XmlSchemaSet ();
327 document.Schemas.Add (schema);
328 document.Validate (null);
332 public void FacetsOnBaseSimpleContentRestriction ()
334 XmlReaderSettings settings = new XmlReaderSettings ();
335 settings.Schemas.Add (null, "Test/XmlFiles/595947.xsd");
336 settings.ValidationType = ValidationType.Schema;
337 settings.Schemas.Compile ();
339 Validate ("TEST 1.1", 1, "0123456789", "0123456789", settings, false);
340 Validate ("TEST 1.2", 1, "0123456789***", "0123456789", settings, true);
341 Validate ("TEST 1.3", 1, "0123456789", "0123456789***", settings, true);
343 Validate ("TEST 2.1", 2, "0123456789", "0123456789", settings, false);
344 Validate ("TEST 2.2", 2, "0123456789***", "0123456789", settings, true);
345 Validate ("TEST 2.3", 2, "0123456789", "0123456789***", settings, true);
347 Validate ("TEST 3.1", 3, "0123456789", "0123456789", settings, false);
348 Validate ("TEST 3.2", 3, "0123456789***", "0123456789", settings, true);
349 Validate ("TEST 3.3", 3, "0123456789", "0123456789***", settings, true);
352 void Validate (string testName, int testNumber, string idValue, string elementValue, XmlReaderSettings settings, bool shouldFail)
354 string content = string.Format ("<MyTest{0} Id=\"{1}\">{2}</MyTest{0}>", testNumber, idValue, elementValue);
357 XmlReader reader = XmlReader.Create (new StringReader (content), settings);
358 XmlDocument document = new XmlDocument ();
359 document.Load (reader);
360 document.Validate (null);
361 } catch (Exception e) {
367 Assert.Fail (testName + " should fail");
371 public void Bug676993 ()
373 Validate (File.ReadAllText ("Test/XmlFiles/676993.xml"), File.ReadAllText ("Test/XmlFiles/676993.xsd"));
377 public void Bug10245 ()
380 <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo'>
381 <xs:element name='root'>
383 <xs:attribute name='d' default='v' use='optional' />
387 string xml = "<root xmlns='urn:foo' />";
388 var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
389 xrs.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null));
390 var xr = XmlReader.Create (new StringReader (xml), xrs);
393 Assert.AreEqual (2, xr.AttributeCount, "#1");
395 for (more = xr.MoveToFirstAttribute (); more; more = xr.MoveToNextAttribute ())
397 Assert.AreEqual (2, i, "#2");
401 public void Bug12035 ()
403 string xml = @"<UserSettings
404 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
405 xmlns:xsd='http://www.w3.org/2001/XMLSchema'
406 xmlns='http://schema/test'><Enabled>false</Enabled><Time xsi:nil='true' /></UserSettings>";
407 string xsd = @"<?xml version='1.0' encoding='utf-8'?>
409 targetNamespace='http://schema/test'
410 xmlns='http://schema/test'
411 xmlns:xs='http://www.w3.org/2001/XMLSchema'
412 elementFormDefault='qualified'>
413 <xs:element name='UserSettings'>
416 <xs:element name='Enabled' type='xs:boolean' />
417 <xs:element name='Time' type='CoarseTime' nillable='true' />
422 <xs:complexType name='CoarseTime'>
424 <xs:element name='Hours' type='xs:int' />
428 var schema = XmlSchema.Read (new StringReader (xsd), null);
429 var schemaSet = new XmlSchemaSet ();
430 schemaSet.Add (schema);
431 var xmlReaderSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
432 xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
433 xmlReaderSettings.Schemas.Add (schemaSet);
435 using (var configStream = new StringReader (xml)) {
436 using (var validatingReader = XmlReader.Create (configStream, xmlReaderSettings)) {
437 // Read the XML, throwing an exception if a validation error occurs
438 while (validatingReader.Read()) {
445 public void IgnoresInvalidBaseUri ()
447 var source = new StringReader (@"<?xml version='1.0' encoding='utf-8'?><Test></Test>");
448 var readerSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
449 var reader = XmlReader.Create (source, readerSettings, "invalidBaseUri");
451 Assert.IsNotNull (reader);