(in-package #:topic-maps.xtm.tests)
(defun uri-data-original-contract ()
(setf *uri-data-outputs* nil)
(dolist (spec '(("variant-resourcedata-uri" "http://example.org/command.wav" "http://example.org/command.wav" 621)))
(destructuring-bind (stem source-text value baseline-bytes) spec
(let* ((id (format nil "xtm2/in/~A.xtm" stem))
(*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
(expected (file-bytes (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem))))
(case (topic-maps.test-foundation:make-test-case :id id :category :valid
:source-pathname (corpus-path id) :document-iri *document* :expected-outcome :cxtm-equal
:expected-baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem))
:clauses '("XTM 2.0 4.3.3,4.3.4,4.12,4.13,4.17" "TMDM 5.5,6.4" "CXTM 3,4")))
(map nil) (actual nil) (c nil) (label (concatenate 'string "uri-data-original:" stem)))
(setf *cases* (append *cases* (list case)))
(handler-case
(progn
(setf map (deserialize-octets raw :document-iri *document* :source-key id))
(let* ((owner (by-id map "topic")) (name (first (topic-names owner)))
(v (first (topic-name-variants name))) (theme (by-id map "command"))
(before (variant-state map))
(root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
(source-topic (first (topic-maps.xtm::elements root)))
(source-name (first (topic-maps.xtm::elements source-topic)))
(source-variant (second (topic-maps.xtm::elements source-name)))
(resource (second (topic-maps.xtm::elements source-variant))))
;; Inspect the actual original's resource node, not its filename.
(check (concatenate 'string label ":original-resource-text-and-datatype")
(and (equal "resourceData" (topic-maps.xtm::node-name resource))
(equal (iri-datatype) (topic-maps.xtm::attr resource "datatype"))
(equal source-text (apply #'concatenate 'string (topic-maps.xtm::node-children resource)))
(= baseline-bytes (length expected))))
(check (concatenate 'string label ":native-parent-value-datatype-scope-eq")
(and (= 3 (length (topic-map-topics map))) (null (topic-map-associations map))
(= 1 (length (all-variants map))) (= 1 (length (topic-names owner)))
(eq name (canonical-topic-name name)) (eq owner (topic-name-parent name))
(eq name (variant-parent v)) (eq v (canonical-variant v))
(equal "Topic" (topic-name-value name)) (null (topic-name-scope name))
(equal value (variant-value v)) (equal (iri-datatype) (variant-datatype v))
(scope-eq-p (variant-scope v) (list theme)) (variants-current-p map)))
(check (concatenate 'string label ":resource-does-not-create-topic-identifiers")
(and (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner))
(equal (list (concatenate 'string *document* "#command")) (item-identifiers theme))
(every (lambda (topic) (and (null (topic-subject-locators topic))
(if (eq topic (topic-name-type name))
(and (null (item-identifiers topic)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers topic)))
(null (topic-subject-identifiers topic))))) (topic-map-topics map))
(null (item-identifiers map)) (null (item-identifiers name)) (null (item-identifiers v))
(null (item-reifier name)) (null (item-reifier v))
(topic-maps.tmdm::validate-map-item-identifiers map)))
(setf actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
(check (concatenate 'string label ":source-preserved-pure-deterministic")
(and (equalp raw copy) (equal before (variant-state map))
(equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))
(equal before (variant-state map))))
(let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:uri-data-provenance")))
(check (concatenate 'string label ":source-key-independent-model-and-cxtm")
(and (equalp (variant-model-data map) (variant-model-data other))
(equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*)))))))
(error (condition) (setf c condition)))
(record-original-result case actual expected c)
(push (topic-maps.test-foundation::fields "id" id "documentIRI" *document* "sourceKey" id
"sourceBytes" (length raw) "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
"sourceText" (babel:octets-to-string raw :encoding :utf-8) "resourceText" source-text "absoluteValue" value
"model" (when map (variant-model-data map)) "condition" (negative-condition-data c)
"actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8))
"expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
"actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
"firstDifferentByte" (when actual (mismatch actual expected))
"status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *uri-data-outputs*)
(when c (error c))
(check (concatenate 'string label ":byte-exact") (equalp actual expected)))))
(setf *uri-data-outputs* (nreverse *uri-data-outputs*))
(check "uri-data-originals:60-unique-case-records"
(and (= 60 (length *cases*)) (= 52 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category))
(= 60 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
(defun uri-data-xml (value themes)
(format nil "~A~A" (scope-xml themes) value))
(defun uri-data-value-controls ()
(dolist (row '(("absolute-no-fragment" "https://external.invalid/file.xml" "https://external.invalid/file.xml")
("relative" "../resource.wav" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/resource.wav")
("fragment-not-shorthand" "#not/a/shorthand" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm#not/a/shorthand")
("empty-reference" "" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/example.xtm")
("unicode" "urn:馬籠🦊" "urn:馬籠🦊") ("percent-utf8" "urn:%E9%A6%AC%F0%9F%A6%8A" "urn:馬🦊")
("percent-reserved" "https://external.invalid/a%2Fb%23part" "https://external.invalid/a/b#part")
("XML-escaping-once" "https://external.invalid/?a=1&b=2" "https://external.invalid/?a=1&b=2")
("urn-no-fragment" "urn:external" "urn:external")
("file-no-read" "file:///nonexistent-21i/resource.xml" "file:///nonexistent-21i/resource.xml")))
(destructuring-bind (label href value) row
(variant-control (concatenate 'string "uri-data:" label)
(format nil "~A" (variant-name-xml nil (uri-data-xml href '("s"))))
(lambda (map)
(let* ((owner (by-id map "a")) (name (first (topic-names owner))) (v (first (all-variants map)))
(comparison (read-body (format nil "~A" (variant-name-xml nil (variant-xml "text" '("s")))))))
(check (concatenate 'string "uri-data:value-datatype-parent-no-topic-effects:" label)
(and (equal value (variant-value v)) (equal (iri-datatype) (variant-datatype v))
(eq owner (topic-name-parent name)) (eq name (variant-parent v)) (eq v (canonical-variant v))
(scope-eq-p (variant-scope v) (list (by-id map "s")))
(equalp (original-model-data map) (original-model-data comparison)))))))))
(let ((*document* "https://other.invalid/maps/input.xtm?version=2"))
(variant-control "uri-data:alternate-document"
(format nil "~A" (variant-name-xml nil (uri-data-xml "sound.wav" '("s"))))
(lambda (map) (check "uri-data:alternate-document-absolute-value"
(and (equal "https://other.invalid/maps/sound.wav" (variant-value (first (all-variants map))))
(equal (list (concatenate 'string *document* "#a")) (item-identifiers (by-id map "a"))))))))
(variant-control "uri-data:inherited-scope-duplicates-forward"
(format nil "~A"
(variant-name-xml '("base") (concatenate 'string
(uri-data-xml "urn:sound" '("base" "s" "t" "s")) (uri-data-xml "urn:sound" '("t" "s")))))
(lambda (map) (let* ((name (first (topic-names (by-id map "a")))) (v (first (all-variants map))))
(check "uri-data:scope-union-and-variant-normalization"
(and (= 1 (length (all-variants map))) (equal "urn:sound" (variant-value v))
(equal (iri-datatype) (variant-datatype v)) (eq name (variant-parent v))
(scope-eq-p (topic-name-scope name) (list (by-id map "base")))
(scope-eq-p (variant-scope v) (list (by-id map "base") (by-id map "s") (by-id map "t"))))))))
(variant-control "uri-data:distinct-values-datatypes-scopes-and-normalized-iris"
(format nil "~A" (variant-name-xml nil (concatenate 'string
(uri-data-xml "urn:a" '("s")) (uri-data-xml "urn:%61" '("s"))
(uri-data-xml "urn:b" '("s")) (uri-data-xml "urn:a" '("t")) (variant-xml "urn:a" '("s")))))
(lambda (map)
(check "uri-data:equality-is-value-datatype-scope-not-topic-identity"
(and (= 4 (length (all-variants map))) (= 4 (length (topic-map-topics map)))
(= 3 (count (iri-datatype) (all-variants map) :key #'variant-datatype :test #'equal))
(= 1 (count (string-datatype) (all-variants map) :key #'variant-datatype :test #'equal))))))
(variant-control "uri-data:same-locator-does-not-identify-topics"
(format nil "~A~A"
(variant-name-xml nil (uri-data-xml "urn:shared" '("s")))
(variant-name-xml nil (uri-data-xml "urn:shared" '("s"))))
(lambda (map)
(check "uri-data:resource-identity-separated-from-SI-SL-II"
(and (not (eq (by-id map "a") (by-id map "b"))) (= 4 (length (topic-map-topics map)))
(= 2 (length (all-variants map)))
(equal '("urn:shared") (topic-subject-identifiers (by-id map "a")))
(equal '("urn:shared") (topic-subject-locators (by-id map "b")))
(equal (list (concatenate 'string *document* "#a")) (item-identifiers (by-id map "a")))
(equal (list (concatenate 'string *document* "#b")) (item-identifiers (by-id map "b")))))))
;; One primary XML parse only, even for an unreachable external .xml locator.
(let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (calls 0))
(unwind-protect
(progn
(setf (symbol-function 'topic-maps.xml:parse-octets)
(lambda (&rest args) (incf calls) (apply saved args)))
(read-body (format nil "~A"
(variant-name-xml nil (uri-data-xml "https://unreachable.invalid/external.xml" '("s")))))
(check "uri-data:one-primary-XML-parse-no-recursive-deserialization" (= 1 calls)))
(setf (symbol-function 'topic-maps.xml:parse-octets) saved))))
(defun uri-data-equivalence-controls ()
(let* ((value "relative-resource.wav")
(ref-body (format nil "~A" (variant-name-xml '("base") (resource-ref-xml value '("extra")))))
(data-body (format nil "~A" (variant-name-xml '("base") (uri-data-xml value '("extra")))))
(map (read-body data-body)) (ref-map (read-body ref-body))
(both (read-body (format nil "~A" (variant-name-xml '("base")
(concatenate 'string (resource-ref-xml value '("extra")) (uri-data-xml value '("extra")))))))
(name (first (topic-names (by-id both "a")))) (v (first (all-variants both)))
(expected "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/relative-resource.wav"))
(check "uri-data:resourceRef-equivalent-native-model-value-type-scope-and-cxtm"
(and (equalp (variant-model-data map) (variant-model-data ref-map))
(equalp (variant-model-data both) (variant-model-data ref-map))
(equal expected (variant-value v)) (equal (iri-datatype) (variant-datatype v))
(= 4 (length (topic-map-topics both))) (= 1 (length (all-variants both)))
(eq v (canonical-variant v)) (eq name (variant-parent v))
(scope-eq-p (variant-scope v) (list (by-id both "base") (by-id both "extra")))
(equalp (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)
(topic-maps.cxtm:canonicalize-octets ref-map :base-locator *document*))
(equalp (topic-maps.cxtm:canonicalize-octets both :base-locator *document*)
(topic-maps.cxtm:canonicalize-octets ref-map :base-locator *document*))))
(push (topic-maps.test-foundation::fields "label" "uri-data:resourceRef-equivalence"
"kind" "synthetic-equivalence-not-original" "resourceRefSource" (document ref-body)
"resourceDataSource" (document data-body) "absoluteValue" expected "model" (variant-model-data both)
"actualCXTM" (babel:octets-to-string (topic-maps.cxtm:canonicalize-octets both :base-locator *document*) :encoding :utf-8)) *variant-controls*))
(let ((old-name nil) (old-variant nil) (*name-controls* nil))
(staged-name-control "uri-data:mixed-forms-retained-handles"
(format nil "~A~A"
(variant-name-xml '("base") (resource-ref-xml "sound.wav" '("extra")))
(variant-name-xml '("base") (uri-data-xml "sound.wav" '("extra"))))
(lambda (map step)
(when (= step 1)
(setf old-name (first (topic-names (by-id map "a"))) old-variant (first (all-variants map)))))
(lambda (map)
(let* ((owner (by-id map "a")) (name (first (topic-names owner))) (v (first (all-variants map))))
(check "uri-data:mixed-forms-canonical-parent-name-variant-eq"
(and (eq owner (by-id map "b")) (= 1 (length (all-variants map)))
(eq name (canonical-topic-name old-name)) (eq owner (topic-name-parent name))
(eq v (canonical-variant old-variant)) (eq name (variant-parent old-variant))
(equal (iri-datatype) (variant-datatype v)) (variants-current-p map)))
(push (topic-maps.test-foundation::fields "label" "uri-data:mixed-forms-retained-handles"
"kind" "staged-native-reader-control" "model" (variant-model-data map)) *variant-controls*))))))
(defun uri-data-error-controls ()
(dolist (row '(("invalid-iri" "bad space" :invalid-iri)
("leading-space-not-trimmed" " urn:x" :invalid-iri)
("trailing-space-not-trimmed" "urn:x " :invalid-iri)
("tab-not-trimmed" "urn:x " :invalid-iri)
("invalid-decoded-iri" "urn:x%00" :invalid-iri)
("invalid-relative-iri" "1bad:relative" :invalid-iri)
("percent-short" "urn:x%2" :invalid-percent-escape)
("percent-hex" "urn:x%GG" :invalid-percent-escape)
("percent-incomplete-utf8" "urn:x%C3" :invalid-percent-utf8)
("percent-overlong-utf8" "urn:x%C0%AF" :invalid-percent-utf8)
("percent-surrogate" "urn:x%ED%A0%80" :invalid-percent-utf8)))
(variant-error-control (concatenate 'string "uri-data:" (first row))
(format nil "~A" (variant-name-xml nil (uri-data-xml (second row) '("s"))))
'xtm-rejection (third row)))
(dolist (row '(("extra-attribute" "urn:x" :unexpected-attribute)
("relative-datatype" "urn:x" :invalid-resource-data-datatype)
("empty-datatype" "urn:x" :invalid-resource-data-datatype)
("invalid-datatype-scheme" "urn:x" :invalid-resource-data-datatype)
("invalid-datatype-IRI" "urn:x" :invalid-iri)
("invalid-datatype-percent" "urn:x" :invalid-percent-escape)
("foreign-child" "urn:x" :unexpected-child)
("XTM-child" "urn:x" :unexpected-child)))
(variant-error-control (concatenate 'string "uri-data:" (first row))
(format nil "N~A" (second row))
'xtm-rejection (third row)))
(dolist (row '(("mixed-resources" "urn:x" :variant-resource-cardinality)
("multiple-data" "urn:xurn:y" :variant-resource-cardinality)
("wrong-order" "urn:x" :missing-variant-scope)
("scope-after-resource" "urn:x" :element-order)))
(variant-error-control (concatenate 'string "uri-data:" (first row))
(format nil "N~A" (second row)) 'xtm-rejection (third row)))
(dolist (row '(("anyType-text-still-unsupported" "42")
("anyType-still-unsupported" "XML")))
(variant-error-control (concatenate 'string "uri-data:" (first row))
(format nil "N~A" (second row))
'xtm-unsupported :resource-resolver-required)))
(defun uri-data-scope-controls ()
(dolist (row '(("equal-scope" ("base")) ("duplicate-scope-no-new-theme" ("base" "base"))))
(variant-error-control (concatenate 'string "uri-data:" (first row))
(format nil "~A" (variant-name-xml '("base") (uri-data-xml "urn:x" (second row))))
'variant-error :invalid-variant-scope))
(variant-error-control "uri-data:SI-alias-adds-no-theme"
"Nurn:x"
'variant-error :invalid-variant-scope)
(variant-error-control "uri-data:forward-merge-collapses-proper-scope"
(format nil "~A"
(variant-name-xml '("base") (uri-data-xml "urn:x" '("extra")))) 'variant-error :variant-scope-collapse)
(let* ((key "uri-data:invalid-raw-UTF8")
(raw (concatenate '(vector (unsigned-byte 8))
(bytes "Nurn:")
#(255) (bytes "")))
(c (condition-from (lambda () (deserialize-octets raw :document-iri *document* :source-key key)))))
(check "uri-data:invalid-raw-UTF8-is-XML-error"
(and (eq 'xtm-xml-error (type-of c)) (eq :xml-not-well-formed (error-code c))
(equal key (error-source-key c)) (equal *document* (error-document-iri c))))
(push (topic-maps.test-foundation::fields "label" key "kind" "invalid-XML-octets"
"sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "condition" (negative-condition-data c)) *variant-controls*))
(dolist (kind '(:subject-identifier :subject-locator :item-identifier))
(let* ((element (ecase kind (:subject-identifier "subjectIdentifier") (:subject-locator "subjectLocator") (:item-identifier "itemIdentity")))
(body (format nil "<~A href='urn:base'/>~A"
element element (variant-name-xml '("base") (uri-data-xml "urn:resource" '("extra")))))
(map (read-body body)) (v (first (all-variants map))) (before (variant-state map))
(c (condition-from (lambda () (add-topic-identifier (by-id map "extra") kind "urn:base")))))
(check (format nil "uri-data:scope-collapse-~A-specific-atomic-eq" kind)
(and (eq 'variant-error (type-of c)) (eq :variant-scope-collapse (variant-error-code c))
(equal before (variant-state map)) (eq v (canonical-variant v)) (variants-current-p map)))
(push (topic-maps.test-foundation::fields "label" (format nil "uri-data:collapse-~A" kind)
"kind" "native-TMDM-collapse-atomicity" "sourceText" (document body)
"condition" (negative-condition-data c) "modelUnchanged" :true "model" (variant-model-data map)) *variant-controls*))))
(defun uri-data-tests ()
(let ((*variant-controls* nil))
(uri-data-value-controls)
(uri-data-equivalence-controls)
(variant-merge-controls (lambda (themes) (uri-data-xml "urn:resource" themes)) "uri-data-merge:"
(lambda (label map)
(check (concatenate 'string label ":absolute-resource-value-and-datatype")
(every (lambda (v) (and (equal "urn:resource" (variant-value v))
(equal (iri-datatype) (variant-datatype v)))) (all-variants map)))))
(uri-data-error-controls)
(uri-data-scope-controls)
(setf *uri-data-controls* (nreverse *variant-controls*))))