Skip to content

Commit

Permalink
Fix issue #51778 and add more test coverage for xs:restrictions. (#51779
Browse files Browse the repository at this point in the history
)

* Fix issue #51778 and add more test coverage for xs:restrictions.

* Fix issue #51778

* CR feedback
  • Loading branch information
lovettchris committed Apr 28, 2021
1 parent 41f3d48 commit 2223bab
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='urn:gba:sqg' xmlns:ns1='urn:gba:sqg'
elementFormDefault='qualified' attributeFormDefault='unqualified'>
<xs:complexType name='base' abstract='true'>
<xs:choice minOccurs='2' maxOccurs='unbounded'>
<xs:element name='a'/>
<xs:element name='b'/>
<xs:element name='c'/>
</xs:choice>
</xs:complexType>
<xs:complexType name='derived'>
<xs:complexContent>
<xs:restriction base='ns1:base'>
<xs:sequence>
<xs:element name='b'/>
<xs:element name='a'/>
<xs:element name='c'/>
<xs:element name='c'/>
<xs:element name='a'/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name='root' type='ns1:derived'>
</xs:element>
</xs: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 = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:ns1='urn:gba:sqg'
xmlns:xenc='http://www.w3.org/2001/04/xmlenc#'
xmlns:ds='http://www.w3.org/2000/09/xmldsig#'
xmlns:xi='urn:gba:sqg' targetNamespace='urn:gba:sqg'
elementFormDefault='qualified' attributeFormDefault='unqualified'>
<xs:complexType name='base' abstract='true'>
<xs:choice maxOccurs='unbounded'>
<xs:sequence>
<xs:element name='a' minOccurs='0'/>
<xs:element name='b' minOccurs='0'/>
</xs:sequence>
<xs:sequence>
<xs:element name='c' minOccurs='0'/>
<xs:element name='d' minOccurs='0'/>
<xs:element name='e' minOccurs='0'/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexType name='derived'>
<xs:complexContent>
<xs:restriction base='ns1:base'>
<xs:sequence>
<xs:element name='c'/>
<xs:element name='d' minOccurs='0'/>
<xs:element name='e' minOccurs='0'/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name='root' type='ns1:derived'>
</xs:element>
</xs: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 = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='urn:gba:sqg' xmlns:ns1='urn:gba:sqg'
elementFormDefault='qualified' attributeFormDefault='unqualified'>
<xs:complexType name='base' abstract='true'>
<xs:choice maxOccurs='unbounded'>
<xs:sequence>
<xs:element name='a' minOccurs='0'/>
<xs:element name='b' minOccurs='0'/>
</xs:sequence>
<xs:sequence>
<xs:element name='c' minOccurs='0'/>
<xs:element name='d' minOccurs='0'/>
<xs:element name='e' minOccurs='0'/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:complexType name='derived'>
<xs:complexContent>
<xs:restriction base='ns1:base'>
<xs:sequence>
<xs:element name='a'/>
<xs:element name='c' minOccurs='0'/>
<xs:element name='e' minOccurs='0'/>
<xs:element name='f' minOccurs='0'/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name='root' type='ns1:derived'>
</xs:element>
</xs:schema>
";
var xr = XmlReader.Create(new StringReader(schema));
var ss = new XmlSchemaSet();
ss.Add("urn:gba:sqg", xr);

Exception ex = Assert.Throws<XmlSchemaException>(() => ss.Compile());

Assert.Contains("Invalid particle derivation by restriction", ex.Message);
}
#endregion

#region FacetBaseFixed tests
public static IEnumerable<object[]> FacetBaseFixed_Throws_TestData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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");
}

Expand Down

0 comments on commit 2223bab

Please sign in to comment.