Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit test to provoke exception using Sch_WhiteSpaceRestriction… #35013

Merged
merged 10 commits into from
May 11, 2020
Merged
6 changes: 3 additions & 3 deletions src/libraries/System.Private.Xml/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@
<value>Derived attribute's use has to be required if base attribute's use is required.</value>
</data>
<data name="Sch_AttributeRestrictionInvalidFromWildcard" xml:space="preserve">
<value>The {base type definition} must have an {attribute wildcard} and the {target namespace} of the R's {attribute declaration} must be valid with respect to that wildcard.</value>
<value>The base type definition must have an attribute wildcard and the target namespace of the attribute declaration in the 'redefine' must be valid with respect to that wildcard.</value>
</data>
<data name="Sch_NoDerivedAttribute" xml:space="preserve">
<value>The base attribute '{0}' whose use = 'required' does not have a corresponding derived attribute while redefining attribute group '{1}'.</value>
Expand Down Expand Up @@ -1771,10 +1771,10 @@
<value>Values that are declared with fixed='true' in a base type can not be changed in a derived type.</value>
</data>
<data name="Sch_WhiteSpaceRestriction1" xml:space="preserve">
<value>It is an error if 'whiteSpace' is among the members of {facets} of {base type definition}, {value} is 'replace' or 'preserve', and the {value} of the parent 'whiteSpace' is 'collapse'.</value>
<value>It is an error if 'whiteSpace' is among the facets of the type definition, its value is 'replace' or 'preserve', and the value of the parent 'whiteSpace' is 'collapse'.</value>
</data>
<data name="Sch_WhiteSpaceRestriction2" xml:space="preserve">
<value>It is an error if 'whiteSpace' is among the members of {facets} of {base type definition}, {value} is 'preserve', and the {value} of the parent 'whiteSpace' is 'replace'.</value>
<value>It is an error if 'whiteSpace' is among the facets of the type definition, its value is 'preserve', and the value of the parent 'whiteSpace' is 'replace'.</value>
</data>
<data name="Sch_XsiNilAndFixed" xml:space="preserve">
<value>There must be no fixed value when an attribute is 'xsi:nil' and has a value of 'true'.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Text;

namespace System.Xml.Tests
{
Expand Down Expand Up @@ -1123,5 +1124,219 @@ public void TotalDigitsParseValue_Succeeds()
ss.Compile();
}
}

#region tests causing XmlSchemaException with Sch_WhiteSpaceRestriction1
public static IEnumerable<object[]> WhiteSpaceRestriction1_Throws_TestData
{
get
{
return new List<object[]>()
{
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:simpleType name='baseType'>
<xs:restriction base='xs:string'>
<xs:whiteSpace value='collapse'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name='restrictedType'>
<xs:restriction base='baseType'>
<xs:whiteSpace value='preserve'/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
"
},
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:simpleType name='baseType'>
<xs:restriction base='xs:string'>
<xs:whiteSpace value='collapse'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name='restrictedType'>
<xs:restriction base='baseType'>
<xs:whiteSpace value='replace'/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
"
}
};
}
}

[Theory]
[MemberData(nameof(WhiteSpaceRestriction1_Throws_TestData))]
public void WhiteSpaceRestriction1_Throws(string schema)
{
XmlSchemaSet ss = new XmlSchemaSet();
using (StringReader sr = new StringReader(schema))
using (XmlReader xmlrdr = XmlReader.Create(sr))
{
ss.Add(null, xmlrdr);
}

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

Assert.Contains("whiteSpace", ex.Message);
Assert.Contains("collapse", ex.Message);
Assert.Contains("preserve", ex.Message);
Assert.Contains("replace", ex.Message);
}
#endregion

#region tests causing XmlSchemaException with Sch_WhiteSpaceRestriction2
public static IEnumerable<object[]> WhiteSpaceRestriction2_Throws_TestData
{
get
{
return new List<object[]>()
{
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:simpleType name='baseType'>
<xs:restriction base='xs:string'>
<xs:whiteSpace value='replace'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name='restrictedType'>
<xs:restriction base='baseType'>
<xs:whiteSpace value='preserve'/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
"
}
};
}
}

[Theory]
[MemberData(nameof(WhiteSpaceRestriction2_Throws_TestData))]
public void WhiteSpaceRestriction2_Throws(string schema)
{
XmlSchemaSet ss = new XmlSchemaSet();
using (StringReader sr = new StringReader(schema))
using (XmlReader xmlrdr = XmlReader.Create(sr))
{
ss.Add(null, xmlrdr);
}

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

Assert.Contains("whiteSpace", ex.Message);
Assert.DoesNotContain("collapse", ex.Message);
Assert.Contains("preserve", ex.Message);
Assert.Contains("replace", ex.Message);
}
#endregion

#region Attribute Restriction Invalid From WildCard tests

public static IEnumerable<object[]> AttributeRestrictionInvalidFromWildCard_Throws_TestData
{
get
{
return new List<object[]>()
{
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>

<xs:redefine schemaLocation='fake://0'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
<xs:attribute name='d' type='xs:integer'/>
</xs:attributeGroup>
</xs:redefine>
</xs:schema>
"
},
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='http://www.foo.bar'>

<xs:redefine schemaLocation='fake://1'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
<xs:attribute name='d' type='xs:integer' form='qualified'/>
</xs:attributeGroup>
</xs:redefine>
</xs:schema>
"
}
};
}
}

[Theory]
[MemberData(nameof(AttributeRestrictionInvalidFromWildCard_Throws_TestData))]
public void AttributeRestrictionInvalidFromWildCard_Throws(string schema)
{
XmlSchemaSet ss = new XmlSchemaSet();
ss.XmlResolver = new FakeXmlResolverAttributeRestriction();
using (StringReader sr = new StringReader(schema))
using (XmlReader xmlrdr = XmlReader.Create(sr))
{
ss.Add(null, xmlrdr);
}

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

Assert.Contains("wildcard", ex.Message);
Assert.Contains("redefine", ex.Message);
}

private class FakeXmlResolverAttributeRestriction : XmlResolver
{
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
int uriIndex = int.Parse(absoluteUri.Host);
string[] schema = { @"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
</xs:attributeGroup>
</xs:schema>
",
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
<xs:anyAttribute namespace='##local'/>
</xs:attributeGroup>
</xs:schema>
"
};

return new MemoryStream(Encoding.UTF8.GetBytes(schema[uriIndex]));
}
}
#endregion
}
}