2 // XmlSchemaValidatorTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2008 Novell Inc.
13 using System.Collections;
16 using System.Xml.Schema;
17 using NUnit.Framework;
19 namespace MonoTests.System.Xml
22 public class XmlSchemaValidatorTests
24 void Validate (string xml, string xsd)
26 XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
27 XmlReaderSettings settings = new XmlReaderSettings ();
28 settings.ValidationType = ValidationType.Schema;
29 settings.Schemas.Add (schema);
30 XmlReader reader = XmlReader.Create (new StringReader (xml), settings);
31 while (reader.Read ())
36 public void XsdAnyToSkipAttributeValidation ()
39 XmlSchemaSet schemas = new XmlSchemaSet ();
40 schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd");
41 XmlSchemaValidator v = new XmlSchemaValidator (
44 new XmlNamespaceManager (new NameTable ()),
45 XmlSchemaValidationFlags.ProcessIdentityConstraints);
47 v.ValidateWhitespace (" ");
48 XmlSchemaInfo info = new XmlSchemaInfo ();
49 ArrayList list = new ArrayList ();
51 v.ValidateElement ("configuration", "", info, null, null, null, null);
52 v.GetUnspecifiedDefaultAttributes (list);
53 v.ValidateEndOfAttributes (info);
55 v.ValidateWhitespace (" ");
57 v.ValidateElement ("host", "", info, null, null, null, null);
58 v.ValidateAttribute ("auto-start", "", "true", info);
60 v.GetUnspecifiedDefaultAttributes (list);
61 v.ValidateEndOfAttributes (info);
62 v.ValidateEndElement (null);//info);
64 v.ValidateWhitespace (" ");
66 v.ValidateElement ("service-managers", "", info, null, null, null, null);
68 v.GetUnspecifiedDefaultAttributes (list);
69 v.ValidateEndOfAttributes (info);
71 v.ValidateWhitespace (" ");
73 v.ValidateElement ("service-manager", "", info, null, null, null, null);
75 v.GetUnspecifiedDefaultAttributes (list);
76 v.ValidateEndOfAttributes (info);
78 v.ValidateWhitespace (" ");
80 v.ValidateElement ("foo", "", info, null, null, null, null);
81 v.ValidateAttribute ("bar", "", "", info);
85 public void SkipInvolved () // bug #422581
87 XmlReader schemaReader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xsd");
88 XmlSchema schema = XmlSchema.Read (schemaReader, null);
89 XmlReaderSettings settings = new XmlReaderSettings ();
90 settings.ValidationType = ValidationType.Schema;
91 settings.Schemas.Add (schema);
92 XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xml", settings);
93 while (reader.Read ());
97 public void Bug433774 ()
99 string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
100 <xs:element name='Root'>
102 <xs:sequence></xs:sequence>
103 <xs:attribute name='version' type='xs:string' fixed='3' />
107 XmlDocument doc = new XmlDocument ();
108 doc.LoadXml ("<Root version='3' xmlns='urn:foo'/>");
109 XmlSchemaSet schemaSet = new XmlSchemaSet();
110 schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
111 doc.Schemas = schemaSet;
112 XmlNode root = doc.DocumentElement;
113 doc.Validate (null, root);
117 [ExpectedException (typeof (XmlSchemaValidationException))]
118 public void Bug435206 ()
120 string xsd = @"<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
121 <xs:element name='myDoc'>
123 <xs:attribute name='foo' type='xs:unsignedLong' use='required' />
124 <xs:attribute name='bar' type='xs:dateTime' use='required' />
128 string xml = @"<myDoc foo='12' bar='January 1st 1900'/>";
133 public void Bug469713 ()
135 string xsd = @"<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
136 <xs:element name='Message'>
139 <xs:element name='MyDateTime' nillable='true' type='xs:dateTime' />
144 string xml = @"<Message xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='test.xsd'>
145 <MyDateTime xsi:nil='true'></MyDateTime>
151 public void Bug496192_496205 ()
153 using (var xmlr = new StreamReader ("Test/XmlFiles/496192.xml"))
154 using (var xsdr = new StreamReader ("Test/XmlFiles/496192.xsd"))
155 Validate (xmlr.ReadToEnd (), xsdr.ReadToEnd ());
159 public void Bug501666 ()
162 <xs:schema id='Settings'
163 targetNamespace='foo'
165 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
167 <xs:element name='Settings' type='Settings'/>
169 <xs:complexType name='Settings'>
170 <xs:attribute name='port' type='PortNumber' use='required'/>
173 <xs:simpleType name='PortNumber'>
174 <xs:restriction base='xs:positiveInteger'>
175 <xs:minInclusive value='1'/>
176 <xs:maxInclusive value='65535'/>
181 string xml = @"<Settings port='1337' xmlns='foo'/>";
183 XmlDocument doc = new XmlDocument ();
185 doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
189 public void Bug502251 ()
192 <xs:schema id='foo' targetNamespace='foo'
193 elementFormDefault='qualified'
195 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
197 <xs:group name='LayoutElementTypes'>
199 <xs:element name='Rows' type='Rows' />
200 <xs:element name='Conditional' type='Conditional' />
204 <xs:complexType name='Element' abstract='true'>
205 <xs:attribute name='id' type='xs:ID' use='optional'/>
208 <xs:complexType name='SingleChildElement' abstract='true'>
210 <xs:extension base='Element'>
211 <xs:group ref='LayoutElementTypes' minOccurs='1' maxOccurs='1' />
216 <xs:complexType name='Rows'>
218 <xs:extension base='Element'>
219 <xs:sequence minOccurs='1' maxOccurs='unbounded'>
220 <xs:element name='Row' type='Row' />
226 <xs:complexType name='Row'>
228 <xs:extension base='SingleChildElement'>
233 <xs:complexType name='Conditional'>
235 <xs:extension base='Element'>
240 <xs:complexType name='Layout'>
242 <xs:extension base='SingleChildElement'>
247 <xs:element name='Layout' type='Layout' />
250 XmlDocument doc = new XmlDocument ();
251 doc.LoadXml (@"<Layout xmlns='foo'>
253 <Row><Conditional/></Row>
257 XmlSchema schema = XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null);
259 doc.Schemas.Add (schema);
264 public void Bug557452 ()
267 <xs:schema id='Settings'
268 targetNamespace='foo'
270 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
272 <xs:element name='Settings' type='Settings'/>
274 <xs:complexType name='Settings'>
275 <xs:attribute name='port' type='PortNumber' use='required'/>
278 <xs:simpleType name='PortNumber'>
279 <xs:restriction base='xs:decimal'>
280 <xs:minInclusive value='1'/>
281 <xs:maxInclusive value='65535'/>
286 string xml = @"<Settings port='1337' xmlns='foo'/>";
288 XmlDocument doc = new XmlDocument ();
290 doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
295 public void Bug584664 ()
297 Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664a.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664a.xsd"));
298 Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664b.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664b.xsd"));
302 public void MultipleMissingIds ()
304 var schema = XmlSchema.Read (new StringReader (@"<?xml version=""1.0"" encoding=""utf-8""?>
305 <xs:schema targetNamespace=""urn:multiple-ids"" elementFormDefault=""qualified"" xmlns=""urn:multiple-ids"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
306 <xs:element name=""root"">
308 <xs:sequence minOccurs=""0"" maxOccurs=""unbounded"">
309 <xs:element name=""item"">
311 <xs:attribute name=""id"" type=""xs:ID"" />
312 <xs:attribute name=""parent"" type=""xs:IDREF"" />
318 </xs:schema>"), null);
319 var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
320 <root xmlns=""urn:multiple-ids"">
321 <item id=""id2"" parent=""id1"" />
322 <item id=""id3"" parent=""id1"" />
323 <item id=""id1"" parent=""id1"" />
325 var document = new XmlDocument ();
326 document.LoadXml (xml);
327 document.Schemas = new XmlSchemaSet ();
328 document.Schemas.Add (schema);
329 document.Validate (null);
333 public void FacetsOnBaseSimpleContentRestriction ()
335 XmlReaderSettings settings = new XmlReaderSettings ();
336 settings.Schemas.Add (null, "Test/XmlFiles/595947.xsd");
337 settings.ValidationType = ValidationType.Schema;
338 settings.Schemas.Compile ();
340 Validate ("TEST 1.1", 1, "0123456789", "0123456789", settings, false);
341 Validate ("TEST 1.2", 1, "0123456789***", "0123456789", settings, true);
342 Validate ("TEST 1.3", 1, "0123456789", "0123456789***", settings, true);
344 Validate ("TEST 2.1", 2, "0123456789", "0123456789", settings, false);
345 Validate ("TEST 2.2", 2, "0123456789***", "0123456789", settings, true);
346 Validate ("TEST 2.3", 2, "0123456789", "0123456789***", settings, true);
348 Validate ("TEST 3.1", 3, "0123456789", "0123456789", settings, false);
349 Validate ("TEST 3.2", 3, "0123456789***", "0123456789", settings, true);
350 Validate ("TEST 3.3", 3, "0123456789", "0123456789***", settings, true);
353 void Validate (string testName, int testNumber, string idValue, string elementValue, XmlReaderSettings settings, bool shouldFail)
355 string content = string.Format ("<MyTest{0} Id=\"{1}\">{2}</MyTest{0}>", testNumber, idValue, elementValue);
358 XmlReader reader = XmlReader.Create (new StringReader (content), settings);
359 XmlDocument document = new XmlDocument ();
360 document.Load (reader);
361 document.Validate (null);
362 } catch (Exception e) {
368 Assert.Fail (testName + " should fail");
372 public void Bug676993 ()
374 Validate (File.ReadAllText ("Test/XmlFiles/676993.xml"), File.ReadAllText ("Test/XmlFiles/676993.xsd"));
378 public void Bug10245 ()
381 <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo'>
382 <xs:element name='root'>
384 <xs:attribute name='d' default='v' use='optional' />
388 string xml = "<root xmlns='urn:foo' />";
389 var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema };
390 xrs.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null));
391 var xr = XmlReader.Create (new StringReader (xml), xrs);
394 Assert.AreEqual (2, xr.AttributeCount, "#1");
396 for (more = xr.MoveToFirstAttribute (); more; more = xr.MoveToNextAttribute ())
398 Assert.AreEqual (2, i, "#2");
402 public void Bug12035 ()
404 string xml = @"<UserSettings
405 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
406 xmlns:xsd='http://www.w3.org/2001/XMLSchema'
407 xmlns='http://schema/test'><Enabled>false</Enabled><Time xsi:nil='true' /></UserSettings>";
408 string xsd = @"<?xml version='1.0' encoding='utf-8'?>
410 targetNamespace='http://schema/test'
411 xmlns='http://schema/test'
412 xmlns:xs='http://www.w3.org/2001/XMLSchema'
413 elementFormDefault='qualified'>
414 <xs:element name='UserSettings'>
417 <xs:element name='Enabled' type='xs:boolean' />
418 <xs:element name='Time' type='CoarseTime' nillable='true' />
423 <xs:complexType name='CoarseTime'>
425 <xs:element name='Hours' type='xs:int' />
429 var schema = XmlSchema.Read (new StringReader (xsd), null);
430 var schemaSet = new XmlSchemaSet ();
431 schemaSet.Add (schema);
432 var xmlReaderSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
433 xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
434 xmlReaderSettings.Schemas.Add (schemaSet);
436 using (var configStream = new StringReader (xml)) {
437 using (var validatingReader = XmlReader.Create (configStream, xmlReaderSettings)) {
438 // Read the XML, throwing an exception if a validation error occurs
439 while (validatingReader.Read()) {
446 public void IgnoresInvalidBaseUri ()
448 var source = new StringReader (@"<?xml version='1.0' encoding='utf-8'?><Test></Test>");
449 var readerSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema };
450 var reader = XmlReader.Create (source, readerSettings, "invalidBaseUri");
452 Assert.IsNotNull (reader);