]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-identity.lisp
feat(ctm): validate prologs and register three Slice 20C contracts
[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 (defun check (name truth)
14 (push name *checks*)
15 (unless truth (error "XTM test failed: ~A" name))
16 (format t "XTM-CHECK-PASS ~A~%" name))
17 (defun bytes (text &optional (encoding :utf-8) bom)
18 (concatenate '(vector (unsigned-byte 8))
19 (if bom (ecase encoding (:utf-8 #(239 187 191)) (:utf-16le #(255 254)) (:utf-16be #(254 255))) #())
20 (babel:string-to-octets text :encoding encoding :use-bom nil)))
21 (defun file-bytes (path)
22 (with-open-file (in path :element-type '(unsigned-byte 8))
23 (let ((data (make-array (file-length in) :element-type '(unsigned-byte 8))))
24 (assert (= (read-sequence data in) (length data))) data)))
25 (defun corpus-path (relative) (merge-pathnames relative (topic-maps.test-foundation:default-corpus-root)))
26 (defun document (body) (format nil "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'>~A</topicMap>" body))
27 (defun read-body (body &optional (iri *document*))
28 (deserialize-octets (bytes (document body)) :document-iri iri :source-key "local:xtm-semantic-test"))
29 (defun by-id (map name)
30 (find-if (lambda (topic) (member (concatenate 'string *document* "#" name)
31 (topic-item-identifiers topic) :test #'string=)) (topic-map-topics map)))
32 (defun by-si (map iri) (find iri (topic-map-topics map) :test (lambda (iri topic) (member iri (topic-subject-identifiers topic) :test #'string=))))
33 (defun core (map name) (by-si map (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
34 (defun condition-from (thunk) (handler-case (progn (funcall thunk) nil) (error (c) c)))
35 (defun rejection (name source class code)
36 (let ((c (condition-from (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key name)))))
37 (check (concatenate 'string name ":class") (typep c class))
38 (check (concatenate 'string name ":code") (eq code (error-code c)))
39 (check (concatenate 'string name ":context") (and (equal (error-document-iri c) *document*) (equal (error-source-key c) name)))
40 (check (concatenate 'string name ":position") (and (integerp (error-line c)) (plusp (error-line c))
41 (integerp (error-column c)) (not (minusp (error-column c)))))))
42 (defun public-state (map)
43 ;; Actual references are retained in the snapshot, not only equivalent XML.
44 (list (item-identifiers map)
45 (mapcar (lambda (topic) (list topic (topic-parent topic) (topic-item-identifiers topic)
46 (topic-subject-identifiers topic) (topic-subject-locators topic)
47 (topic-names topic) (topic-occurrences topic) (topic-roles-played topic))) (topic-map-topics map))
48 (mapcar (lambda (a) (list a (association-parent a) (association-type a) (association-scope a) (item-identifiers a)
49 (mapcar (lambda (r) (list r (association-role-parent r) (association-role-type r)
50 (association-role-player r) (item-identifiers r))) (association-roles a))))
51 (topic-map-associations map))))
52 (defun graph-checks (label map owner type)
53 (let* ((a (first (topic-map-associations map))) (roles (association-roles a))
54 (instance (find (core map "instance") roles :key #'association-role-type :test #'eq))
55 (type-role (find (core map "type") roles :key #'association-role-type :test #'eq)))
56 (check (concatenate 'string label ":counts") (and (= 5 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map))) (= 2 (length roles))))
57 (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))))
58 (check (concatenate 'string label ":association-type-eq") (eq (association-type a) (core map "type-instance")))
59 (check (concatenate 'string label ":role-parent-eq") (every (lambda (role) (eq a (association-role-parent role))) roles))
60 (check (concatenate 'string label ":players-eq") (and (eq owner (association-role-player instance)) (eq type (association-role-player type-role))))
61 (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)))
62 (check (concatenate 'string label ":canonical-handles") (and (eq a (canonical-association a)) (every (lambda (r) (eq r (canonical-association-role r))) roles)))))
63
64 (defun original-contracts ()
65 (setf *cases* nil *outputs* nil)
66 (dolist (name '("empty" "topic" "itemid-tm" "topic-type" "topic-type-duplicate" "merge-three-way"))
67 (let* ((id (format nil "xtm2/in/~A.xtm" name))
68 (iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
69 (source (corpus-path id)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name)))
70 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname source
71 :document-iri iri :expected-outcome :cxtm-equal :expected-baseline baseline
72 :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")))
73 (map (deserialize-octets (file-bytes source) :document-iri iri :source-key id))
74 (before (public-state map))
75 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator iri))
76 (expected (file-bytes baseline)))
77 (topic-maps.test-foundation:record-result case (if (equalp actual expected) :cxtm-match :cxtm-mismatch)
78 :diagnostics (list (format nil "Actual ~D bytes; original baseline ~D bytes." (length actual) (length expected))))
79 (push case *cases*) (push (list id actual expected) *outputs*)
80 (check (concatenate 'string name ":original-byte-exact") (equalp actual expected))
81 (check (concatenate 'string name ":pure") (equal before (public-state map)))
82 (check (concatenate 'string name ":deterministic") (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator iri)))
83 (check (concatenate 'string name ":topic-count") (= (length (topic-map-topics map))
84 (cond ((member name '("empty" "itemid-tm") :test #'equal) 0) ((member name '("topic-type" "topic-type-duplicate") :test #'equal) 5) (t 1))))
85 (check (concatenate 'string name ":association-count") (= (length (topic-map-associations map))
86 (if (member name '("topic-type" "topic-type-duplicate") :test #'equal) 1 0)))
87 (when (equal name "topic")
88 (check "topic:actual-item-locator" (equal (topic-item-identifiers (first (topic-map-topics map))) (list (concatenate 'string iri "#topic")))))
89 (when (equal name "itemid-tm")
90 (check "map:actual-item-locator" (equal (item-identifiers map) (list (concatenate 'string iri "#the-topic-map")))))
91 (when (equal name "merge-three-way")
92 (check "merge:three-identifiers" (= 3 (length (topic-item-identifiers (first (topic-map-topics map)))))))
93 (when (member name '("topic-type" "topic-type-duplicate") :test #'equal)
94 (let ((*document* iri)) (graph-checks name map (by-id map "xtm") (by-id map "format"))))))
95 (setf *cases* (nreverse *cases*) *outputs* (nreverse *outputs*)))
96
97 (defun semantic-tests ()
98 (dolist (body '("<topic id='a'><instanceOf><topicRef href='#b'/></instanceOf></topic><topic id='b'/>"
99 "<topic id='b'/><topic id='a'><instanceOf><topicRef href='#b'/></instanceOf></topic>"))
100 (let ((map (read-body body)))
101 (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"))))
102 (let* ((map (read-body "<topic id='type'><subjectIdentifier href='#si'/></topic><topic id='a'><instanceOf><topicRef href='#si'/></instanceOf></topic>"))
103 (type (by-id map "type")))
104 (graph-checks "si-hit" map (by-id map "a") type)
105 (check "si-hit:no-extra-ii" (equal (topic-item-identifiers type) (list (concatenate 'string *document* "#type")))))
106 (let* ((map (read-body "<topic id='a'><instanceOf><topicRef href='#si'/></instanceOf></topic><topic id='type'><subjectIdentifier href='#si'/></topic>"))
107 (type (by-id map "type")))
108 (graph-checks "forward-si" map (by-id map "a") type)
109 (check "forward-si:created-ii-retained" (= 2 (length (topic-item-identifiers type)))))
110 (let* ((map (read-body "<topic id='type'><itemIdentity href='#alias'/></topic><topic id='a'><instanceOf><topicRef href='#alias'/><topicRef href='#type'/></instanceOf></topic>"))
111 (type (by-id map "type")))
112 (graph-checks "ii-hit" map (by-id map "a") type)
113 (check "ii-hit:identifiers-unchanged" (= 2 (length (topic-item-identifiers type)))))
114 (let* ((map (make-topic-map)) (si (intern-topic map :subject-identifier (concatenate 'string *document* "#s")))
115 (hit (topic-maps.xtm::find-reference-topic map (concatenate 'string *document* "#s"))))
116 (check "direct-si:actual-eq" (eq si hit))
117 (check "direct-si:no-ii" (null (topic-item-identifiers si))))
118 ;; Retain the exact object produced by a forward reference, then deserialize
119 ;; a later declaration with the same reader operation in the same context.
120 (let* ((map (make-topic-map))
121 (old (topic-maps.xtm::find-reference-topic map (concatenate 'string *document* "#future")))
122 (other (intern-topic map :subject-identifier "urn:shared"))
123 (a (make-association map (core-topic-for-test map "type-instance")
124 (list (list (core-topic-for-test map "instance") old) (list (core-topic-for-test map "type") other))))
125 (role (find old (association-roles a) :key #'association-role-player :test #'eq)))
126 (let* ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* "retained-forward")
127 (root (topic-maps.xml:parse-octets
128 (bytes (document "<topic id='future'><subjectIdentifier href='urn:shared'/></topic>"))
129 (make-instance 'topic-maps.xtm::source-handler))))
130 (topic-maps.xtm::validate-source root)
131 (topic-maps.xtm::read-topic (first (topic-maps.xtm::elements root)) map))
132 (check "retained:forward-handle-eq" (and (eq (canonical-topic old) (canonical-topic other)) (not (eq old (canonical-topic old)))))
133 (check "retained:role-player-canonical" (eq (association-role-player role) (canonical-topic old)))
134 (check "retained:role-parent-canonical" (eq (association-role-parent role) (canonical-association a)))
135 (let ((before (public-state map)) (pointer (topic-maps.tmdm::%topic-forward old)))
136 (topic-maps.cxtm:canonicalize map :base-locator *document*)
137 (check "retained:cxtm-does-not-mutate" (and (equal before (public-state map)) (eq pointer (topic-maps.tmdm::%topic-forward old))))))
138 (let ((a (read-body "<topic id='same'/>") ) (b (read-body "<topic id='same'/>" "https://other.example/map.xtm")))
139 (check "documents:distinct-object" (not (eq (first (topic-map-topics a)) (first (topic-map-topics b)))))
140 (check "documents:distinct-local-locators" (not (equal (topic-item-identifiers (first (topic-map-topics a))) (topic-item-identifiers (first (topic-map-topics b)))))))
141 (let ((map (read-body "<topic id='a'><subjectLocator href='resource'/><subjectIdentifier href='urn:subject'/><itemIdentity href='#alias'/></topic>")))
142 (check "identity:three-categories" (let ((topic (first (topic-map-topics map))))
143 (and (= 2 (length (topic-item-identifiers topic)))
144 (equal '("urn:subject") (topic-subject-identifiers topic))
145 (equal (list (concatenate 'string (subseq *document* 0 (1+ (position #\/ *document* :from-end t))) "resource")) (topic-subject-locators topic))))))
146 (let ((map (read-body "<topic id=' Ä水 '><subjectIdentifier href='http://example.org/%C3%A4/%F0%9F%A6%8A'/></topic>")))
147 (check "unicode:id-collapse" (by-id map "Ä水"))
148 (check "unicode:href-percent-utf8" (equal '("http://example.org/ä/🦊") (topic-subject-identifiers (first (topic-map-topics map))))))
149 (dolist (pair '(("../folder/./x#t" "https://example.test/folder/x#t")
150 ("#t" "https://example.test/a/map.xtm#t")
151 ("?q#t" "https://example.test/a/map.xtm?q#t")
152 ("http://EXAMPLE.test:80/a" "http://EXAMPLE.test:80/a")
153 ("http://example.test/a%20b#t" "http://example.test/a b#t")))
154 (let ((topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* "iri-test"))
155 (check (concatenate 'string "iri:" (first pair))
156 (equal (topic-maps.xtm::resolve-iri (first pair) "https://example.test/a/map.xtm" nil) (second pair)))))
157 (let* ((source (document "<topic id='a'/><topic id='b'><instanceOf><topicRef href='#a'/></instanceOf></topic>"))
158 (one (read-body "<topic id='a'/><topic id='b'><instanceOf><topicRef href='#a'/></instanceOf></topic>"))
159 (two (read-body "<topic id='b'><instanceOf><topicRef href='#a'/></instanceOf></topic><topic id='a'/>")))
160 (check "order:canonical-independence" (equal (topic-maps.cxtm:canonicalize one :base-locator *document*) (topic-maps.cxtm:canonicalize two :base-locator *document*)))
161 (dolist (spec '((:utf-8 t "UTF-8") (:utf-16le t "UTF-16") (:utf-16be t "UTF-16")
162 (:utf-16le nil "UTF-16LE") (:utf-16be nil "UTF-16BE")))
163 (destructuring-bind (encoding bom label) spec
164 (let* ((xml (format nil "<?xml version='1.0' encoding='~A'?>~A" label source))
165 (map (deserialize-octets (bytes xml encoding bom) :document-iri *document*)))
166 (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*))))))))
167 (defun core-topic-for-test (map name)
168 (intern-topic map :subject-identifier (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" name)))
169
170 (defun identity-cascade-tests ()
171 (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>")))
172 (check "transitive:mixed-identifiers-one-topic" (= 1 (length (topic-map-topics map))))
173 (check "transitive:three-ii-two-si" (let ((topic (first (topic-map-topics map))))
174 (and (= 3 (length (topic-item-identifiers topic))) (= 2 (length (topic-subject-identifiers topic)))))))
175 (let* ((map (read-body "<topic id='s'><subjectLocator href='#x'/></topic><topic id='a'><instanceOf><topicRef href='#x'/></instanceOf></topic>"))
176 (s (by-id map "s")) (x (by-id map "x")))
177 (check "reference:subject-locator-is-not-si-or-ii" (and s x (not (eq s x)) (= 6 (length (topic-map-topics map))))))
178 (let* ((map (make-topic-map)) (topic-maps.xtm::*document-iri* *document*)
179 (topic-maps.xtm::*source-key* "association-cascade")
180 (root (topic-maps.xml:parse-octets
181 (bytes (document "<topic id='a'><instanceOf><topicRef href='#t1'/><topicRef href='#t2'/></instanceOf></topic><topic id='t1'><itemIdentity href='#t2'/></topic>"))
182 (make-instance 'topic-maps.xtm::source-handler)))
183 (nodes (topic-maps.xtm::elements (topic-maps.xtm::validate-source root))))
184 (topic-maps.xtm::read-topic (first nodes) map)
185 (let* ((associations (topic-map-associations map))
186 (roles (loop for a in associations append (association-roles a)))
187 (old-type (by-id map "t2")))
188 (check "cascade:before-two-associations" (= 2 (length associations)))
189 (topic-maps.xtm::read-topic (second nodes) map)
190 (let ((a (first (topic-map-associations map))))
191 (check "cascade:after-one-association" (and (= 1 (length (topic-map-associations map))) (= 2 (length (association-roles a)))))
192 (check "cascade:retained-association-handles-eq" (every (lambda (old) (eq a (canonical-association old))) associations))
193 (check "cascade:retained-role-handles-eq" (every (lambda (r) (member (canonical-association-role r) (association-roles a) :test #'eq)) roles))
194 (check "cascade:stored-type-player-canonical" (every (lambda (r) (and (eq a (association-role-parent r))
195 (eq (association-role-type r) (canonical-topic (association-role-type r)))
196 (eq (association-role-player r) (canonical-topic (association-role-player r))))) (association-roles a)))
197 (check "cascade:retained-topic-handle-eq" (eq (canonical-topic old-type) (by-id map "t1")))
198 (let* ((handles (append associations roles (list old-type)))
199 (pointers (mapcar #'topic-maps.tmdm::canonical-item handles))
200 (before (public-state map)))
201 (topic-maps.cxtm:canonicalize map :base-locator *document*)
202 (check "cascade:all-handles-pure" (and (equal before (public-state map))
203 (every #'eq pointers (mapcar #'topic-maps.tmdm::canonical-item handles))))))))
204 (let* ((prefixed "<x:topicMap xmlns:x='http://www.topicmaps.org/xtm/' version='2.0'><x:topic id='a'/></x:topicMap>")
205 (source (bytes prefixed))
206 (a (deserialize-octets source :document-iri *document* :source-key "disk:a"))
207 (b (deserialize-octets source :document-iri *document* :source-key "memory:b")))
208 (check "namespace:prefix-equivalent" (by-id a "a"))
209 (check "source-key:not-topic-identity" (equal (topic-item-identifiers (by-id a "a")) (topic-item-identifiers (by-id b "a")))))
210 (let* ((map (read-body "<topic id='a'><instanceOf><topicRef href='https://external.example/map.xtm#unfetched'/></instanceOf></topic>"))
211 (type (find-if (lambda (topic) (member "https://external.example/map.xtm#unfetched" (topic-item-identifiers topic) :test #'equal)) (topic-map-topics map))))
212 (graph-checks "external-reference:no-retrieval" map (by-id map "a") type)))
213
214 (defun negative-tests ()
215 (dolist (spec '(("missing-id" "<topic/>" :missing-id)
216 ("invalid-id" "<topic id='2bad'/>" :invalid-id)
217 ("id-colon" "<topic id='a:b'/>" :invalid-id)
218 ("id-empty" "<topic id=''/>" :invalid-id)
219 ("id-spaces" "<topic id='a b'/>" :invalid-id)
220 ("duplicate-id" "<topic id='a'/><topic id=' a '/>" :duplicate-id)
221 ("unknown-attribute" "<topic id='a' other='v'/>" :unexpected-attribute)
222 ("qualified-attribute" "<topic xmlns:x='urn:x' x:id='a'/>" :unexpected-attribute)
223 ("unknown-element" "<topic id='a'><garbage/></topic>" :unexpected-element)
224 ("wrong-namespace-child" "<topic xmlns='urn:other' id='a'/>" :invalid-namespace)
225 ("topic-identity-order" "<topic id='a'><instanceOf><topicRef href='#b'/></instanceOf><itemIdentity href='#c'/></topic>" :element-order)
226 ("map-identity-order" "<topic id='a'/><itemIdentity href='#map'/>" :element-order)
227 ("instance-duplicate" "<topic id='a'><instanceOf><topicRef href='#b'/></instanceOf><instanceOf><topicRef href='#b'/></instanceOf></topic>" :duplicate-instance-of)
228 ("empty-instance" "<topic id='a'><instanceOf/></topic>" :empty-instance-of)
229 ("missing-href" "<topic id='a'><subjectIdentifier/></topic>" :missing-attribute)
230 ("nonempty-locator" "<topic id='a'><subjectIdentifier href='urn:s'><topic/></subjectIdentifier></topic>" :unexpected-child)
231 ("locator-text" "<topic id='a'><subjectIdentifier href='urn:s'>text</subjectIdentifier></topic>" :unexpected-text)
232 ("non-whitespace" "text" :unexpected-text)
233 ("no-ref-fragment" "<topic id='a'><instanceOf><topicRef href='other.xtm'/></instanceOf></topic>" :invalid-topic-ref)
234 ("bad-ref-fragment" "<topic id='a'><instanceOf><topicRef href='#2bad'/></instanceOf></topic>" :invalid-topic-ref)
235 ("empty-ref-fragment" "<topic id='a'><instanceOf><topicRef href='#'/></instanceOf></topic>" :invalid-topic-ref)
236 ("xpointer-ref" "<topic id='a'><instanceOf><topicRef href='#xpointer(a)'/></instanceOf></topic>" :invalid-topic-ref)
237 ("relative-colon" "<topic id='a'><subjectIdentifier href='1bad:subject'/></topic>" :invalid-iri)
238 ("decoded-control" "<topic id='a'><subjectIdentifier href='urn:%00'/></topic>" :invalid-iri)
239 ("bad-percent" "<topic id='a'><subjectIdentifier href='urn:%GG'/></topic>" :invalid-percent-escape)
240 ("bad-percent-utf8" "<topic id='a'><subjectIdentifier href='urn:%C3%28'/></topic>" :invalid-percent-utf8)
241 ("invalid-iri" "<topic id='a'><subjectIdentifier href='urn:a b'/></topic>" :invalid-iri)))
242 (rejection (first spec) (document (second spec)) 'xtm-rejection (third spec)))
243 (rejection "missing-version" "<topicMap xmlns='http://www.topicmaps.org/xtm/'/>" 'xtm-rejection :missing-attribute)
244 (rejection "version-21" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.1'/>" 'xtm-rejection :invalid-version)
245 (rejection "no-namespace" "<topicMap version='2.0'/>" 'xtm-rejection :invalid-namespace)
246 (rejection "namespace-10" "<topicMap xmlns='http://www.topicmaps.org/xtm/1.0/' version='2.0'/>" 'xtm-rejection :invalid-namespace)
247 (rejection "xml-misnested" "<a><b></a>" 'xtm-xml-error :xml-not-well-formed)
248 (rejection "xml-duplicate-attribute" "<topicMap version='2.0' version='2.0'/>" 'xtm-xml-error :xml-not-well-formed)
249 (rejection "xml-undeclared-prefix" "<x:topicMap/>" 'xtm-xml-error :xml-not-well-formed)
250 (dolist (body '("<!DOCTYPE topicMap>" "<!DOCTYPE topicMap SYSTEM 'file:///tmp/no-xtm-file'>"
251 "<!DOCTYPE topicMap SYSTEM 'http://127.0.0.1:9/no-xtm-network'>"
252 "<!DOCTYPE topicMap [<!ENTITY e SYSTEM 'file:///tmp/no-xtm-file'>]>"))
253 (rejection (concatenate 'string "doctype:" body) (concatenate 'string body (document "")) 'xtm-xml-error :doctype-forbidden))
254 (dolist (spec '(("unsupported-name" "<topic id='a'><name><value>Name</value></name></topic>" :unsupported-construct)
255 ("unsupported-occurrence" "<topic id='a'><occurrence><type><topicRef href='#t'/></type><resourceData>v</resourceData></occurrence></topic>" :unsupported-construct)
256 ("unsupported-association" "<association><type><topicRef href='#t'/></type><role><type><topicRef href='#r'/></type><topicRef href='#p'/></role></association>" :unsupported-construct)
257 ("unsupported-mergemap" "<mergeMap href='other.xtm'/>" :unsupported-construct)))
258 (rejection (first spec) (document (second spec)) 'xtm-unsupported (third spec)))
259 (rejection "unsupported-reifier" "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0' reifier='#r'/>" 'xtm-unsupported :reification)
260 (let ((c (condition-from (lambda () (read-body (format nil "~%~%<topic id='2bad'/>"))))))
261 (check "diagnostic:multiline-observed-position" (and (typep c 'xtm-rejection) (= 3 (error-line c)) (plusp (error-column c)))))
262 (dolist (iri '(nil "relative.xtm" "https://example.test/a#fragment"))
263 (let ((c (condition-from (lambda () (deserialize-octets (bytes (document "")) :document-iri iri :source-key "document")))))
264 (check (format nil "document-iri:~S" iri) (and (typep c 'xtm-rejection) (eq (error-code c) :invalid-document-iri)))))
265 (check "api:raw-bytes-required" (typep (condition-from (lambda () (deserialize-octets (document "") :document-iri *document*))) 'type-error))
266 (let* ((source "<itemIdentity href='#a'/><topic id='a'/>")
267 (c (condition-from (lambda () (read-body source)))))
268 (check "tmdm:identifier-conflict-not-xml-or-xtm" (and (typep c 'item-identifier-error) (not (typep c 'topic-maps.xtm:xtm-error))))
269 (check "tmdm:identifier-conflict-specific" (eq (item-identifier-error-code c) :duplicate-item-identifier)))
270 (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>")))))
271 (check "tmdm:core-subject-conflict-preserved" (and (typep c 'core-subject-error) (eq :core-subject-conflict (core-subject-error-code c)))))
272 (dolist (bad '(#(195 40) #(192 175) #(226 130) #(237 160 128)))
273 (let ((c (condition-from (lambda () (deserialize-octets
274 (concatenate '(vector (unsigned-byte 8)) (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><topic id='a'><subjectIdentifier href='urn:")
275 (coerce bad '(vector (unsigned-byte 8))) (bytes "'/></topic></topicMap>")) :document-iri *document*)))))
276 (check (format nil "actual-reader:invalid-utf8:~S" bad) (and (typep c 'xtm-xml-error) (eq :xml-not-well-formed (error-code c))))))
277 ;; Astral Unicode in an href exercises the qualified UTF-16 decoder through
278 ;; the real XTM reader. No pre-decoding strings are accepted by its API.
279 (dolist (encoding '(:utf-16le :utf-16be))
280 (let ((map (deserialize-octets (bytes (document "<topic id='a'><subjectIdentifier href='urn:🦊'/></topic>") encoding t) :document-iri *document*)))
281 (check (format nil "actual-reader:astral-surrogate:~A" encoding) (by-si map "urn:🦊"))))
282 (dolist (bad '(#(0 216) #(0 220) #(0 216 65 0)))
283 (let ((c (condition-from (lambda ()
284 (deserialize-octets (concatenate '(vector (unsigned-byte 8))
285 (bytes "<r>" :utf-16le t) (coerce bad '(vector (unsigned-byte 8))) (bytes "</r>" :utf-16le)) :document-iri *document*)))))
286 (check (format nil "actual-reader:invalid-surrogate:~S" bad) (typep c 'xtm-xml-error))))
287 ;; Verify the required entry point is actually called exactly once and sees
288 ;; the original octet vector by EQ, not a reconstructed decoded XML string.
289 (let* ((original (symbol-function 'topic-maps.xml:parse-octets))
290 (source (bytes (document "<topic id='a'/>"))) (calls 0) (same nil))
291 (unwind-protect
292 (progn (setf (symbol-function 'topic-maps.xml:parse-octets)
293 (lambda (bytes handler) (incf calls) (setf same (eq source bytes)) (funcall original bytes handler)))
294 (deserialize-octets source :document-iri *document*)
295 (check "boundary:real-parse-octets-only-once" (and (= 1 calls) same)))
296 (setf (symbol-function 'topic-maps.xml:parse-octets) original))))
297
298 (defun helper-source-test ()
299 (let* ((id "ctm/in/mergemap-xtm.sub")
300 (iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
301 (*document* iri) (map (deserialize-octets (file-bytes (corpus-path id)) :document-iri iri :source-key id)))
302 (graph-checks "direct-sub" map (by-id map "lars") (by-id map "person"))
303 (check "direct-sub:exact-lars-ii" (equal (list (concatenate 'string iri "#lars")) (topic-item-identifiers (by-id map "lars"))))
304 (check "direct-sub:exact-person-ii" (equal (list (concatenate 'string iri "#person")) (topic-item-identifiers (by-id map "person"))))
305 (check "direct-sub:no-names-occurrences" (every (lambda (topic) (and (null (topic-names topic)) (null (topic-occurrences topic)))) (topic-map-topics map)))
306 (setf *fixture* map)))
307
308 (defun run-tests ()
309 (setf *checks* nil *fixture* nil)
310 (original-contracts) (semantic-tests) (identity-cascade-tests) (negative-tests) (helper-source-test)
311 (check "checks:unique-test-names" (= (length *checks*) (length (remove-duplicates *checks* :test #'equal))))
312 (format t "XTM-SUMMARY ~D PASS 0 FAIL; 6 original CXTM PASS; helper source PASS~%" (length *checks*))
313 (length *checks*))
314
315 (defun write-report (pathname)
316 (let ((cases *cases*) (fixture *fixture*))
317 (flet ((fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
318 (array (items) (coerce items 'vector)))
319 (with-open-file (out pathname :direction :output :if-exists :supersede :external-format :utf-8)
320 (topic-maps.test-foundation::emit-json-value
321 (fields "scope" "XTM 2.0 identity subset, six registered positive contracts only; no CTM Mergemap integration"
322 "corpusCommit" "cd165b150176c1d6842b37be9cc9051b40d4996b"
323 "statuses" (fields "pass" (count :pass cases :key #'topic-maps.test-foundation:test-case-status)
324 "fail" (count :fail cases :key #'topic-maps.test-foundation:test-case-status)
325 "unsupported" 0 "not-run" 0)
326 "otherCorpusInputs" (fields "positiveUnexecuted" (- (length (directory (corpus-path "xtm2/in/*.xtm"))) (length cases))
327 "negativeUnregisteredUnexecuted" (length (directory (corpus-path "xtm2/invalid/*.xtm"))))
328 "checks" (array (reverse *checks*))
329 "cases" (array (loop for case in cases collect
330 (fields "id" (topic-maps.test-foundation:test-case-id case)
331 "documentIRI" (topic-maps.test-foundation:test-case-document-iri case)
332 "status" (string-downcase (symbol-name (topic-maps.test-foundation:test-case-status case)))
333 "actualOutcome" (string-downcase (symbol-name (topic-maps.test-foundation:test-case-actual-outcome case)))
334 "diagnostics" (array (topic-maps.test-foundation:test-case-diagnostics case)))))
335 "helperSource" (fields "id" "ctm/in/mergemap-xtm.sub" "status" "pass" "topics" (length (topic-map-topics fixture))
336 "associations" (length (topic-map-associations fixture)) "roles" 2
337 "ctmMergemapContract" "NOT-RUN; direct helper deserialization only")
338 "actualAndExpected" (array (loop for (id actual expected) in *outputs* collect
339 (fields "id" id "actual" (babel:octets-to-string actual :encoding :utf-8)
340 "expected" (babel:octets-to-string expected :encoding :utf-8) "byteLength" (length actual))))) out)
341 (terpri out)))))