(in-package #:topic-maps.xtm.tests) (defparameter *resource-ref-original-specs* '(("variant-resourceref" "http://example.org/command.wav" "http://example.org/command.wav" 621) ("variant-resourceref-relative" "topic-command.wav" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/in/topic-command.wav" 608))) (defun resource-ref-xml (href themes) (format nil "~A" (scope-xml themes) href)) (defun resource-ref-original-contracts () (setf *resource-ref-outputs* nil) (dolist (spec *resource-ref-original-specs*) (destructuring-bind (stem href 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 "resource-ref-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-href") (and (equal "resourceRef" (topic-maps.xtm::node-name resource)) (equal href (topic-maps.xtm::attr resource "href")) (= 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:resource-ref-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) "href" href "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)))) *resource-ref-outputs*) (when c (error c)) (check (concatenate 'string label ":byte-exact") (equalp actual expected))))) (setf *resource-ref-outputs* (nreverse *resource-ref-outputs*)) (check "resource-ref-originals:59-unique-case-records" (and (= 59 (length *cases*)) (= 51 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)) (= 59 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))) (defun resource-ref-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 "resource-ref:" label) (format nil "~A" (variant-name-xml nil (resource-ref-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 "resource-ref: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 "resource-ref:alternate-document" (format nil "~A" (variant-name-xml nil (resource-ref-xml "sound.wav" '("s")))) (lambda (map) (check "resource-ref: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 "resource-ref:inherited-scope-duplicates-forward" (format nil "~A" (variant-name-xml '("base") (concatenate 'string (resource-ref-xml "urn:sound" '("base" "s" "t" "s")) (resource-ref-xml "urn:sound" '("t" "s"))))) (lambda (map) (let* ((name (first (topic-names (by-id map "a")))) (v (first (all-variants map)))) (check "resource-ref: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 "resource-ref:distinct-values-datatypes-scopes-and-normalized-iris" (format nil "~A" (variant-name-xml nil (concatenate 'string (resource-ref-xml "urn:a" '("s")) (resource-ref-xml "urn:%61" '("s")) (resource-ref-xml "urn:b" '("s")) (resource-ref-xml "urn:a" '("t")) (variant-xml "urn:a" '("s"))))) (lambda (map) (check "resource-ref: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 "resource-ref:same-locator-does-not-identify-topics" (format nil "~A~A" (variant-name-xml nil (resource-ref-xml "urn:shared" '("s"))) (variant-name-xml nil (resource-ref-xml "urn:shared" '("s")))) (lambda (map) (check "resource-ref: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 (resource-ref-xml "https://unreachable.invalid/external.xml" '("s"))))) (check "resource-ref:one-primary-XML-parse-no-recursive-deserialization" (= 1 calls))) (setf (symbol-function 'topic-maps.xml:parse-octets) saved)))) (defun resource-ref-error-controls () (dolist (row '(("missing-href" "" :missing-attribute) ("extra-attribute" "" :unexpected-attribute) ("namespaced-attribute" "" :unexpected-attribute) ("child" "" :unexpected-child) ("text" "text" :unexpected-text) ("invalid-iri" "" :invalid-iri) ("invalid-decoded-iri" "" :invalid-iri) ("invalid-relative-iri" "" :invalid-iri) ("percent-short" "" :invalid-percent-escape) ("percent-hex" "" :invalid-percent-escape) ("percent-incomplete-utf8" "" :invalid-percent-utf8) ("percent-overlong-utf8" "" :invalid-percent-utf8) ("percent-surrogate" "" :invalid-percent-utf8))) (variant-error-control (concatenate 'string "resource-ref:" (first row)) (format nil "N~A" (second row)) 'xtm-rejection (third row))) (dolist (row '(("mixed-ref-data" "V" :variant-resource-cardinality) ("multiple-ref" "" :variant-resource-cardinality) ("wrong-order" "" :missing-variant-scope) ("scope-after-resource" "" :element-order))) (variant-error-control (concatenate 'string "resource-ref:" (first row)) (format nil "N~A" (second row)) 'xtm-rejection (third row))) (dolist (row '(("equal-scope" ("base")) ("duplicate-scope-no-new-theme" ("base" "base")))) (variant-error-control (concatenate 'string "resource-ref:" (first row)) (format nil "~A" (variant-name-xml '("base") (resource-ref-xml "urn:x" (second row)))) 'variant-error :invalid-variant-scope)) (variant-error-control "resource-ref:SI-alias-adds-no-theme" "N" 'variant-error :invalid-variant-scope) (variant-error-control "resource-ref:forward-merge-collapses-proper-scope" (format nil "~A" (variant-name-xml '("base") (resource-ref-xml "urn:x" '("extra")))) 'variant-error :variant-scope-collapse) (let* ((key "resource-ref:invalid-raw-UTF8") (raw (concatenate '(vector (unsigned-byte 8)) (bytes "N"))) (c (condition-from (lambda () (deserialize-octets raw :document-iri *document* :source-key key))))) (check "resource-ref: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 href='urn:extra'/>~A" element element (variant-name-xml '("base") (resource-ref-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 "resource-ref: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 "resource-ref: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 resource-ref-tests () (let ((*variant-controls* nil)) (resource-ref-value-controls) (variant-merge-controls (lambda (themes) (resource-ref-xml "urn:resource" themes)) "resource-ref-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))))) (resource-ref-error-controls) (setf *resource-ref-controls* (nreverse *variant-controls*))))