]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-variant-resourcerefs.lisp
feat(xtm): support anyURI resource data on name variants
[topic-maps.git] / tests / xtm-variant-resourcerefs.lisp
1 (in-package #:topic-maps.xtm.tests)
2
3 (defparameter *resource-ref-original-specs*
4 '(("variant-resourceref" "http://example.org/command.wav" "http://example.org/command.wav" 621)
5 ("variant-resourceref-relative" "topic-command.wav"
6 "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/topic-command.wav" 608)))
7
8 (defun resource-ref-xml (href themes)
9 (format nil "<variant>~A<resourceRef href='~A'/></variant>" (scope-xml themes) href))
10
11 (defun resource-ref-original-contracts ()
12 (setf *resource-ref-outputs* nil)
13 (dolist (spec *resource-ref-original-specs*)
14 (destructuring-bind (stem href value baseline-bytes) spec
15 (let* ((id (format nil "xtm2/in/~A.xtm" stem))
16 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
17 (raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
18 (expected (file-bytes (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem))))
19 (case (topic-maps.test-foundation:make-test-case :id id :category :valid
20 :source-pathname (corpus-path id) :document-iri *document* :expected-outcome :cxtm-equal
21 :expected-baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem))
22 :clauses '("XTM 2.0 4.3.3,4.3.4,4.12,4.13,4.17" "TMDM 5.5,6.4" "CXTM 3,4")))
23 (map nil) (actual nil) (c nil) (label (concatenate 'string "resource-ref-original:" stem)))
24 (setf *cases* (append *cases* (list case)))
25 (handler-case
26 (progn
27 (setf map (deserialize-octets raw :document-iri *document* :source-key id))
28 (let* ((owner (by-id map "topic")) (name (first (topic-names owner)))
29 (v (first (topic-name-variants name))) (theme (by-id map "command"))
30 (before (variant-state map))
31 (root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
32 (source-topic (first (topic-maps.xtm::elements root)))
33 (source-name (first (topic-maps.xtm::elements source-topic)))
34 (source-variant (second (topic-maps.xtm::elements source-name)))
35 (resource (second (topic-maps.xtm::elements source-variant))))
36 ;; Inspect the actual original's resource node, not its filename.
37 (check (concatenate 'string label ":original-resource-href")
38 (and (equal "resourceRef" (topic-maps.xtm::node-name resource))
39 (equal href (topic-maps.xtm::attr resource "href"))
40 (= baseline-bytes (length expected))))
41 (check (concatenate 'string label ":native-parent-value-datatype-scope-eq")
42 (and (= 3 (length (topic-map-topics map))) (null (topic-map-associations map))
43 (= 1 (length (all-variants map))) (= 1 (length (topic-names owner)))
44 (eq name (canonical-topic-name name)) (eq owner (topic-name-parent name))
45 (eq name (variant-parent v)) (eq v (canonical-variant v))
46 (equal "Topic" (topic-name-value name)) (null (topic-name-scope name))
47 (equal value (variant-value v)) (equal (iri-datatype) (variant-datatype v))
48 (scope-eq-p (variant-scope v) (list theme)) (variants-current-p map)))
49 (check (concatenate 'string label ":resource-does-not-create-topic-identifiers")
50 (and (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner))
51 (equal (list (concatenate 'string *document* "#command")) (item-identifiers theme))
52 (every (lambda (topic) (and (null (topic-subject-locators topic))
53 (if (eq topic (topic-name-type name))
54 (and (null (item-identifiers topic)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers topic)))
55 (null (topic-subject-identifiers topic))))) (topic-map-topics map))
56 (null (item-identifiers map)) (null (item-identifiers name)) (null (item-identifiers v))
57 (null (item-reifier name)) (null (item-reifier v))
58 (topic-maps.tmdm::validate-map-item-identifiers map)))
59 (setf actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
60 (check (concatenate 'string label ":source-preserved-pure-deterministic")
61 (and (equalp raw copy) (equal before (variant-state map))
62 (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
63 (equal before (variant-state map))))
64 (let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:resource-ref-provenance")))
65 (check (concatenate 'string label ":source-key-independent-model-and-cxtm")
66 (and (equalp (variant-model-data map) (variant-model-data other))
67 (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
68 (error (condition) (setf c condition)))
69 (record-original-result case actual expected c)
70 (push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id
71 "sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
72 "sourceText" (babel:octets-to-string raw :encoding :utf-8) "href" href "absoluteValue" value
73 "model" (when map (variant-model-data map)) "condition" (negative-condition-data c)
74 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
75 "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
76 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
77 "firstDifferentByte" (when actual (mismatch actual expected))
78 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *resource-ref-outputs*)
79 (when c (error c))
80 (check (concatenate 'string label ":byte-exact") (equalp actual expected)))))
81 (setf *resource-ref-outputs* (nreverse *resource-ref-outputs*))
82 (check "resource-ref-originals:59-unique-case-records"
83 (and (= 59 (length *cases*)) (= 51 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
84 (= 59 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
85
86 (defun resource-ref-value-controls ()
87 (dolist (row '(("absolute-no-fragment" "https://external.invalid/file.xml" "https://external.invalid/file.xml")
88 ("relative" "../resource.wav" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/resource.wav")
89 ("fragment-not-shorthand" "#not/a/shorthand" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm#not/a/shorthand")
90 ("empty-reference" "" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm")
91 ("unicode" "urn:馬籠🦊" "urn:馬籠🦊") ("percent-utf8" "urn:%E9%A6%AC%F0%9F%A6%8A" "urn:馬🦊")
92 ("percent-reserved" "https://external.invalid/a%2Fb%23part" "https://external.invalid/a/b#part")
93 ("XML-escaping-once" "https://external.invalid/?a=1&amp;b=2" "https://external.invalid/?a=1&b=2")
94 ("urn-no-fragment" "urn:external" "urn:external")
95 ("file-no-read" "file:///nonexistent-21i/resource.xml" "file:///nonexistent-21i/resource.xml")))
96 (destructuring-bind (label href value) row
97 (variant-control (concatenate 'string "resource-ref:" label)
98 (format nil "<topic id='a'>~A</topic>" (variant-name-xml nil (resource-ref-xml href '("s"))))
99 (lambda (map)
100 (let* ((owner (by-id map "a")) (name (first (topic-names owner))) (v (first (all-variants map)))
101 (comparison (read-body (format nil "<topic id='a'>~A</topic>" (variant-name-xml nil (variant-xml "text" '("s")))))))
102 (check (concatenate 'string "resource-ref:value-datatype-parent-no-topic-effects:" label)
103 (and (equal value (variant-value v)) (equal (iri-datatype) (variant-datatype v))
104 (eq owner (topic-name-parent name)) (eq name (variant-parent v)) (eq v (canonical-variant v))
105 (scope-eq-p (variant-scope v) (list (by-id map "s")))
106 (equalp (original-model-data map) (original-model-data comparison)))))))))
107 (let ((*document* "https://other.invalid/maps/input.xtm?version=2"))
108 (variant-control "resource-ref:alternate-document"
109 (format nil "<topic id='a'>~A</topic>" (variant-name-xml nil (resource-ref-xml "sound.wav" '("s"))))
110 (lambda (map) (check "resource-ref:alternate-document-absolute-value"
111 (and (equal "https://other.invalid/maps/sound.wav" (variant-value (first (all-variants map))))
112 (equal (list (concatenate 'string *document* "#a")) (item-identifiers (by-id map "a"))))))))
113 (variant-control "resource-ref:inherited-scope-duplicates-forward"
114 (format nil "<topic id='a'>~A</topic><topic id='base'/><topic id='s'/><topic id='t'/>"
115 (variant-name-xml '("base") (concatenate 'string
116 (resource-ref-xml "urn:sound" '("base" "s" "t" "s")) (resource-ref-xml "urn:sound" '("t" "s")))))
117 (lambda (map) (let* ((name (first (topic-names (by-id map "a")))) (v (first (all-variants map))))
118 (check "resource-ref:scope-union-and-variant-normalization"
119 (and (= 1 (length (all-variants map))) (equal "urn:sound" (variant-value v))
120 (equal (iri-datatype) (variant-datatype v)) (eq name (variant-parent v))
121 (scope-eq-p (topic-name-scope name) (list (by-id map "base")))
122 (scope-eq-p (variant-scope v) (list (by-id map "base") (by-id map "s") (by-id map "t"))))))))
123 (variant-control "resource-ref:distinct-values-datatypes-scopes-and-normalized-iris"
124 (format nil "<topic id='a'>~A</topic>" (variant-name-xml nil (concatenate 'string
125 (resource-ref-xml "urn:a" '("s")) (resource-ref-xml "urn:%61" '("s"))
126 (resource-ref-xml "urn:b" '("s")) (resource-ref-xml "urn:a" '("t")) (variant-xml "urn:a" '("s")))))
127 (lambda (map)
128 (check "resource-ref:equality-is-value-datatype-scope-not-topic-identity"
129 (and (= 4 (length (all-variants map))) (= 4 (length (topic-map-topics map)))
130 (= 3 (count (iri-datatype) (all-variants map) :key #'variant-datatype :test #'equal))
131 (= 1 (count (string-datatype) (all-variants map) :key #'variant-datatype :test #'equal))))))
132 (variant-control "resource-ref:same-locator-does-not-identify-topics"
133 (format nil "<topic id='a'><subjectIdentifier href='urn:shared'/>~A</topic><topic id='b'><subjectLocator href='urn:shared'/>~A</topic>"
134 (variant-name-xml nil (resource-ref-xml "urn:shared" '("s")))
135 (variant-name-xml nil (resource-ref-xml "urn:shared" '("s"))))
136 (lambda (map)
137 (check "resource-ref:resource-identity-separated-from-SI-SL-II"
138 (and (not (eq (by-id map "a") (by-id map "b"))) (= 4 (length (topic-map-topics map)))
139 (= 2 (length (all-variants map)))
140 (equal '("urn:shared") (topic-subject-identifiers (by-id map "a")))
141 (equal '("urn:shared") (topic-subject-locators (by-id map "b")))
142 (equal (list (concatenate 'string *document* "#a")) (item-identifiers (by-id map "a")))
143 (equal (list (concatenate 'string *document* "#b")) (item-identifiers (by-id map "b")))))))
144 ;; One primary XML parse only, even for an unreachable external .xml locator.
145 (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (calls 0))
146 (unwind-protect
147 (progn
148 (setf (symbol-function 'topic-maps.xml:parse-octets)
149 (lambda (&rest args) (incf calls) (apply saved args)))
150 (read-body (format nil "<topic id='a'>~A</topic>"
151 (variant-name-xml nil (resource-ref-xml "https://unreachable.invalid/external.xml" '("s")))))
152 (check "resource-ref:one-primary-XML-parse-no-recursive-deserialization" (= 1 calls)))
153 (setf (symbol-function 'topic-maps.xml:parse-octets) saved))))
154
155 (defun resource-ref-error-controls ()
156 (dolist (row '(("missing-href" "<resourceRef/>" :missing-attribute)
157 ("extra-attribute" "<resourceRef href='urn:x' extra='bad'/>" :unexpected-attribute)
158 ("namespaced-attribute" "<resourceRef xmlns:a='urn:a' href='urn:x' a:href='urn:y'/>" :unexpected-attribute)
159 ("child" "<resourceRef href='urn:x'><topicRef href='#s'/></resourceRef>" :unexpected-child)
160 ("text" "<resourceRef href='urn:x'>text</resourceRef>" :unexpected-text)
161 ("invalid-iri" "<resourceRef href='bad space'/>" :invalid-iri)
162 ("invalid-decoded-iri" "<resourceRef href='urn:x%00'/>" :invalid-iri)
163 ("invalid-relative-iri" "<resourceRef href='1bad:relative'/>" :invalid-iri)
164 ("percent-short" "<resourceRef href='urn:x%2'/>" :invalid-percent-escape)
165 ("percent-hex" "<resourceRef href='urn:x%GG'/>" :invalid-percent-escape)
166 ("percent-incomplete-utf8" "<resourceRef href='urn:x%C3'/>" :invalid-percent-utf8)
167 ("percent-overlong-utf8" "<resourceRef href='urn:x%C0%AF'/>" :invalid-percent-utf8)
168 ("percent-surrogate" "<resourceRef href='urn:x%ED%A0%80'/>" :invalid-percent-utf8)))
169 (variant-error-control (concatenate 'string "resource-ref:" (first row))
170 (format nil "<topic id='a'><name><value>N</value><variant><scope><topicRef href='#s'/></scope>~A</variant></name></topic>" (second row))
171 'xtm-rejection (third row)))
172 (dolist (row '(("mixed-ref-data" "<scope><topicRef href='#s'/></scope><resourceRef href='urn:x'/><resourceData>V</resourceData>" :variant-resource-cardinality)
173 ("multiple-ref" "<scope><topicRef href='#s'/></scope><resourceRef href='urn:x'/><resourceRef href='urn:y'/>" :variant-resource-cardinality)
174 ("wrong-order" "<resourceRef href='urn:x'/><scope><topicRef href='#s'/></scope>" :missing-variant-scope)
175 ("scope-after-resource" "<scope><topicRef href='#s'/></scope><resourceRef href='urn:x'/><scope><topicRef href='#t'/></scope>" :element-order)))
176 (variant-error-control (concatenate 'string "resource-ref:" (first row))
177 (format nil "<topic id='a'><name><value>N</value><variant>~A</variant></name></topic>" (second row)) 'xtm-rejection (third row)))
178 (dolist (row '(("equal-scope" ("base")) ("duplicate-scope-no-new-theme" ("base" "base"))))
179 (variant-error-control (concatenate 'string "resource-ref:" (first row))
180 (format nil "<topic id='a'>~A</topic>" (variant-name-xml '("base") (resource-ref-xml "urn:x" (second row))))
181 'variant-error :invalid-variant-scope))
182 (variant-error-control "resource-ref:SI-alias-adds-no-theme"
183 "<topic id='base'><subjectIdentifier href='urn:base#alias'/></topic><topic id='a'><name><scope><topicRef href='#base'/></scope><value>N</value><variant><scope><topicRef href='urn:base#alias'/></scope><resourceRef href='urn:x'/></variant></name></topic>"
184 'variant-error :invalid-variant-scope)
185 (variant-error-control "resource-ref:forward-merge-collapses-proper-scope"
186 (format nil "<topic id='a'>~A</topic><topic id='base'><itemIdentity href='#extra'/></topic>"
187 (variant-name-xml '("base") (resource-ref-xml "urn:x" '("extra")))) 'variant-error :variant-scope-collapse)
188 (let* ((key "resource-ref:invalid-raw-UTF8")
189 (raw (concatenate '(vector (unsigned-byte 8))
190 (bytes "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'><topic id='a'><name><value>N</value><variant><scope><topicRef href='#s'/></scope><resourceRef href='urn:")
191 #(255) (bytes "'/></variant></name></topic></topicMap>")))
192 (c (condition-from (lambda () (deserialize-octets raw :document-iri *document* :source-key key)))))
193 (check "resource-ref:invalid-raw-UTF8-is-XML-error"
194 (and (eq 'xtm-xml-error (type-of c)) (eq :xml-not-well-formed (error-code c))
195 (equal key (error-source-key c)) (equal *document* (error-document-iri c))))
196 (push (topic-maps.test-foundation::fields "label" key "kind" "invalid-XML-octets"
197 "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "condition" (negative-condition-data c)) *variant-controls*))
198 (dolist (kind '(:subject-identifier :subject-locator :item-identifier))
199 (let* ((element (ecase kind (:subject-identifier "subjectIdentifier") (:subject-locator "subjectLocator") (:item-identifier "itemIdentity")))
200 (body (format nil "<topic id='base'><~A href='urn:base'/></topic><topic id='extra'><~A href='urn:extra'/></topic><topic id='a'>~A</topic>"
201 element element (variant-name-xml '("base") (resource-ref-xml "urn:resource" '("extra")))))
202 (map (read-body body)) (v (first (all-variants map))) (before (variant-state map))
203 (c (condition-from (lambda () (add-topic-identifier (by-id map "extra") kind "urn:base")))))
204 (check (format nil "resource-ref:scope-collapse-~A-specific-atomic-eq" kind)
205 (and (eq 'variant-error (type-of c)) (eq :variant-scope-collapse (variant-error-code c))
206 (equal before (variant-state map)) (eq v (canonical-variant v)) (variants-current-p map)))
207 (push (topic-maps.test-foundation::fields "label" (format nil "resource-ref:collapse-~A" kind)
208 "kind" "native-TMDM-collapse-atomicity" "sourceText" (document body)
209 "condition" (negative-condition-data c) "modelUnchanged" :true "model" (variant-model-data map)) *variant-controls*))))
210
211 (defun resource-ref-tests ()
212 (let ((*variant-controls* nil))
213 (resource-ref-value-controls)
214 (variant-merge-controls (lambda (themes) (resource-ref-xml "urn:resource" themes)) "resource-ref-merge:"
215 (lambda (label map)
216 (check (concatenate 'string label ":absolute-resource-value-and-datatype")
217 (every (lambda (v) (and (equal "urn:resource" (variant-value v))
218 (equal (iri-datatype) (variant-datatype v)))) (all-variants map)))))
219 (resource-ref-error-controls)
220 (setf *resource-ref-controls* (nreverse *variant-controls*))))