// // XmlSchemaValidatorTests.cs // // Author: // Atsushi Enomoto // // (C) 2008 Novell Inc. // using System; using System.Collections; using System.IO; using System.Xml; using System.Xml.Schema; using NUnit.Framework; namespace MonoTests.System.Xml { [TestFixture] public class XmlSchemaValidatorTests { void Validate (string xml, string xsd) { XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null); XmlReaderSettings settings = new XmlReaderSettings (); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add (schema); XmlReader reader = XmlReader.Create (new StringReader (xml), settings); while (reader.Read ()) ; } [Test] public void XsdAnyToSkipAttributeValidation () { // bug #358408 XmlSchemaSet schemas = new XmlSchemaSet (); schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd"); XmlSchemaValidator v = new XmlSchemaValidator ( new NameTable (), schemas, new XmlNamespaceManager (new NameTable ()), XmlSchemaValidationFlags.ProcessIdentityConstraints); v.Initialize (); v.ValidateWhitespace (" "); XmlSchemaInfo info = new XmlSchemaInfo (); ArrayList list = new ArrayList (); v.ValidateElement ("configuration", "", info, null, null, null, null); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateWhitespace (" "); v.ValidateElement ("host", "", info, null, null, null, null); v.ValidateAttribute ("auto-start", "", "true", info); list.Clear (); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateEndElement (null);//info); v.ValidateWhitespace (" "); v.ValidateElement ("service-managers", "", info, null, null, null, null); list.Clear (); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateWhitespace (" "); v.ValidateElement ("service-manager", "", info, null, null, null, null); list.Clear (); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateWhitespace (" "); v.ValidateElement ("foo", "", info, null, null, null, null); v.ValidateAttribute ("bar", "", "", info); } [Test] public void SkipInvolved () // bug #422581 { XmlReader schemaReader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xsd"); XmlSchema schema = XmlSchema.Read (schemaReader, null); XmlReaderSettings settings = new XmlReaderSettings (); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add (schema); XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xml", settings); while (reader.Read ()); } [Test] public void Bug433774 () { string xsd = @" "; XmlDocument doc = new XmlDocument (); doc.LoadXml (""); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null)); doc.Schemas = schemaSet; XmlNode root = doc.DocumentElement; doc.Validate (null, root); } [Test] [ExpectedException (typeof (XmlSchemaValidationException))] public void Bug435206 () { string xsd = @" "; string xml = @""; Validate (xml, xsd); } [Test] public void Bug469713 () { string xsd = @" "; string xml = @" "; Validate (xml, xsd); } [Test] public void Bug496192_496205 () { using (var xmlr = new StreamReader ("Test/XmlFiles/496192.xml")) using (var xsdr = new StreamReader ("Test/XmlFiles/496192.xsd")) Validate (xmlr.ReadToEnd (), xsdr.ReadToEnd ()); } [Test] public void Bug501666 () { string xsd = @" "; string xml = @""; XmlDocument doc = new XmlDocument (); doc.LoadXml (xml); doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null)); doc.Validate (null); } public void Bug502251 () { string xsd = @" "; XmlDocument doc = new XmlDocument (); doc.LoadXml (@" "); XmlSchema schema = XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null); doc.Schemas.Add (schema); doc.Validate (null); } [Test] public void Bug557452 () { string xsd = @" "; string xml = @""; XmlDocument doc = new XmlDocument (); doc.LoadXml (xml); doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null)); doc.Validate (null); } [Test] public void Bug584664 () { Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664a.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664a.xsd")); Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664b.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664b.xsd")); } [Test] public void MultipleMissingIds () { var schema = XmlSchema.Read (new StringReader (@" "), null); var xml = @" "; var document = new XmlDocument (); document.LoadXml (xml); document.Schemas = new XmlSchemaSet (); document.Schemas.Add (schema); document.Validate (null); } [Test] public void FacetsOnBaseSimpleContentRestriction () { XmlReaderSettings settings = new XmlReaderSettings (); settings.Schemas.Add (null, "Test/XmlFiles/595947.xsd"); settings.ValidationType = ValidationType.Schema; settings.Schemas.Compile (); Validate ("TEST 1.1", 1, "0123456789", "0123456789", settings, false); Validate ("TEST 1.2", 1, "0123456789***", "0123456789", settings, true); Validate ("TEST 1.3", 1, "0123456789", "0123456789***", settings, true); Validate ("TEST 2.1", 2, "0123456789", "0123456789", settings, false); Validate ("TEST 2.2", 2, "0123456789***", "0123456789", settings, true); Validate ("TEST 2.3", 2, "0123456789", "0123456789***", settings, true); Validate ("TEST 3.1", 3, "0123456789", "0123456789", settings, false); Validate ("TEST 3.2", 3, "0123456789***", "0123456789", settings, true); Validate ("TEST 3.3", 3, "0123456789", "0123456789***", settings, true); } void Validate (string testName, int testNumber, string idValue, string elementValue, XmlReaderSettings settings, bool shouldFail) { string content = string.Format ("{2}", testNumber, idValue, elementValue); try { XmlReader reader = XmlReader.Create (new StringReader (content), settings); XmlDocument document = new XmlDocument (); document.Load (reader); document.Validate (null); } catch (Exception e) { if (!shouldFail) throw; return; } if (shouldFail) Assert.Fail (testName + " should fail"); } [Test] public void Bug676993 () { Validate (File.ReadAllText ("Test/XmlFiles/676993.xml"), File.ReadAllText ("Test/XmlFiles/676993.xsd")); } [Test] public void Bug10245 () { string xsd = @" "; string xml = ""; var xrs = new XmlReaderSettings () { ValidationType = ValidationType.Schema }; xrs.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null)); var xr = XmlReader.Create (new StringReader (xml), xrs); xr.Read (); bool more; Assert.AreEqual (2, xr.AttributeCount, "#1"); int i = 0; for (more = xr.MoveToFirstAttribute (); more; more = xr.MoveToNextAttribute ()) i++; Assert.AreEqual (2, i, "#2"); } [Test] public void Bug12035 () { string xml = @"false"; string xsd = @" "; var schema = XmlSchema.Read (new StringReader (xsd), null); var schemaSet = new XmlSchemaSet (); schemaSet.Add (schema); var xmlReaderSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema }; xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; xmlReaderSettings.Schemas.Add (schemaSet); using (var configStream = new StringReader (xml)) { using (var validatingReader = XmlReader.Create (configStream, xmlReaderSettings)) { // Read the XML, throwing an exception if a validation error occurs while (validatingReader.Read()) { } } } } [Test] public void IgnoresInvalidBaseUri () { var source = new StringReader (@""); var readerSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema }; var reader = XmlReader.Create (source, readerSettings, "invalidBaseUri"); Assert.IsNotNull (reader); } } }