1 (in-package #:topic-maps.xtm.tests)
3 (defparameter *iid-original-specs* '(("itemid-name" 1) ("name-duplicate-iid" 2)))
5 (defun iid-original-model-checks (spec map raw)
6 (let* ((label (concatenate 'string "iid-original:" (first spec)))
7 (owner (by-id map "topic")) (names (topic-names owner)) (name (first names))
8 (type (by-si map (default-name-type-identifier)))
9 (locator (concatenate 'string *document* "#the-name"))
10 (root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
11 (source-names (topic-maps.xtm::elements (first (topic-maps.xtm::elements root)))))
12 (check (concatenate 'string label ":original-source-relationships")
13 (and (= (second spec) (length source-names))
15 (let ((children (topic-maps.xtm::elements node)))
16 (and (equal "name" (topic-maps.xtm::node-name node)) (= 2 (length children))
17 (equal "itemIdentity" (topic-maps.xtm::node-name (first children)))
18 (equal "#the-name" (topic-maps.xtm::attr (first children) "href"))
19 (equal "value" (topic-maps.xtm::node-name (second children)))
20 (equal '("Topic") (topic-maps.xtm::node-children (second children)))))) source-names)))
21 (check (concatenate 'string label ":counts-parent-type-value-eq")
22 (and (= 2 (length (topic-map-topics map))) (null (topic-map-associations map))
23 (= 1 (length names)) (null (topic-names type))
24 (eq name (canonical-topic-name name)) (eq owner (topic-name-parent name))
25 (eq type (topic-name-type name)) (equal "Topic" (topic-name-value name)) (names-current-p map)))
26 (check (concatenate 'string label ":identifier-owned-by-name-only")
27 (and (equal (list locator) (item-identifiers name))
28 (equal (list (concatenate 'string *document* "#topic")) (topic-item-identifiers owner))
29 (null (topic-subject-identifiers owner)) (null (topic-subject-locators owner))
30 (null (item-identifiers type)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers type))
31 (null (topic-subject-locators type)) (null (item-identifiers map))
32 (eq name (find locator (topic-maps.tmdm::map-information-items map) :test
33 (lambda (iri item) (member iri (item-identifiers item) :test #'equal))))))
34 (check (concatenate 'string label ":scope-and-excluded-properties-empty")
35 (and (null (topic-name-scope name)) (null (topic-name-variants name)) (null (item-reifier name))))
36 (check (concatenate 'string label ":global-identifier-uniqueness")
37 (topic-maps.tmdm::validate-map-item-identifiers map))))
39 (defun iid-original-contracts ()
40 (setf *iid-outputs* nil)
41 (dolist (spec *iid-original-specs*)
42 (let* ((name (first spec)) (id (format nil "xtm2/in/~A.xtm" name))
43 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
44 (source (corpus-path id)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name)))
45 (raw (file-bytes source)) (copy (copy-seq raw)) (expected (file-bytes baseline))
46 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source
47 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
48 :clauses '("XTM 2.0 4.2,4.3.1,4.3.3,4.3.4,4.6,4.10" "TMDM 5.1,5.4,6.2,6.3" "CXTM 3,4")))
49 (map nil) (actual nil) (c nil) (before nil))
50 (setf *cases* (append *cases* (list case)))
51 (handler-case (setf map (deserialize-octets raw :document-iri *document* :source-key id)
52 before (name-state map)
53 actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
54 (error (condition) (setf c condition)))
55 ;; A reader or model failure can never satisfy this positive contract.
57 (handler-case (iid-original-model-checks spec map raw) (error (condition) (setf c condition))))
58 (record-original-result case actual expected c)
59 (push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id
60 "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
61 "sourceText" (babel:octets-to-string raw :encoding :utf-8)
62 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
63 "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
64 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
65 "firstDifferentByte" (when actual (mismatch actual expected))
66 "condition" (negative-condition-data c) "model" (when map (name-model-data map))
67 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *iid-outputs*)
69 (check (concatenate 'string "iid-original:" name ":byte-exact") (equalp actual expected))
70 (check (concatenate 'string "iid-original:" name ":source-preserved") (equalp raw copy))
71 (check (concatenate 'string "iid-original:" name ":pure") (equal before (name-state map)))
72 (check (concatenate 'string "iid-original:" name ":deterministic")
73 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)))
74 (check (concatenate 'string "iid-original:" name ":repeat-pure") (equal before (name-state map)))
75 (let ((other (deserialize-octets raw :document-iri *document* :source-key "different-name-provenance")))
76 (check (concatenate 'string "iid-original:" name ":source-key-only-provenance")
77 (and (equalp (name-model-data map) (name-model-data other))
78 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
79 (setf *iid-outputs* (nreverse *iid-outputs*))
80 (check "iid-originals:50-unique-case-records"
81 (and (= 50 (length *cases*)) (= 43 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
82 (= 50 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
84 ;; All counterprobes use the existing Name harness, native model and reports.
85 (defun iid-control (label body verify)
86 (let ((*name-controls* nil))
87 (name-control (concatenate 'string "iid:" label) body
88 (lambda (map) (funcall verify map)
89 (check (concatenate 'string "iid:" label ":global-identifier-uniqueness")
90 (topic-maps.tmdm::validate-map-item-identifiers map))))
91 (setf *iid-controls* (append *iid-controls* (reverse *name-controls*)))))
92 (defun iid-error-control (label body class code)
93 (let ((*name-controls* nil))
94 (name-error-control (concatenate 'string "iid:" label) body class code)
95 (setf *iid-controls* (append *iid-controls* (reverse *name-controls*)))))
96 (defun iid-conflict-control (label body class)
97 (let* ((source (document body)) (c (condition-from (lambda ()
98 (deserialize-octets (bytes source) :document-iri *document* :source-key label)))))
99 (check (concatenate 'string "iid-conflict:" label ":native-class-code")
100 (and (eq class (type-of c)) (eq :duplicate-item-identifier (item-identifier-error-code c))
101 (not (typep c 'xtm-rejection)) (not (typep c 'xtm-unsupported))))
102 (push (topic-maps.test-foundation::fields "label" label "kind" "reader-TMDM-conflict"
103 "sourceText" source "documentIRI" *document* "sourceKey" label
104 "condition" (negative-condition-data c)) *iid-controls*)))
106 (defun iid-positive-controls ()
107 (iid-control "no-identifier" "<topic id='a'><name><value>N</value></name></topic>"
108 (lambda (map) (check "iid:absent-is-empty" (null (item-identifiers (first (topic-names (by-id map "a"))))))))
109 (iid-control "one-owner"
110 "<topic id='a'><name><itemIdentity href='#n'/><value>N</value></name></topic>"
111 (lambda (map) (let* ((owner (by-id map "a")) (name (first (topic-names owner))))
112 (check "iid:actual-name-owner-eq"
113 (and (equal (list (concatenate 'string *document* "#n")) (item-identifiers name))
114 (eq owner (topic-name-parent name)) (eq name (canonical-topic-name name))
115 (equal (list (concatenate 'string *document* "#a")) (item-identifiers owner))
116 (null (item-identifiers (topic-name-type name))))))))
117 (let ((*document* "https://names.example.invalid/a/maps/example.xtm"))
118 (iid-control "multiple-duplicate-relative-absolute"
119 "<topic id='a'><name><itemIdentity href='#n'/><itemIdentity href='#n'/><itemIdentity href='../names#n'/><itemIdentity href='urn:name:absolute'/><itemIdentity href='#m%C3%A4'/><value>N</value></name></topic>"
120 (lambda (map) (check "iid:resolved-locator-set-document-base"
121 (same-identifiers-p (item-identifiers (first (topic-names (by-id map "a"))))
122 '("https://names.example.invalid/a/maps/example.xtm#n" "https://names.example.invalid/a/names#n" "urn:name:absolute" "https://names.example.invalid/a/maps/example.xtm#mä"))))))
123 (iid-control "empty-reference"
124 "<topic id='a'><name><itemIdentity href=''/><value>N</value></name></topic>"
125 (lambda (map) (check "iid:empty-anyURI-resolves-to-document"
126 (equal (list *document*) (item-identifiers (first (topic-names (by-id map "a"))))))))
127 (iid-control "non-topicRef-fragment"
128 "<topic id='a'><name><itemIdentity href='#2valid-item-identifier'/><value>N</value></name></topic>"
129 (lambda (map) (check "iid:no-topicRef-shorthand-restriction"
130 (equal (list (concatenate 'string *document* "#2valid-item-identifier"))
131 (item-identifiers (first (topic-names (by-id map "a"))))))))
132 (iid-control "equal-names-union-identifiers"
133 "<topic id='a'><name><itemIdentity href='urn:first'/><scope><topicRef href='#s'/><topicRef href='#t'/></scope><value>N</value></name><name><itemIdentity href='urn:second'/><itemIdentity href='urn:first'/><scope><topicRef href='#t'/><topicRef href='#s'/></scope><value>N</value></name></topic>"
134 (lambda (map) (let ((names (topic-names (by-id map "a"))))
135 (check "iid:equal-native-name-union-and-scope-set"
136 (and (= 1 (length names)) (same-identifiers-p '("urn:first" "urn:second") (item-identifiers (first names)))
137 (scope-eq-p (topic-name-scope (first names)) (list (by-id map "s") (by-id map "t"))))))))
138 (dolist (row '(("different-value" "<value>N</value>" "<value>N </value>")
139 ("different-type" "<type><topicRef href='#one'/></type><value>N</value>" "<type><topicRef href='#two'/></type><value>N</value>")
140 ("different-scope" "<scope><topicRef href='#one'/></scope><value>N</value>" "<scope><topicRef href='#two'/></scope><value>N</value>")))
141 (iid-control (first row)
142 (format nil "<topic id='a'><name><itemIdentity href='urn:first'/>~A</name><name><itemIdentity href='urn:second'/>~A</name></topic>" (second row) (third row))
143 (lambda (map) (let ((names (topic-names (by-id map "a"))))
144 (check (concatenate 'string "iid:" (first row) ":distinct-owner-items")
145 (and (= 2 (length names)) (not (eq (first names) (second names)))
146 (equal '("urn:first") (item-identifiers (first names)))
147 (equal '("urn:second") (item-identifiers (second names)))))))))
148 (iid-control "different-parent"
149 "<topic id='a'><name><itemIdentity href='urn:first'/><value>N</value></name></topic><topic id='b'><name><itemIdentity href='urn:second'/><value>N</value></name></topic>"
150 (lambda (map) (check "iid:parent-distinguishes-items"
151 (let ((a (first (topic-names (by-id map "a")))) (b (first (topic-names (by-id map "b")))))
152 (and (not (eq a b)) (equal '("urn:first") (item-identifiers a)) (equal '("urn:second") (item-identifiers b)))))))
153 (iid-control "type-and-scope-and-item-identity"
154 "<topic id='a'><name><itemIdentity href='urn:n'/><type><topicRef href='#s'/></type><scope><topicRef href='#s'/><topicRef href='#s'/></scope><value>N</value></name></topic>"
155 (lambda (map) (let ((name (first (topic-names (by-id map "a")))))
156 (check "iid:all-name-properties-canonical-eq"
157 (and (equal '("urn:n") (item-identifiers name)) (eq (by-id map "a") (topic-name-parent name))
158 (eq (by-id map "s") (topic-name-type name)) (scope-eq-p (topic-name-scope name) (list (by-id map "s")))
159 (not (member "urn:n" (item-identifiers (by-id map "s")) :test #'equal)))))))
160 (iid-control "name-II-and-topic-SI-separate"
161 "<topic id='s'><subjectIdentifier href='urn:n'/></topic><topic id='a'><name><itemIdentity href='urn:n'/><value>N</value></name></topic>"
162 (lambda (map) (let ((name (first (topic-names (by-id map "a")))))
163 (check "iid:no-implicit-reification-or-topic-identity"
164 (and (equal '("urn:n") (item-identifiers name)) (null (item-reifier name))
165 (null (topic-reified (by-id map "s"))) (= 3 (length (topic-map-topics map)))))))))
167 (defun iid-retained-controls ()
168 (dolist (target '("parent" "theme"))
169 (dolist (kind '("subjectIdentifier" "subjectLocator" "itemIdentity"))
170 (let ((names nil) (themes nil) (*name-controls* nil) (label (format nil "iid-merge:~A:~A" target kind)))
171 (flet ((theme-source (id locator)
172 (if (equal kind "itemIdentity") (format nil "<topic id='~A'/>" id)
173 (format nil "<topic id='~A'><~A href='~A'/></topic>" id kind locator))))
174 (staged-name-control label
175 (if (equal target "parent")
176 (format nil "<topic id='a'>~A<name><itemIdentity href='urn:name:1'/><scope><topicRef href='#s'/></scope><value>N</value></name></topic><topic id='b'>~A<name><itemIdentity href='urn:name:2'/><scope><topicRef href='#s'/></scope><value>N</value></name></topic><topic id='bridge'><~A href='~A'/><~A href='~A'/></topic>"
177 (if (equal kind "itemIdentity") "" (format nil "<~A href='urn:one'/>" kind))
178 (if (equal kind "itemIdentity") "" (format nil "<~A href='urn:two'/>" kind))
179 kind (if (equal kind "itemIdentity") "#a" "urn:one") kind (if (equal kind "itemIdentity") "#b" "urn:two"))
180 (concatenate 'string "<topic id='a'><name><itemIdentity href='urn:name:1'/><scope><topicRef href='#s1'/></scope><value>N</value></name><name><itemIdentity href='urn:name:2'/><scope><topicRef href='#s2'/></scope><value>N</value></name></topic>"
181 (theme-source "s1" "urn:one") (theme-source "s2" "urn:two")
182 (format nil "<topic id='bridge'><~A href='~A'/><~A href='~A'/></topic>"
183 kind (if (equal kind "itemIdentity") "#s1" "urn:one") kind (if (equal kind "itemIdentity") "#s2" "urn:two"))))
185 (if (equal target "parent")
186 (when (<= step 2) (push (first (topic-names (by-id map (if (= step 1) "a" "b")))) names))
187 (when (= step 1) (setf names (topic-names (by-id map "a")) themes (list (by-id map "s1") (by-id map "s2")))))
188 (when (= step (if (equal target "parent") 2 3))
189 (check (concatenate 'string label ":distinct-before")
190 (and (= 2 (length names)) (not (eq (first names) (second names)))
191 (equal '("urn:name:1" "urn:name:2")
192 (sort (append (item-identifiers (first names)) (item-identifiers (second names))) #'string<))))))
194 (let* ((owner (by-id map "a")) (name (first (topic-names owner))) (before (name-state map)))
195 (check (concatenate 'string label ":canonical-handles-and-union")
196 (and (= 1 (length (topic-names owner))) (names-current-p map)
197 (every (lambda (old) (and (eq name (canonical-topic-name old)) (eq owner (topic-name-parent old))
198 (same-identifiers-p '("urn:name:1" "urn:name:2") (item-identifiers old)))) names)
199 (if (equal target "parent") (eq owner (by-id map "b"))
200 (and (every (lambda (old) (eq (by-id map "bridge") (canonical-topic old))) themes)
201 (scope-eq-p (topic-name-scope name) (list (by-id map "bridge")))))))
202 (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)
203 (check (concatenate 'string label ":pure-cxtm-retains-identifiers")
204 (and (equal before (name-state map))
205 (every (lambda (old) (eq name (canonical-topic-name old))) names)
206 (topic-maps.tmdm::validate-map-item-identifiers map)))))))
207 (setf *iid-controls* (append *iid-controls* (reverse *name-controls*)))))))
209 (defun iid-conflict-controls ()
210 (dolist (row '(("name-parent" "<topic id='a'><name><itemIdentity href='#a'/><value>N</value></name></topic>" item-identifier-error)
211 ("name-type" "<topic id='a'><name><itemIdentity href='#s'/><type><topicRef href='#s'/></type><value>N</value></name></topic>" item-identifier-error)
212 ("map-before-name" "<itemIdentity href='urn:n'/><topic id='a'><name><itemIdentity href='urn:n'/><value>N</value></name></topic>" item-identifier-error)
213 ("topic-after-name" "<topic id='a'><name><itemIdentity href='#b'/><value>N</value></name></topic><topic id='b'/>" topic-maps.tmdm::occurrence-identifier-error)
214 ("topic-II-after-name" "<topic id='a'><name><itemIdentity href='urn:n'/><value>N</value></name></topic><topic id='b'><itemIdentity href='urn:n'/></topic>" topic-maps.tmdm::occurrence-identifier-error)
215 ("different-value-same-II" "<topic id='a'><name><itemIdentity href='urn:n'/><value>N</value></name><name><itemIdentity href='urn:n'/><value>M</value></name></topic>" item-identifier-error)
216 ("different-type-same-II" "<topic id='a'><name><itemIdentity href='urn:n'/><type><topicRef href='#s'/></type><value>N</value></name><name><itemIdentity href='urn:n'/><value>N</value></name></topic>" item-identifier-error)
217 ("different-scope-same-II" "<topic id='a'><name><itemIdentity href='urn:n'/><scope><topicRef href='#s'/></scope><value>N</value></name><name><itemIdentity href='urn:n'/><value>N</value></name></topic>" item-identifier-error)
218 ("different-parent-same-II" "<topic id='a'><name><itemIdentity href='urn:n'/><value>N</value></name></topic><topic id='b'><name><itemIdentity href='urn:n'/><value>N</value></name></topic>" item-identifier-error)))
219 (iid-conflict-control (first row) (second row) (third row)))
220 ;; Direct native mutation controls use a genuinely deserialized map. They are
221 ;; not original contracts and prove that rejected writes do not overwrite IDs.
222 (let* ((body "<itemIdentity href='urn:map'/><topic id='a'><name><itemIdentity href='urn:name'/><value>N</value></name><name><itemIdentity href='urn:other'/><value>M</value></name></topic>")
223 (map (read-body body)) (owner (by-id map "a")) (name (first (topic-names owner))) (other (second (topic-names owner))))
224 (dolist (row (list (list "name-map" name "urn:map" 'item-identifier-error)
225 (list "map-name" map "urn:name" 'item-identifier-error)
226 (list "name-topic" name (concatenate 'string *document* "#a") 'item-identifier-error)
227 (list "topic-name" owner "urn:name" 'topic-maps.tmdm::occurrence-identifier-error)
228 (list "name-name" other "urn:name" 'item-identifier-error)))
229 (let* ((before (name-state map)) (c (condition-from (lambda () (add-item-identifier (second row) (third row))))))
230 (check (concatenate 'string "iid-native-conflict:" (first row) ":specific-and-atomic")
231 (and (eq (fourth row) (type-of c)) (eq :duplicate-item-identifier (item-identifier-error-code c))
232 (equal before (name-state map)) (eq name (canonical-topic-name name))
233 (eq other (canonical-topic-name other)) (topic-maps.tmdm::validate-map-item-identifiers map)))
234 (push (topic-maps.test-foundation::fields "label" (first row) "kind" "native-TMDM-conflict-atomicity"
235 "sourceText" (document body) "condition" (negative-condition-data c) "modelUnchanged" :true
236 "model" (name-model-data map)) *iid-controls*)))))
238 (defun iid-syntax-controls ()
239 (dolist (row '(("missing-href" "<itemIdentity/>" :missing-attribute)
240 ("bad-percent" "<itemIdentity href='%GG'/>" :invalid-percent-escape)
241 ("bad-percent-utf8" "<itemIdentity href='%FF'/>" :invalid-percent-utf8)
242 ("illegal-IRI-character" "<itemIdentity href='bad uri'/>" :invalid-iri)
243 ("unexpected-attribute" "<itemIdentity href='urn:n' other='x'/>" :unexpected-attribute)
244 ("namespaced-attribute" "<itemIdentity xmlns:q='urn:q' q:href='urn:n'/>" :unexpected-attribute)
245 ("nested-element" "<itemIdentity href='urn:n'><value>N</value></itemIdentity>" :unexpected-child)
246 ("non-whitespace-text" "<itemIdentity href='urn:n'>text</itemIdentity>" :unexpected-text)
247 ("wrong-namespace" "<itemIdentity xmlns='urn:foreign' href='urn:n'/>" :invalid-namespace)
248 ("after-type" "<type><topicRef href='#t'/></type><itemIdentity href='urn:n'/>" :element-order)
249 ("after-scope" "<scope><topicRef href='#s'/></scope><itemIdentity href='urn:n'/>" :element-order)))
250 (iid-error-control (first row) (format nil "<topic id='a'><name>~A<value>N</value></name></topic>" (second row)) 'xtm-rejection (third row)))
251 (iid-error-control "after-value" "<topic id='a'><name><value>N</value><itemIdentity href='urn:n'/></name></topic>" 'xtm-rejection :element-order)
252 (iid-error-control "variant-reifier-excluded-datatype" "<topic id='a'><name><itemIdentity href='urn:n'/><value>N</value><variant reifier='#r'><scope><topicRef href='#v'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>42</resourceData></variant></name></topic>" 'xtm-unsupported :resource-data-datatype)
253 (iid-error-control "variant-resourceData-datatype-stays-unsupported" "<topic id='a'><name><itemIdentity href='urn:n'/><scope><topicRef href='#s'/></scope><value>N</value><variant><scope><topicRef href='#v'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#anyType'>42</resourceData></variant></name></topic>" 'xtm-unsupported :resource-data-datatype))
256 (setf *iid-controls* nil)
257 (iid-positive-controls) (iid-retained-controls) (iid-conflict-controls) (iid-syntax-controls))