From 2223babdd49118787c675e04aff711f936a10b26 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Wed, 28 Apr 2021 01:15:04 -0700 Subject: [PATCH] Fix issue #51778 and add more test coverage for xs:restrictions. (#51779) * Fix issue #51778 and add more test coverage for xs:restrictions. * Fix issue #51778 * CR feedback --- .../Xml/Schema/SchemaCollectionCompiler.cs | 2 +- .../System/Xml/Schema/SchemaSetCompiler.cs | 2 +- .../XmlSchemaSet/TC_SchemaSet_Compile.cs | 129 ++++++++++++++++++ .../XmlSchemaSet/TC_SchemaSet_ProhibitDTD.cs | 20 ++- 4 files changed, 140 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs index 26f8fcb768846..9747de559c99b 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs @@ -1560,7 +1560,7 @@ private bool IsSequenceFromChoice(XmlSchemaSequence derivedSequence, XmlSchemaCh { decimal minOccurs, maxOccurs; CalculateSequenceRange(derivedSequence, out minOccurs, out maxOccurs); - if (!IsValidOccurrenceRangeRestriction(minOccurs, maxOccurs, baseChoice.MinOccurs, baseChoice.MaxOccurs) || derivedSequence.Items.Count > baseChoice.Items.Count) + if (!IsValidOccurrenceRangeRestriction(minOccurs, maxOccurs, baseChoice.MinOccurs, baseChoice.MaxOccurs)) { return false; } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs index 98b63e36e8748..6941f0c76d630 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs @@ -1954,7 +1954,7 @@ private bool IsSequenceFromChoice(XmlSchemaSequence derivedSequence, XmlSchemaCh { maxOccurs = derivedSequence.MaxOccurs * derivedSequence.Items.Count; } - if (!IsValidOccurrenceRangeRestriction(minOccurs, maxOccurs, baseChoice.MinOccurs, baseChoice.MaxOccurs) || derivedSequence.Items.Count > baseChoice.Items.Count) + if (!IsValidOccurrenceRangeRestriction(minOccurs, maxOccurs, baseChoice.MinOccurs, baseChoice.MaxOccurs)) { return false; } diff --git a/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_Compile.cs b/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_Compile.cs index e80a2357bf940..98dbac22ca4cf 100644 --- a/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_Compile.cs +++ b/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_Compile.cs @@ -646,6 +646,135 @@ public void LengthGtBaseLength_Throws() Assert.Contains("length", ex.Message); } + #region Complex Restricton tests + + [Fact] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] + public void SequenceRestrictsChoiceValid() + { + string schema = @" + + + + + + + + + + + + + + + + + + + + + + + +"; + var xr = XmlReader.Create(new StringReader(schema)); + var ss = new XmlSchemaSet(); + ss.Add("urn:gba:sqg", xr); + ss.Compile(); + } + + + [Fact] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] + public void SequenceRestrictsChoiceComplexButValid() + { + string schema = @" + + + + + + + + + + + + + + + + + + + + + + + + + + + +"; + var xr = XmlReader.Create(new StringReader(schema)); + var ss = new XmlSchemaSet(); + ss.Add("urn:gba:sqg", xr); + ss.Compile(); + } + + [Fact] + public void SequenceRestrictsChoiceInvalid() + { + // particle "f" in derrived type has no mapping to any particle in the base type. + string schema = @" + + + + + + + + + + + + + + + + + + + + + + + + + + + + +"; + var xr = XmlReader.Create(new StringReader(schema)); + var ss = new XmlSchemaSet(); + ss.Add("urn:gba:sqg", xr); + + Exception ex = Assert.Throws(() => ss.Compile()); + + Assert.Contains("Invalid particle derivation by restriction", ex.Message); + } + #endregion + #region FacetBaseFixed tests public static IEnumerable FacetBaseFixed_Throws_TestData { diff --git a/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_ProhibitDTD.cs b/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_ProhibitDTD.cs index 3584085afb91c..2d2cd2a5ed391 100644 --- a/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_ProhibitDTD.cs +++ b/src/libraries/System.Private.Xml/tests/XmlSchema/XmlSchemaSet/TC_SchemaSet_ProhibitDTD.cs @@ -204,18 +204,17 @@ public void v4() } [Theory] - [ActiveIssue("https://github.com/dotnet/runtime/issues/30107")] //[Variation(Desc = "v5.2- Test Default value of ProhibitDTD for Add(TextReader) for schema with DTD", Priority = 1, Params = new object[] { "bug356711_a.xsd", 0 })] [InlineData("bug356711_a.xsd", 0)] //[Variation(Desc = "v5.1- Test Default value of ProhibitDTD for Add(TextReader) with an xs:import for schema with DTD", Priority = 1, Params = new object[] { "bug356711.xsd", 0 })] - [InlineData("bug356711.xsd", 0)] - public void v5(object param0, object param1) + [InlineData("bug356711.xsd", 2)] + public void v5(string fileName, int expectedWarnings) { Initialize(); XmlSchemaSet xss = new XmlSchemaSet(); xss.XmlResolver = new XmlUrlResolver(); xss.ValidationEventHandler += ValidationCallback; - XmlSchema schema = XmlSchema.Read(new StreamReader(new FileStream(Path.Combine(TestData._Root, param0.ToString()), FileMode.Open, FileAccess.Read)), ValidationCallback); + XmlSchema schema = XmlSchema.Read(new StreamReader(new FileStream(Path.Combine(TestData._Root, fileName), FileMode.Open, FileAccess.Read)), ValidationCallback); #pragma warning disable 0618 schema.Compile(ValidationCallback, new XmlUrlResolver()); #pragma warning restore 0618 @@ -227,23 +226,22 @@ public void v5(object param0, object param1) { Assert.True(false); //expect a validation warning for unresolvable schema location } - CError.Compare(warningCount, (int)param1, "Warning Count mismatch"); + CError.Compare(warningCount, expectedWarnings, "Warning Count mismatch"); CError.Compare(errorCount, 0, "Error Count mismatch"); } [Theory] - [ActiveIssue("https://github.com/dotnet/runtime/issues/30107")] //[Variation(Desc = "v6.2- Test Default value of ProhibitDTD for Add(XmlTextReader) for schema with DTD", Priority = 1, Params = new object[] { "bug356711_a.xsd" })] - [InlineData("bug356711_a.xsd")] + [InlineData("bug356711_a.xsd", 0)] //[Variation(Desc = "v6.1- Test Default value of ProhibitDTD for Add(XmlTextReader) with an xs:import for schema with DTD", Priority = 1, Params = new object[] { "bug356711.xsd" })] - [InlineData("bug356711.xsd")] - public void v6(object param0) + [InlineData("bug356711.xsd", 1)] + public void v6(string fileName, int expectedWarnings) { Initialize(); XmlSchemaSet xss = new XmlSchemaSet(); xss.XmlResolver = new XmlUrlResolver(); xss.ValidationEventHandler += ValidationCallback; - var reader = new XmlTextReader(Path.Combine(TestData._Root, param0.ToString())); + var reader = new XmlTextReader(Path.Combine(TestData._Root, fileName)); reader.XmlResolver = new XmlUrlResolver(); XmlSchema schema = XmlSchema.Read(reader, ValidationCallback); #pragma warning disable 0618 @@ -253,7 +251,7 @@ public void v6(object param0) xss.Add(schema); // expect a validation warning for unresolvable schema location - CError.Compare(warningCount, 0, "Warning Count mismatch"); + CError.Compare(warningCount, expectedWarnings, "Warning Count mismatch"); CError.Compare(errorCount, 0, "Error Count mismatch"); }