]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-identity.lisp
test: register seven negative XTM originals (slice 21C)
[topic-maps.git] / tests / xtm-identity.lisp
1 (defpackage #:topic-maps.xtm.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.xtm #:deserialize-octets #:xtm-rejection #:xtm-unsupported
4 #:xtm-xml-error #:error-code #:error-document-iri #:error-source-key #:error-line #:error-column)
5 (:export #:run-tests #:write-report))
6 (in-package #:topic-maps.xtm.tests)
7
8 (defparameter *document* "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm")
9 (defvar *checks* nil)
10 (defvar *cases* nil)
11 (defvar *outputs* nil)
12 (defvar *fixture* nil)
13 (defvar *negative-outputs* nil)
14 (defvar *negative-controls* nil)
15 (defun check (name truth)
16 (push name *checks*)
17 (unless truth (error "XTM test failed: ~A" name))
18 (format t "XTM-CHECK-PASS ~A~%" name))
19 (defun bytes (text &optional (encoding :utf-8) bom)
20 (concatenate '(vector (unsigned-byte 8))
21 (if bom (ecase encoding (:utf-8 #(239 187 191)) (:utf-16le #(255 254)) (:utf-16be #(254 255))) #())
22 (babel:string-to-octets text :encoding encoding :use-bom nil)))
23 (defun file-bytes (path)
24 (with-open-file (in path :element-type '(unsigned-byte 8))
25 (let ((data (make-array (file-length in) :element-type '(unsigned-byte 8))))
26 (assert (= (read-sequence data in) (length data))) data)))
27 (defun corpus-path (relative) (merge-pathnames relative (topic-maps.test-foundation:default-corpus-root)))
28 (defun document (body) (format nil "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'>~A</topicMap>" body))
29 (defun read-body (body &optional (iri *document*))
30 (deserialize-octets (bytes (document body)) :document-iri iri :source-key "local:xtm-semantic-test"))
31 (defun by-id (map name)
32 (find-if (lambda (topic) (member (concatenate 'string *document* "#" name)
33 (topic-item-identifiers topic) :test #'string=)) (topic-map-topics map)))
34 (defun by-si (map iri) (find iri (topic-map-topics map) :test (lambda (iri topic) (member iri (topic-subject-identifiers topic) :test #'string=))))
35 (defun core (map name) (by-si map (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
36 (defun condition-from (thunk) (handler-case (progn (funcall thunk) nil) (error (c) c)))
37 (defun rejection (name source class code)
38 (let ((c (condition-from (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key name)))))
39 (check (concatenate 'string name ":class") (typep c class))
40 (check (concatenate 'string name ":code") (eq code (error-code c)))
41 (check (concatenate 'string name ":context") (and (equal (error-document-iri c) *document*) (equal (error-source-key c) name)))
42 (check (concatenate 'string name ":position") (and (integerp (error-line c)) (plusp (error-line c))
43 (integerp (error-column c)) (not (minusp (error-column c)))))))
44 (defun public-state (map)
45 ;; Actual references are retained in the snapshot, not only equivalent XML.
46 (list (item-identifiers map)
47 (mapcar (lambda (topic) (list topic (topic-parent topic) (topic-item-identifiers topic)
48 (topic-subject-identifiers topic) (topic-subject-locators topic)
49 (topic-names topic) (topic-occurrences topic) (topic-roles-played topic))) (topic-map-topics map))
50 (mapcar (lambda (a) (list a (association-parent a) (association-type a) (association-scope a) (item-identifiers a)
51 (mapcar (lambda (r) (list r (association-role-parent r) (association-role-type r)
52 (association-role-player r) (item-identifiers r))) (association-roles a))))
53 (topic-map-associations map))))
54 (defun graph-checks (label map owner type)
55 (let* ((a (first (topic-map-associations map))) (roles (association-roles a))
56 (instance (find (core map "instance") roles :key #'association-role-type :test #'eq))
57 (type-role (find (core map "type") roles :key #'association-role-type :test #'eq)))
58 (check (concatenate 'string label ":counts") (and (= 5 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map))) (= 2 (length roles))))
59 (check (concatenate 'string label ":map-parent-eq") (and (eq map (association-parent a)) (every (lambda (topic) (eq map (topic-parent topic))) (topic-map-topics map))))
60 (check (concatenate 'string label ":association-type-eq") (eq (association-type a) (core map "type-instance")))
61 (check (concatenate 'string label ":role-parent-eq") (every (lambda (role) (eq a (association-role-parent role))) roles))
62 (check (concatenate 'string label ":players-eq") (and (eq owner (association-role-player instance)) (eq type (association-role-player type-role))))
63 (check (concatenate 'string label ":roles-played-eq") (and (member instance (topic-roles-played owner) :test #'eq) (member type-role (topic-roles-played type) :test #'eq)))
64 (check (concatenate 'string label ":canonical-handles") (and (eq a (canonical-association a)) (every (lambda (r) (eq r (canonical-association-role r))) roles)))))
65
66 ;; Slice 21A's twenty successful, previously unregistered originals. Keep this
67 ;; explicit: similar filenames and .sub helpers are not additional contracts.
68 (defparameter *additional-originals*
69 '("itemid-duplicate" "itemid-fragment" "itemid-relative" "itemid"
70 "merge-itemid-with-types" "merge-itemid" "merge-subjid" "merge-subjloc"
71 "subjid-duplicate" "subjid-escaping" "subjid-escaping2" "subjid-fragment"
72 "subjid-relative" "subjid-sameas-itemid" "subjid"
73 "subjloc-duplicate" "subjloc-fragment" "subjloc-multiple" "subjloc-relative" "subjloc"))
74
75 (defun references-current-p (map)
76 (let ((topics (topic-map-topics map)) (associations (topic-map-associations map)))
77 (flet ((live (topic) (and (member topic topics :test #'eq) (eq topic (canonical-topic topic)))))
78 (and
79 (every (lambda (topic) (and (live topic) (eq map (topic-parent topic))
80 (every (lambda (role) (and (eq topic (association-role-player role))
81 (member (association-role-parent role) associations :test #'eq)
82 (member role (association-roles (association-role-parent role)) :test #'eq)))
83 (topic-roles-played topic)))) topics)
84 (every (lambda (a) (and (eq a (canonical-association a)) (eq map (association-parent a))
85 (live (association-type a)) (every #'live (association-scope a))
86 (every (lambda (role) (and (eq role (canonical-association-role role))
87 (eq a (association-role-parent role)) (live (association-role-type role))
88 (live (association-role-player role))
89 (member role (topic-roles-played (association-role-player role)) :test #'eq)))
90 (association-roles a)))) associations)))))
91
92 (defun same-identifiers-p (a b)
93 (and (= (length a) (length b)) (null (set-exclusive-or a b :test #'equal))))
94
95 (defun additional-model-checks (name map)
96 (let* ((topic (by-id map "topic"))
97 (ii (list (concatenate 'string *document* "#topic"))) (si nil) (sl nil)
98 (directory (subseq *document* 0 (1+ (position #\/ *document* :from-end t))))
99 (shared "http://example.org/#topic"))
100 (check (concatenate 'string name ":declared-topic-live-eq")
101 (and topic (eq topic (canonical-topic topic)) (member topic (topic-map-topics map) :test #'eq)))
102 (cond
103 ((equal name "itemid-fragment") (push (concatenate 'string *document* "#topic2") ii))
104 ((equal name "itemid-relative") (push (concatenate 'string directory "more-tests/test.xtm#topic") ii))
105 ((equal name "itemid") (push shared ii))
106 ((member name '("merge-itemid" "merge-itemid-with-types" "merge-subjid" "merge-subjloc") :test #'equal)
107 (push (concatenate 'string *document* "#topic2") ii)
108 (cond ((member name '("merge-itemid" "merge-itemid-with-types") :test #'equal) (push shared ii))
109 ((equal name "merge-subjid") (push shared si))
110 (t (push shared sl)))
111 (check (concatenate 'string name ":merged-declarations-eq") (eq topic (by-id map "topic2"))))
112 ((member name '("subjid-duplicate" "subjid-relative") :test #'equal)
113 (push (concatenate 'string directory "photo.jpg") si))
114 ((equal name "subjid-escaping") (push "http://example.org/test+folder/#topic" si))
115 ((equal name "subjid-escaping2") (push "http://example.org/test folder/#topic" si))
116 ((equal name "subjid-fragment") (push (concatenate 'string *document* "#topic") si))
117 ((equal name "subjid-sameas-itemid")
118 (push "http://psi.example.org/test" ii) (push "http://psi.example.org/test" si))
119 ((equal name "subjid") (push shared si))
120 ((member name '("subjloc-duplicate" "subjloc-relative") :test #'equal)
121 (push (concatenate 'string directory "photo.jpg") sl))
122 ((equal name "subjloc-fragment") (push (concatenate 'string *document* "#topic") sl))
123 ((equal name "subjloc-multiple") (setf sl (list shared "http://example.org/#subject")))
124 ((equal name "subjloc") (push shared sl)))
125 (check (concatenate 'string name ":exact-ii-set") (same-identifiers-p ii (topic-item-identifiers topic)))
126 (check (concatenate 'string name ":exact-si-set") (same-identifiers-p si (topic-subject-identifiers topic)))
127 (check (concatenate 'string name ":exact-sl-set") (same-identifiers-p sl (topic-subject-locators topic)))
128 (check (concatenate 'string name ":no-unrequested-statements-or-reification")
129 (and (null (item-identifiers map)) (null (item-reifier map))
130 (every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic))
131 (null (topic-reified topic)))) (topic-map-topics map))))
132 (when (equal name "merge-itemid-with-types")
133 (dolist (type-id '("topictype1" "topictype2"))
134 (let* ((type (by-id map type-id))
135 (role (find type (loop for a in (topic-map-associations map) append (association-roles a))
136 :key #'association-role-player :test #'eq))
137 (a (and role (association-role-parent role))))
138 (check (concatenate 'string name ":forward-reference:" type-id)
139 (and type role (eq type (canonical-topic type))
140 (equal (list (concatenate 'string *document* "#" type-id)) (topic-item-identifiers type))
141 (eq (association-role-type role) (core map "type"))
142 (eq (association-type a) (core map "type-instance"))
143 (= 2 (length (association-roles a)))
144 (find-if (lambda (r) (and (eq topic (association-role-player r))
145 (eq (core map "instance") (association-role-type r)))) (association-roles a))))))
146 (dolist (core-id '("type" "instance" "type-instance"))
147 (check (concatenate 'string name ":core-no-extra-ii:" core-id)
148 (null (topic-item-identifiers (core map core-id))))))))
149
150 (defun original-condition-data (condition)
151 (when condition
152 (let ((code (cond ((typep condition 'topic-maps.xtm:xtm-error) (error-code condition))
153 ((typep condition 'item-identifier-error) (item-identifier-error-code condition)))))
154 (topic-maps.test-foundation::fields
155 "class" (string (type-of condition)) "code" (when code (string-downcase (string code)))
156 "message" (princ-to-string condition)))))
157
158 (defun record-original-result (case actual expected condition)
159 ;; No rejection (XML, XTM or TMDM) can satisfy a positive original contract.
160 (topic-maps.test-foundation:record-result case
161 (cond ((typep condition 'xtm-unsupported) :unsupported) (condition :execution-error)
162 ((equalp actual expected) :cxtm-match) (t :cxtm-mismatch))
163 :diagnostics (list (if condition (format nil "~A: ~A" (type-of condition) condition)
164 (format nil "Actual ~D bytes; original baseline ~D bytes." (length actual) (length expected))))))
165
166 (defun original-model-data (map)
167 (when map
168 (flet ((array (items) (coerce items 'vector))
169 (fields (&rest args) (apply #'topic-maps.test-foundation::fields args)))
170 (let ((topics (topic-map-topics map)))
171 (labels ((ref (topic) (1+ (position topic topics :test #'eq))))
172 (fields "topicCount" (length topics) "associationCount" (length (topic-map-associations map))
173 "canonicalParentsAndInverseReferences" (if (references-current-p map) :true :false)
174 "topics" (array (loop for topic in topics collect
175 (fields "ordinal" (ref topic) "itemIdentifiers" (array (topic-item-identifiers topic))
176 "subjectIdentifiers" (array (topic-subject-identifiers topic)) "subjectLocators" (array (topic-subject-locators topic)))))
177 "associations" (array (loop for a in (topic-map-associations map) collect
178 (fields "type" (ref (association-type a)) "roles" (array (loop for role in (association-roles a) collect
179 (fields "type" (ref (association-role-type role)) "player" (ref (association-role-player role))))))))))))))
180
181 (defun original-contracts ()
182 (setf *cases* nil *outputs* nil)
183 (dolist (name (append '("empty" "topic" "itemid-tm" "topic-type" "topic-type-duplicate" "merge-three-way") *additional-originals*))
184 (let* ((id (format nil "xtm2/in/~A.xtm" name))
185 (iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
186 (source (corpus-path id)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name)))
187 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source
188 :document-iri iri :expected-outcome :cxtm-equal :expected-baseline baseline
189 :clauses '("XTM 2.0 4.3-4.9,4.20" "TMDM 5.1-5.3,5.7-5.8,6,7.2" "CXTM 3.13,3.18,4")))
190 (raw (file-bytes source)) (raw-before (copy-seq raw))
191 (expected (file-bytes baseline)) (map nil) (actual nil) (condition nil) (before nil))
192 (push case *cases*)
193 (handler-case
194 (setf map (deserialize-octets raw :document-iri iri :source-key id)
195 before (public-state map)
196 actual (topic-maps.cxtm:canonicalize-octets map :base-locator iri))
197 (error (c) (setf condition c)))
198 (record-original-result case actual expected condition)
199 (push (list id actual expected (original-model-data map) (original-condition-data condition) (length raw)) *outputs*)
200 (when condition (error condition))
201 (check (concatenate 'string name ":original-byte-exact") (equalp actual expected))
202 (check (concatenate 'string name ":pure") (equal before (public-state map)))
203 (check (concatenate 'string name ":deterministic") (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator iri)))
204 (check (concatenate 'string name ":repeat-pure") (equal before (public-state map)))
205 (check (concatenate 'string name ":topic-count") (= (length (topic-map-topics map))
206 (cond ((member name '("empty" "itemid-tm") :test #'equal) 0)
207 ((member name '("topic-type" "topic-type-duplicate") :test #'equal) 5)
208 ((equal name "merge-itemid-with-types") 6) (t 1))))
209 (check (concatenate 'string name ":association-count") (= (length (topic-map-associations map))
210 (cond ((member name '("topic-type" "topic-type-duplicate") :test #'equal) 1)
211 ((equal name "merge-itemid-with-types") 2) (t 0))))
212 (check (concatenate 'string name ":canonical-parent-and-inverse-eq") (references-current-p map))
213 (check (concatenate 'string name ":source-octets-preserved") (equalp raw raw-before))
214 (when (member name *additional-originals* :test #'equal)
215 (let* ((*document* iri)
216 (other (deserialize-octets raw :document-iri iri :source-key "different-provenance-only")))
217 (additional-model-checks name map)
218 (check (concatenate 'string name ":source-key-only-provenance")
219 (and (equalp (original-model-data map) (original-model-data other))
220 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator iri))))))
221 (when (equal name "topic")
222 (check "topic:actual-item-locator" (equal (topic-item-identifiers (first (topic-map-topics map))) (list (concatenate 'string iri "#topic")))))
223 (when (equal name "itemid-tm")
224 (check "map:actual-item-locator" (equal (item-identifiers map) (list (concatenate 'string iri "#the-topic-map")))))
225 (when (equal name "merge-three-way")
226 (check "merge:three-identifiers" (= 3 (length (topic-item-identifiers (first (topic-map-topics map)))))))
227 (when (member name '("topic-type" "topic-type-duplicate") :test #'equal)
228 (let ((*document* iri)) (graph-checks name map (by-id map "xtm") (by-id map "format"))))))
229 (setf *cases* (nreverse *cases*) *outputs* (nreverse *outputs*))
230 (check "originals:26-unique-case-records"
231 (and (= 26 (length *cases*))
232 (= 26 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
233
234 (defun result-classification-tests ()
235 (flet ((case-record () (topic-maps.test-foundation:make-test-case
236 :id "local:classification-control" :category :valid :expected-outcome :cxtm-equal)))
237 (let* ((case (case-record)) (expected (third (first *outputs*))) (actual (copy-seq expected)))
238 (setf (aref actual 0) (logxor 1 (aref actual 0)))
239 (record-original-result case actual expected nil)
240 (check "classification:changed-actual-cxtm-is-fail"
241 (and (eq :fail (topic-maps.test-foundation:test-case-status case))
242 (eq :cxtm-mismatch (topic-maps.test-foundation:test-case-actual-outcome case)))))
243 (dolist (spec (list (list "xml" "<a><b></a>" 'xtm-xml-error :fail)
244 (list "xtm" (document "<topic id='2bad'/>") 'xtm-rejection :fail)
245 (list "tmdm" (document "<itemIdentity href='#a'/><topic id='a'/>") 'item-identifier-error :fail)
246 (list "unsupported" (document "<topic id='a'><name><value>N</value></name></topic>") 'xtm-unsupported :unsupported)))
247 (let ((case (case-record))
248 (c (condition-from (lambda () (deserialize-octets (bytes (second spec)) :document-iri *document*)))))
249 (record-original-result case nil #() c)
250 (check (concatenate 'string "classification:" (first spec) ":never-positive-pass")
251 (and (typep c (third spec)) (eq (fourth spec) (topic-maps.test-foundation:test-case-status case))
252 (not (eq :cxtm-match (topic-maps.test-foundation:test-case-actual-outcome case)))))))))
253
254 (defun semantic-tests ()
255 (dolist (body '("<topic id='a'><instanceOf><topicRef href='#b'/></instanceOf></topic><topic id='b'/>"
256 "<topic id='b'/><topic id='a'><instanceOf><topicRef href='#b'/></instanceOf></topic>"))
257 (let ((map (read-body body)))
258 (graph-checks (if (search "id='b'/>" body :start2 0 :end2 (min 20 (length body))) "backward" "forward") map (by-id map "a") (by-id map "b"))))
259 (let* ((map (read-body "<topic id='type'><subjectIdentifier href='#si'/></topic><topic id='a'><instanceOf><topicRef href='#si'/></instanceOf></topic>"))
260 (type (by-id map "type")))
261 (graph-checks "si-hit" map (by-id map "a") type)
262 (check "si-hit:no-extra-ii" (equal (topic-item-identifiers type) (list (concatenate 'string *document* "#type")))))
263 (let* ((map (read-body "<topic id='a'><instanceOf><topicRef href='#si'/></instanceOf></topic><topic id='type'><subjectIdentifier href='#si'/></topic>"))
264 (type (by-id map "type")))
265 (graph-checks "forward-si" map (by-id map "a") type)
266 (check "forward-si:created-ii-retained" (= 2 (length (topic-item-identifiers type)))))
267 (let* ((map (read-body "<topic id='type'><itemIdentity href='#alias'/></topic><topic id='a'><instanceOf><topicRef href='#alias'/><topicRef href='#type'/></instanceOf></topic>"))
268 (type (by-id map "type")))
269 (graph-checks "ii-hit" map (by-id map "a") type)
270 (check "ii-hit:identifiers-unchanged" (= 2 (length (topic-item-identifiers type)))))
271 (let* ((map (make-topic-map)) (si (intern-topic map :subject-identifier (concatenate 'string *document* "#s")))
272 (hit (topic-maps.xtm::find-reference-topic map (concatenate 'string *document* "#s"))))
273 (check "direct-si:actual-eq" (eq si hit))
274 (check "direct-si:no-ii" (null (topic-item-identifiers si))))
275 ;; Retain the exact object produced by a forward reference, then deserialize
276 ;; a later declaration with the same reader operation in the same context.
277 (let* ((map (make-topic-map))
278 (old (topic-maps.xtm::find-reference-topic map (concatenate 'string *document* "#future")))
279 (other (intern-topic map :subject-identifier "urn:shared"))
280 (a (make-association map (core-topic-for-test map "type-instance")
281 (list (list (core-topic-for-test map "instance") old) (list (core-topic-for-test map "type") other))))
282 (role (find old (association-roles a) :key #'association-role-player :test #'eq)))
283 (let* ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* "retained-forward")
284 (root (topic-maps.xml:parse-octets
285 (bytes (document "<topic id='future'><subjectIdentifier href='urn:shared'/></topic>"))
286 (make-instance 'topic-maps.xtm::source-handler))))
287 (topic-maps.xtm::validate-source root)
288 (topic-maps.xtm::read-topic (first (topic-maps.xtm::elements root)) map))
289 (check "retained:forward-handle-eq" (and (eq (canonical-topic old) (canonical-topic other)) (not (eq old (canonical-topic old)))))
290 (check "retained:role-player-canonical" (eq (association-role-player role) (canonical-topic old)))
291 (check "retained:role-parent-canonical" (eq (association-role-parent role) (canonical-association a)))
292 (let ((before (public-state map)) (pointer (topic-maps.tmdm::%topic-forward old)))
293 (topic-maps.cxtm:canonicalize map :base-locator *document*)
294 (check "retained:cxtm-does-not-mutate" (and (equal before (public-state map)) (eq pointer (topic-maps.tmdm::%topic-forward old))))))
295 (let ((a (read-body "<topic id='same'/>") ) (b (read-body "<topic id='same'/>" "https://other.example/map.xtm")))
296 (check "documents:distinct-object" (not (eq (first (topic-map-topics a)) (first (topic-map-topics b)))))
297 (check "documents:distinct-local-locators" (not (equal (topic-item-identifiers (first (topic-map-topics a))) (topic-item-identifiers (first (topic-map-topics b)))))))
298 (let ((map (read-body "<topic id='a'><subjectLocator href='resource'/><subjectIdentifier href='urn:subject'/><itemIdentity href='#alias'/></topic>")))
299 (check "identity:three-categories" (let ((topic (first (topic-map-topics map))))
300 (and (= 2 (length (topic-item-identifiers topic)))
301 (equal '("urn:subject") (topic-subject-identifiers topic))
302 (equal (list (concatenate 'string (subseq *document* 0 (1+ (position #\/ *document* :from-end t))) "resource")) (topic-subject-locators topic))))))
303 (let ((map (read-body "<topic id=' Ä水 '><subjectIdentifier href='http://example.org/%C3%A4/%F0%9F%A6%8A'/></topic>")))
304 (check "unicode:id-collapse" (by-id map "Ä水"))
305 (check "unicode:href-percent-utf8" (equal '("http://example.org/ä/🦊") (topic-subject-identifiers (first (topic-map-topics map))))))
306 (dolist (pair '(("../folder/./x#t" "https://example.test/folder/x#t")
307 ("#t" "https://example.test/a/map.xtm#t")
308 ("?q#t" "https://example.test/a/map.xtm?q#t")
309 ("http://EXAMPLE.test:80/a" "http://EXAMPLE.test:80/a")
310 ("http://example.test/a%20b#t" "http://example.test/a b#t")))
311 (let ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* "iri-test"))
312 (check (concatenate 'string "iri:" (first pair))
313 (equal (topic-maps.xtm::resolve-iri (first pair) "https://example.test/a/map.xtm" nil) (second pair)))))
314 (let* ((source (document "<topic id='a'/><topic id='b'><instanceOf><topicRef href='#a'/></instanceOf></topic>"))
315 (one (read-body "<topic id='a'/><topic id='b'><instanceOf><topicRef href='#a'/></instanceOf></topic>"))
316 (two (read-body "<topic id='b'><instanceOf><topicRef href='#a'/></instanceOf></topic><topic id='a'/>")))
317 (check "order:canonical-independence" (equal (topic-maps.cxtm:canonicalize one :base-locator *document*) (topic-maps.cxtm:canonicalize two :base-locator *document*)))
318 (dolist (spec '((:utf-8 t "UTF-8") (:utf-16le t "UTF-16") (:utf-16be t "UTF-16")
319 (:utf-16le nil "UTF-16LE") (:utf-16be nil "UTF-16BE")))
320 (destructuring-bind (encoding bom label) spec
321 (let* ((xml (format nil "<?xml version='1.0' encoding='~A'?>~A" label source))
322 (map (deserialize-octets (bytes xml encoding bom) :document-iri *document*)))
323 (check (format nil "encoding:~A:~A" encoding bom) (equal (topic-maps.cxtm:canonicalize one :base-locator *document*) (topic-maps.cxtm:canonicalize map :base-locator *document*))))))))
324 (defun core-topic-for-test (map name)
325 (intern-topic map :subject-identifier (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
326
327 (defun identity-cascade-tests ()
328 (let ((map (read-body "<topic id='a'><subjectIdentifier href='urn:one'/></topic><topic id='b'><subjectIdentifier href='urn:two'/></topic><topic id='c'><itemIdentity href='#a'/><subjectIdentifier href='urn:two'/></topic>")))
329 (check "transitive:mixed-identifiers-one-topic" (= 1 (length (topic-map-topics map))))
330 (check "transitive:three-ii-two-si" (let ((topic (first (topic-map-topics map))))
331 (and (= 3 (length (topic-item-identifiers topic))) (= 2 (length (topic-subject-identifiers topic)))))))
332 (let* ((map (read-body "<topic id='s'><subjectLocator href='#x'/></topic><topic id='a'><instanceOf><topicRef href='#x'/></instanceOf></topic>"))
333 (s (by-id map "s")) (x (by-id map "x")))
334 (check "reference:subject-locator-is-not-si-or-ii" (and s x (not (eq s x)) (= 6 (length (topic-map-topics map))))))
335 (let* ((map (make-topic-map)) (topic-maps.xtm::*document-iri* *document*)
336 (topic-maps.xtm::*source-key* "association-cascade")
337 (root (topic-maps.xml:parse-octets
338 (bytes (document "<topic id='a'><instanceOf><topicRef href='#t1'/><topicRef href='#t2'/></instanceOf></topic><topic id='t1'><itemIdentity href='#t2'/></topic>"))
339 (make-instance 'topic-maps.xtm::source-handler)))
340 (nodes (topic-maps.xtm::elements (topic-maps.xtm::validate-source root))))
341 (topic-maps.xtm::read-topic (first nodes) map)
342 (let* ((associations (topic-map-associations map))
343 (roles (loop for a in associations append (association-roles a)))
344 (old-type (by-id map "t2")))
345 (check "cascade:before-two-associations" (= 2 (length associations)))
346 (topic-maps.xtm::read-topic (second nodes) map)
347 (let ((a (first (topic-map-associations map))))
348 (check "cascade:after-one-association" (and (= 1 (length (topic-map-associations map))) (= 2 (length (association-roles a)))))
349 (check "cascade:retained-association-handles-eq" (every (lambda (old) (eq a (canonical-association old))) associations))
350 (check "cascade:retained-role-handles-eq" (every (lambda (r) (member (canonical-association-role r) (association-roles a) :test #'eq)) roles))
351 (check "cascade:stored-type-player-canonical" (every (lambda (r) (and (eq a (association-role-parent r))
352 (eq (association-role-type r) (canonical-topic (association-role-type r)))
353 (eq (association-role-player r) (canonical-topic (association-role-player r))))) (association-roles a)))
354 (check "cascade:retained-topic-handle-eq" (eq (canonical-topic old-type) (by-id map "t1")))
355 (let* ((handles (append associations roles (list old-type)))
356 (pointers (mapcar #'topic-maps.tmdm::canonical-item handles))
357 (before (public-state map)))
358 (topic-maps.cxtm:canonicalize map :base-locator *document*)
359 (check "cascade:all-handles-pure" (and (equal before (public-state map))
360 (every #'eq pointers (mapcar #'topic-maps.tmdm::canonical-item handles))))))))
361 (let* ((prefixed "<x:topicMap xmlns:x='http://www.topicmaps.org/xtm/' version='2.0'><x:topic id='a'/></x:topicMap>")
362 (source (bytes prefixed))
363 (a (deserialize-octets source :document-iri *document* :source-key "disk:a"))
364 (b (deserialize-octets source :document-iri *document* :source-key "memory:b")))
365 (check "namespace:prefix-equivalent" (by-id a "a"))
366 (check "source-key:not-topic-identity" (equal (topic-item-identifiers (by-id a "a")) (topic-item-identifiers (by-id b "a")))))
367 (let* ((map (read-body "<topic id='a'><instanceOf><topicRef href='https://external.example/map.xtm#unfetched'/></instanceOf></topic>"))
368 (type (find-if (lambda (topic) (member "https://external.example/map.xtm#unfetched" (topic-item-identifiers topic) :test #'equal)) (topic-map-topics map))))
369 (graph-checks "external-reference:no-retrieval" map (by-id map "a") type)))
370
371 (defun negative-tests ()
372 (dolist (spec '(("missing-id" "<topic/>" :missing-id)
373 ("invalid-id" "<topic id='2bad'/>" :invalid-id)
374 ("id-colon" "<topic id='a:b'/>" :invalid-id)
375 ("id-empty" "<topic id=''/>" :invalid-id)
376 ("id-spaces" "<topic id='a b'/>" :invalid-id)
377 ("duplicate-id" "<topic id='a'/><topic id=' a '/>" :duplicate-id)
378 ("unknown-attribute" "<topic id='a' other='v'/>" :unexpected-attribute)
379 ("qualified-attribute" "<topic xmlns:x='urn:x' x:id='a'/>" :unexpected-attribute)
380 ("unknown-element" "<topic id='a'><garbage/></topic>" :unexpected-element)
381 ("wrong-namespace-child" "<topic xmlns='urn:other' id='a'/>" :invalid-namespace)
382 ("topic-identity-order" "<topic id='a'><instanceOf><topicRef href='#b'/></instanceOf><itemIdentity href='#c'/></topic>" :element-order)
383 ("map-identity-order" "<topic id='a'/><itemIdentity href='#map'/>" :element-order)
384 ("instance-duplicate" "<topic id='a'><instanceOf><topicRef href='#b'/></instanceOf><instanceOf><topicRef href='#b'/></instanceOf></topic>" :duplicate-instance-of)
385 ("empty-instance" "<topic id='a'><instanceOf/></topic>" :empty-instance-of)
386 ("missing-href" "<topic id='a'><subjectIdentifier/></topic>" :missing-attribute)
387 ("nonempty-locator" "<topic id='a'><subjectIdentifier href='urn:s'><topic/></subjectIdentifier></topic>" :unexpected-child)
388 ("locator-text" "<topic id='a'><subjectIdentifier href='urn:s'>text</subjectIdentifier></topic>" :unexpected-text)
389 ("non-whitespace" "text" :unexpected-text)
390 ("no-ref-fragment" "<topic id='a'><instanceOf><topicRef href='other.xtm'/></instanceOf></topic>" :invalid-topic-ref)
391 ("bad-ref-fragment" "<topic id='a'><instanceOf><topicRef href='#2bad'/></instanceOf></topic>" :invalid-topic-ref)
392 ("empty-ref-fragment" "<topic id='a'><instanceOf><topicRef href='#'/></instanceOf></topic>" :invalid-topic-ref)
393 ("xpointer-ref" "<topic id='a'><instanceOf><topicRef href='#xpointer(a)'/></instanceOf></topic>" :invalid-topic-ref)
394 ("relative-colon" "<topic id='a'><subjectIdentifier href='1bad:subject'/></topic>" :invalid-iri)
395 ("decoded-control" "<topic id='a'><subjectIdentifier href='urn:%00'/></topic>" :invalid-iri)
396 ("bad-percent" "<topic id='a'><subjectIdentifier href='urn:%GG'/></topic>" :invalid-percent-escape)
397 ("bad-percent-utf8" "<topic id='a'><subjectIdentifier href='urn:%C3%28'/></topic>" :invalid-percent-utf8)
398 ("invalid-iri" "<topic id='a'><subjectIdentifier href='urn:a b'/></topic>" :invalid-iri)))
399 (rejection (first spec) (document (second spec)) 'xtm-rejection (third spec)))
400 (rejection "missing-version" "<topicMap xmlns='http://www.topicmaps.org/xtm/'/>" 'xtm-rejection :missing-attribute)
401 (rejection "version-21" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.1'/>" 'xtm-rejection :invalid-version)
402 (rejection "no-namespace" "<topicMap version='2.0'/>" 'xtm-rejection :invalid-namespace)
403 (rejection "namespace-10" "<topicMap xmlns='http://www.topicmaps.org/xtm/1.0/' version='2.0'/>" 'xtm-rejection :invalid-namespace)
404 (rejection "xml-misnested" "<a><b></a>" 'xtm-xml-error :xml-not-well-formed)
405 (rejection "xml-duplicate-attribute" "<topicMap version='2.0' version='2.0'/>" 'xtm-xml-error :xml-not-well-formed)
406 (rejection "xml-undeclared-prefix" "<x:topicMap/>" 'xtm-xml-error :xml-not-well-formed)
407 (dolist (body '("<!DOCTYPE topicMap>" "<!DOCTYPE topicMap SYSTEM 'file:///tmp/no-xtm-file'>"
408 "<!DOCTYPE topicMap SYSTEM 'http://127.0.0.1:9/no-xtm-network'>"
409 "<!DOCTYPE topicMap [<!ENTITY e SYSTEM 'file:///tmp/no-xtm-file'>]>"))
410 (rejection (concatenate 'string "doctype:" body) (concatenate 'string body (document "")) 'xtm-xml-error :doctype-forbidden))
411 (dolist (spec '(("unsupported-name" "<topic id='a'><name><value>Name</value></name></topic>" :unsupported-construct)
412 ("unsupported-occurrence" "<topic id='a'><occurrence><type><topicRef href='#t'/></type><resourceData>v</resourceData></occurrence></topic>" :unsupported-construct)
413 ("unsupported-association" "<association><type><topicRef href='#t'/></type><role><type><topicRef href='#r'/></type><topicRef href='#p'/></role></association>" :unsupported-construct)
414 ("unsupported-mergemap" "<mergeMap href='other.xtm'/>" :unsupported-construct)))
415 (rejection (first spec) (document (second spec)) 'xtm-unsupported (third spec)))
416 (rejection "unsupported-reifier" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0' reifier='#r'/>" 'xtm-unsupported :reification)
417 (let ((c (condition-from (lambda () (read-body (format nil "~%~%<topic id='2bad'/>"))))))
418 (check "diagnostic:multiline-observed-position" (and (typep c 'xtm-rejection) (= 3 (error-line c)) (plusp (error-column c)))))
419 (dolist (iri '(nil "relative.xtm" "https://example.test/a#fragment"))
420 (let ((c (condition-from (lambda () (deserialize-octets (bytes (document "")) :document-iri iri :source-key "document")))))
421 (check (format nil "document-iri:~S" iri) (and (typep c 'xtm-rejection) (eq (error-code c) :invalid-document-iri)))))
422 (check "api:raw-bytes-required" (typep (condition-from (lambda () (deserialize-octets (document "") :document-iri *document*))) 'type-error))
423 (let* ((source "<itemIdentity href='#a'/><topic id='a'/>")
424 (c (condition-from (lambda () (read-body source)))))
425 (check "tmdm:identifier-conflict-not-xml-or-xtm" (and (typep c 'item-identifier-error) (not (typep c 'topic-maps.xtm:xtm-error))))
426 (check "tmdm:identifier-conflict-specific" (eq (item-identifier-error-code c) :duplicate-item-identifier)))
427 (let ((c (condition-from (lambda () (read-body "<topic id='a'><subjectIdentifier href='http://psi.topicmaps.org/iso13250/model/type'/><subjectIdentifier href='http://psi.topicmaps.org/iso13250/model/instance'/></topic>")))))
428 (check "tmdm:core-subject-conflict-preserved" (and (typep c 'core-subject-error) (eq :core-subject-conflict (core-subject-error-code c)))))
429 (dolist (bad '(#(195 40) #(192 175) #(226 130) #(237 160 128)))
430 (let ((c (condition-from (lambda () (deserialize-octets
431 (concatenate '(vector (unsigned-byte 8)) (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><topic id='a'><subjectIdentifier href='urn:")
432 (coerce bad '(vector (unsigned-byte 8))) (bytes "'/></topic></topicMap>")) :document-iri *document*)))))
433 (check (format nil "actual-reader:invalid-utf8:~S" bad) (and (typep c 'xtm-xml-error) (eq :xml-not-well-formed (error-code c))))))
434 ;; Astral Unicode in an href exercises the qualified UTF-16 decoder through
435 ;; the real XTM reader. No pre-decoding strings are accepted by its API.
436 (dolist (encoding '(:utf-16le :utf-16be))
437 (let ((map (deserialize-octets (bytes (document "<topic id='a'><subjectIdentifier href='urn:🦊'/></topic>") encoding t) :document-iri *document*)))
438 (check (format nil "actual-reader:astral-surrogate:~A" encoding) (by-si map "urn:🦊"))))
439 (dolist (bad '(#(0 216) #(0 220) #(0 216 65 0)))
440 (let ((c (condition-from (lambda ()
441 (deserialize-octets (concatenate '(vector (unsigned-byte 8))
442 (bytes "<r>" :utf-16le t) (coerce bad '(vector (unsigned-byte 8))) (bytes "</r>" :utf-16le)) :document-iri *document*)))))
443 (check (format nil "actual-reader:invalid-surrogate:~S" bad) (typep c 'xtm-xml-error))))
444 ;; Verify the required entry point is actually called exactly once and sees
445 ;; the original octet vector by EQ, not a reconstructed decoded XML string.
446 (let* ((original (symbol-function 'topic-maps.xml:parse-octets))
447 (source (bytes (document "<topic id='a'/>"))) (calls 0) (same nil))
448 (unwind-protect
449 (progn (setf (symbol-function 'topic-maps.xml:parse-octets)
450 (lambda (bytes handler) (incf calls) (setf same (eq source bytes)) (funcall original bytes handler)))
451 (deserialize-octets source :document-iri *document*)
452 (check "boundary:real-parse-octets-only-once" (and (= 1 calls) same)))
453 (setf (symbol-function 'topic-maps.xml:parse-octets) original))))
454
455 (defun helper-source-test ()
456 (let* ((id "ctm/in/mergemap-xtm.sub")
457 (iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
458 (*document* iri) (map (deserialize-octets (file-bytes (corpus-path id)) :document-iri iri :source-key id)))
459 (graph-checks "direct-sub" map (by-id map "lars") (by-id map "person"))
460 (check "direct-sub:exact-lars-ii" (equal (list (concatenate 'string iri "#lars")) (topic-item-identifiers (by-id map "lars"))))
461 (check "direct-sub:exact-person-ii" (equal (list (concatenate 'string iri "#person")) (topic-item-identifiers (by-id map "person"))))
462 (check "direct-sub:no-names-occurrences" (every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic)))) (topic-map-topics map)))
463 (setf *fixture* map)))
464
465 (defun run-tests ()
466 (setf *checks* nil *fixture* nil *negative-outputs* nil *negative-controls* nil)
467 (handler-case
468 (progn (original-contracts) (negative-original-controls) (negative-original-contracts) (result-classification-tests)
469 (semantic-tests) (identity-cascade-tests) (negative-tests) (helper-source-test))
470 (error (condition)
471 ;; Preserve the actual failed outcome before ASDF propagates the failure.
472 (let ((report (uiop:getenv "TOPIC_MAPS_XTM_REPORT")))
473 (when report (write-report report)))
474 (error condition)))
475 (check "checks:unique-test-names" (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
476 (format t "XTM-SUMMARY ~D PASS 0 FAIL; ~D original CXTM PASS; ~D negative originals PASS; helper source PASS~%"
477 (length *checks*) (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)
478 (count :invalid *cases* :key #'topic-maps.test-foundation:test-case-category))
479 (length *checks*))
480
481 (defun write-report (pathname)
482 (let ((cases *cases*) (fixture *fixture*))
483 (flet ((fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
484 (array (items) (coerce items 'vector)))
485 (with-open-file (out pathname :direction :output :if-exists :supersede :external-format :utf-8)
486 (topic-maps.test-foundation::emit-json-value
487 (fields "scope" "XTM 2.0 identity subset, 26 positive and seven cause-qualified negative original contracts; no CTM Mergemap integration"
488 "corpusCommit" "cd165b150176c1d6842b37be9cc9051b40d4996b"
489 "statuses" (fields "pass" (count :pass cases :key #'topic-maps.test-foundation:test-case-status)
490 "fail" (count :fail cases :key #'topic-maps.test-foundation:test-case-status)
491 "unsupported" (count :unsupported cases :key #'topic-maps.test-foundation:test-case-status)
492 "not-run" (count :not-run cases :key #'topic-maps.test-foundation:test-case-status))
493 "otherCorpusInputs" (fields "positiveUnexecuted" (- (length (directory (corpus-path "xtm2/in/*.xtm"))) (count :valid cases :key #'topic-maps.test-foundation:test-case-category))
494 "negativeUnregisteredUnexecuted" (- (length (directory (corpus-path "xtm2/invalid/*.xtm")))
495 (count :invalid cases :key #'topic-maps.test-foundation:test-case-category)))
496 "checks" (array (reverse *checks*))
497 "cases" (array (loop for case in cases collect
498 (fields "id" (topic-maps.test-foundation:test-case-id case)
499 "documentIRI" (topic-maps.test-foundation:test-case-document-iri case)
500 "status" (string-downcase (symbol-name (topic-maps.test-foundation:test-case-status case)))
501 "actualOutcome" (string-downcase (symbol-name (topic-maps.test-foundation:test-case-actual-outcome case)))
502 "diagnostics" (array (topic-maps.test-foundation:test-case-diagnostics case)))))
503 "helperSource" (fields "id" "ctm/in/mergemap-xtm.sub" "status" (if fixture "pass" "not-run") "topics" (when fixture (length (topic-map-topics fixture)))
504 "associations" (when fixture (length (topic-map-associations fixture))) "roles" 2
505 "ctmMergemapContract" "NOT-RUN; direct helper deserialization only")
506 "negativeOriginals" (array *negative-outputs*)
507 "negativeControls" (array *negative-controls*)
508 "actualAndExpected" (array (loop for (id actual expected model condition source-length) in *outputs* collect
509 (fields "id" id "actual" (when actual (babel:octets-to-string actual :encoding :utf-8))
510 "expected" (babel:octets-to-string expected :encoding :utf-8) "byteLength" (when actual (length actual))
511 "expectedByteLength" (length expected) "sourceByteLength" source-length
512 "firstDifferentByte" (when actual (mismatch actual expected))
513 "model" model "condition" condition)))) out)
514 (terpri out)))))