XSD 1.0 Datatypes Appendix F defines SingleCharNoEsc as [^\[\]] and admits a nested class only as a -[...] subtraction, so a `[` inside a character class that is not introduced by `-` is a syntax error. RegexParser treated such a `[` as a literal scalar, so `[a[:xyz:]` compiled and the schema declaring it was wrongly accepted (XSTS RegexTest_993, RegexTest_1477). nextClassMember now throws a dedicated RegexError.unescapedClassBracket, kept distinct from the generic, deliberately-lenient badClass so the change cannot regress a valid-but-unsupported pattern. XSDSimpleParser.rejectsPattern treats it as a genuine syntax error, so the declaring schema is invalid. Closes RegexTest_993 and RegexTest_1477: invalid-schemas-accepted 15 -> 13, valid-schemas-rejected held at 0, instance buckets unchanged (full XSTS archive). Escaped [a\[c] and subtraction [a-z-[aeiou]] stay valid; the unterminated case [a[:xyz: now faults on the inner [.
67 lines
2.5 KiB
Swift
67 lines
2.5 KiB
Swift
import Testing
|
|
@testable import PureXML
|
|
|
|
/// A `pattern` facet value must be a valid XSD regular expression. The pattern
|
|
/// was only compiled lazily at instance time (`try?`), so an unparseable one was
|
|
/// silently ignored and the schema accepted. Compile-time validation rejects the
|
|
/// unambiguous structural errors (unbalanced parentheses, a quantifier with
|
|
/// nothing to repeat) while tolerating constructs the engine merely does not
|
|
/// support on an otherwise-valid pattern (an untabulated `\p{Is...}` block, the
|
|
/// empty pattern, the lenient `{,m}` quantifier), so no valid schema is rejected.
|
|
@Suite("Pattern facet regex validity")
|
|
struct SchemaPatternFacetTests {
|
|
private func compile(_ pattern: String) throws {
|
|
_ = try PureXML.Schema.Document("""
|
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
|
<xs:element name="v">
|
|
<xs:simpleType>
|
|
<xs:restriction base="xs:string"><xs:pattern value="\(pattern)"/></xs:restriction>
|
|
</xs:simpleType>
|
|
</xs:element>
|
|
</xs:schema>
|
|
""")
|
|
}
|
|
|
|
private func rejects(_ pattern: String) -> Bool {
|
|
do { try compile(pattern)
|
|
return false
|
|
} catch { return true }
|
|
}
|
|
|
|
@Test("a structurally malformed pattern is rejected")
|
|
func test_malformed() {
|
|
#expect(rejects("((a)"))
|
|
#expect(rejects("(a))"))
|
|
#expect(rejects("?a"))
|
|
#expect(rejects("*a"))
|
|
#expect(rejects("+a"))
|
|
}
|
|
|
|
@Test("a valid pattern compiles")
|
|
func test_valid() throws {
|
|
try compile("[a-z]+")
|
|
try compile("[0-9]{2,4}")
|
|
try compile("(ab)*c?")
|
|
}
|
|
|
|
@Test("an unescaped '[' inside a character class is rejected (XSTS RegexTest_993/1477)")
|
|
func test_unescapedClassBracket() {
|
|
// `[a[:xyz:]` (XSTS RegexTest_993 and RegexTest_1477) has an unescaped
|
|
// inner `[`, invalid per XSD Appendix F, so the schema is invalid.
|
|
#expect(rejects("[a[:xyz:]"))
|
|
#expect(rejects("[a[bc]"))
|
|
// The escaped form and a `-[...]` subtraction stay valid patterns.
|
|
#expect(!rejects("[a\\[c]"))
|
|
#expect(!rejects("[a-z-[aeiou]]"))
|
|
}
|
|
|
|
@Test("a construct the engine does not support is not treated as a schema error")
|
|
func test_engineGapTolerated() throws {
|
|
// Empty pattern (valid: matches the empty string), an untabulated Unicode
|
|
// block escape, and the lenient `{,m}` quantifier are all accepted.
|
|
try compile("")
|
|
try compile(#"\p{IsHighSurrogates}"#)
|
|
try compile("[0-9]{,5}")
|
|
}
|
|
}
|