PureXML/Tests/SchemaGroupRedefineRestrictionTests.swift
Mihaela Mihaljevic af5d7882b0 fix(schema): enforce model-group redefine restriction (cos-group-restrict)
A non-self-referencing model group redefined inside xs:redefine must be a valid
restriction of the group it redefines (cos-group-restrict / src-redefine 6.1.2),
so its content model may not widen the original's language. The check reuses the
particle-restriction oracle used for complex-type restriction, resolves the
original group by namespaced identity with a {}name chameleon fallback, and runs
post-compile where element type compatibility is available.

Now rejected: adding required content to an empty base (schZ006), dropping a
required all member (schL1), reordering an all (schL5/L6, the Recurse rule is
order-preserving even for all), growing to a superset (schL8), and the
multi-document/chained redefines schN4/5/6/7 and schL10. To stay
false-positive-free the check skips a redefinition carrying a self-reference and
declines to judge a base with a maxOccurs=0 pointless particle (the contested
case W3C accepts, mgO013); genuine restrictions (dropping an optional member, an
unchanged redefinition) are accepted.

XSTS invalid-schemas-accepted 64 -> 51, no false positive (valid-schemas-rejected
and valid-instances-rejected held at 0; an over-rejection of mgO013 surfaced by
the gate was fixed before commit, and an adversarial critic confirmed FP-safety),
no other bucket moved. Disclosed under-rejections: self-referencing redefinitions
(schN10) and maxOccurs=0-base redefines.
2026-06-18 18:24:58 +02:00

78 lines
3.7 KiB
Swift

@testable import PureXML
import Testing
/// A non-self-referencing model group redefined inside `xs:redefine` must be a valid
/// restriction of the group it redefines (XSD 1.0 cos-group-restrict / src-redefine
/// 6.1.2). Adding content the original cannot produce, or dropping a required member
/// so the language widens, is rejected; a genuine restriction is accepted. The
/// order-preserving Recurse rule applies even to `xs:all`, so reordering an `all` is
/// rejected (W3C schL5), while a base with a `maxOccurs=0` member is left unjudged to
/// avoid the contested pointless-particle case (W3C mgO013).
@Suite("redefined model group restriction")
struct SchemaGroupRedefineRestrictionTests {
private func redefine(group body: String, base: String) throws -> PureXML.Schema.Document {
let main = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
+ "<xs:redefine schemaLocation='b.xsd'><xs:group name='g'>\(body)</xs:group></xs:redefine>"
+ "<xs:complexType name='t'><xs:group ref='g'/></xs:complexType>"
+ "<xs:element name='e' type='t'/></xs:schema>"
let baseDoc = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
+ "<xs:group name='g'>\(base)</xs:group>"
+ "<xs:complexType name='b'><xs:group ref='g'/></xs:complexType></xs:schema>"
return try PureXML.Schema.Document(main, schemaLoader: { _ in baseDoc })
}
@Test("adding required content to an empty base group is rejected")
func test_addRequiredToEmptyRejected() throws {
#expect(throws: (any Error).self) {
try redefine(
group: "<xs:sequence><xs:element name='a' type='xs:string'/></xs:sequence>",
base: "<xs:sequence/>",
)
}
}
@Test("dropping a required member from an all group is rejected")
func test_dropRequiredFromAllRejected() throws {
#expect(throws: (any Error).self) {
try redefine(
group: "<xs:all><xs:element name='a' type='xs:string'/></xs:all>",
base: "<xs:all><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:all>",
)
}
}
@Test("reordering an all group is rejected (order-preserving Recurse)")
func test_reorderAllRejected() throws {
#expect(throws: (any Error).self) {
try redefine(
group: "<xs:all><xs:element name='b' type='xs:string'/><xs:element name='a' type='xs:string'/></xs:all>",
base: "<xs:all><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:all>",
)
}
}
@Test("dropping an optional member is a valid restriction")
func test_dropOptionalAccepted() throws {
_ = try redefine(
group: "<xs:sequence><xs:element name='a' type='xs:string'/></xs:sequence>",
base: "<xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string' minOccurs='0'/></xs:sequence>",
)
}
@Test("re-enabling a maxOccurs=0 base member is left unjudged and accepted")
func test_pointlessParticleBaseAccepted() throws {
_ = try redefine(
group: "<xs:all><xs:element name='a' type='xs:string'/></xs:all>",
base: "<xs:all><xs:element name='a' type='xs:string' minOccurs='0' maxOccurs='0'/></xs:all>",
)
}
@Test("an unchanged group redefinition is a valid restriction")
func test_identityAccepted() throws {
_ = try redefine(
group: "<xs:sequence><xs:element name='a' type='xs:string'/></xs:sequence>",
base: "<xs:sequence><xs:element name='a' type='xs:string'/></xs:sequence>",
)
}
}