(in-package #:topic-maps.xtm.tests) (defvar *ss-retargets* nil) (defvar *ss-results* nil) (defvar *ss-controls* nil) (defvar *ss-replays* nil) (defvar *ss-executed* nil) (defun ss-unsupported-source (source code label) "Explicit 22S control retarget: keep the Unsupported classification/diagnostic check, using a valid mergeMap without a resolver. The old anyType source is retained and replayed separately; no production error is synthesized." (if (and source (member code '(:resource-resolver-required :cyclic-mergemap)) (search "http://www.w3.org/2001/XMLSchema#anyType" source)) (let* ((end (position #\> source)) (new (concatenate 'string (subseq source 0 (1+ end)) (if (eq code :cyclic-mergemap) "" "") (subseq source (1+ end))))) (assert (and end (search "~A~A~A" (if reifier (format nil " reifier='~A'" reifier) "") (if id (format nil "" id) "") (if scope (scope-xml scope) "") resource)) (defun ss-data (content &optional (datatype "http://www.w3.org/2001/XMLSchema#anyType")) (format nil "~A" (if datatype (format nil " datatype='~A'" datatype) "") content)) (defun ss-values (map) (append (loop for tpc in (topic-map-topics map) append (loop for o in (topic-occurrences tpc) collect (list "occurrence" (occurrence-value o) (occurrence-datatype o)))) (loop for tpc in (topic-map-topics map) append (loop for n in (topic-names tpc) append (loop for v in (topic-name-variants n) collect (list "variant" (variant-value v) (variant-datatype v))))))) (defun ss-positive (id source expected &key (encoding :utf-8) bom verify resolver (doc "https://anytype.example.invalid/root.xtm")) (let* ((raw (bytes source encoding bom)) (copy (copy-seq raw)) (map (deserialize-octets raw :document-iri doc :source-key "same-key-is-not-document-identity" :resource-resolver resolver)) (native (ss-values map)) (state (ik-state map)) (actual (mm-cxtm map doc)) (again (mm-cxtm map doc))) (check (format nil "anytype:~A:native-values-and-datatypes" id) (and (= (length expected) (length native)) (every (lambda (row) (= (count row expected :test #'equal) (count row native :test #'equal))) native))) (check (format nil "anytype:~A:all-canonical-EQ-references" id) (mm-current-p map)) (check (format nil "anytype:~A:expected-native-occurrence-parent-type-scope-reifier" id) (every (lambda (o) (let* ((parent (occurrence-parent o)) (origin (cond ((eq parent (mm-local map doc "owner")) doc) ((and (equal id "mergeMap-separate-original-document-contexts") (eq parent (mm-local map "https://anytype.example.invalid/child.xtm" "child"))) "https://anytype.example.invalid/child.xtm")))) (and origin (eq (occurrence-type o) (mm-local map origin "type")) (if (equal id "metadata-variant-and-occurrence") (and (scope-eq-p (list (mm-local map doc "base") (mm-local map doc "extra")) (occurrence-scope o)) (eq (item-reifier o) (mm-local map doc "orr"))) (and (null (occurrence-scope o)) (null (item-reifier o))))))) (all-occurrences map))) (when (search "22Q/" id) (check (format nil "anytype:~A:no-topics-from-XML-names" id) (= 2 (length (topic-map-topics map))))) (when verify (funcall verify map)) (check (format nil "anytype:~A:source-and-model-pure-repeat-CXTM" id) (and (equalp raw copy) (equalp actual again) (equal state (ik-state map)))) (let* ((other (deserialize-octets raw :document-iri doc :source-key "different-diagnostic-key" :resource-resolver resolver)) (cx (mm-cxtm other doc))) (check (format nil "anytype:~A:source-key-independent-deterministic-reader" id) (equalp actual cx))) (push (mm-fields "id" id "inputHex" (cr-hex raw) "inputXML" source "encoding" (string encoding) "documentIRI" doc "sourceKey" "same-key-is-not-document-identity" "expectedValues" (coerce (mapcar (lambda (v) (coerce v 'vector)) expected) 'vector) "actualValues" (coerce (mapcar (lambda (v) (coerce v 'vector)) native) 'vector) "nativeModel" (occurrence-model map) "canonicalReferencesEQ" :true "repeatedCXTMPure" :true "actualCXTM" (babel:octets-to-string actual :encoding :utf-8) "actualCXTMHex" (cr-hex actual)) *ss-results*) map)) (defun ss-error (id source class code &optional thunk) (let ((c (condition-from (or thunk (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key id)))))) (check (format nil "anytype-error:~A:specific-class-code" id) (and (typep c class) (eq (error-code c) code))) (check (format nil "anytype-error:~A:actual-source-and-cause" id) (and (equal (error-document-iri c) *document*) (equal (error-source-key c) id) (if (eq code :relative-namespace-uri) (and (typep (topic-maps.xtm:error-cause c) 'topic-maps.xml:canonicalization-error) (null (error-line c)) (null (error-column c))) t))) (push (mm-fields "id" id "inputXML" source "condition" (mm-condition c)) *ss-controls*) c)) (defun ss-tests () (setf *ss-results* nil *ss-controls* nil *ss-replays* nil *ss-executed* nil) (let ((dt "http://www.w3.org/2001/XMLSchema#anyType")) ;; Reuse independent, fixed 22Q expectations through full productive Reader ;; documents. Root uses an explicit XTM prefix so embedded unprefixed XML ;; keeps the same expanded names as those fixed inputs. (dolist (spec (cq-specs)) (destructuring-bind (id resource want rule) spec (declare (ignore rule)) (ss-positive (concatenate 'string "22Q/" id) (format nil "~A" resource) (list (list "occurrence" want dt))))) (dolist (enc '(:utf-8 :utf-16le :utf-16be)) (ss-positive (format nil "unicode/~A" enc) (document (format nil "~A" (ss-occ (ss-data "Grüße 水 🦊")))) (list (list "occurrence" "Grüße 水 🦊" dt)) :encoding enc :bom (not (eq enc :utf-8)))) (ss-positive "mixed-datatypes-and-foreign-resourceData" (document (format nil "~A~A~A~A~A~A~A" (ss-occ (ss-data "")) (ss-occ (ss-data "literal" nil)) (ss-occ (ss-data "../asset" (iri-datatype))) (ss-occ (ss-data " 007 " "urn:custom")) (ss-occ (ss-data "")) (ss-occ (ss-data " A&B<C>" ")) (ss-occ ""))) (list (list "occurrence" "" dt) (list "occurrence" "literal" (string-datatype)) (list "occurrence" "https://anytype.example.invalid/asset" (iri-datatype)) (list "occurrence" " 007 " "urn:custom") (list "occurrence" "" dt) (list "occurrence" " A&B<C>\" " dt) (list "occurrence" "https://anytype.example.invalid/ref" (iri-datatype)))) (ss-positive "metadata-variant-and-occurrence" (document (format nil "~AN~A" (ss-occ (ss-data "") :id "urn:o" :reifier "#orr" :scope '("base" "extra")) (ss-data "V"))) (list (list "occurrence" "" dt) (list "variant" "V" dt)) :verify (lambda (map) (let* ((owner (mm-local map "https://anytype.example.invalid/root.xtm" "owner")) (o (first (topic-occurrences owner))) (name (first (topic-names owner))) (v (first (topic-name-variants name))) (doc "https://anytype.example.invalid/root.xtm") (base (mm-local map doc "base")) (extra (mm-local map doc "extra"))) (check "anytype:metadata:identifiers-reifiers-parents-types-scopes-EQ" (and (equal '("urn:o") (item-identifiers o)) (equal '("urn:v") (item-identifiers v)) (eq o (topic-reified (mm-local map doc "orr"))) (eq v (topic-reified (mm-local map doc "vr"))) (eq owner (occurrence-parent o)) (eq name (variant-parent v)) (eq owner (topic-name-parent name)) (eq (occurrence-type o) (mm-local map doc "type")) (scope-eq-p (list base extra) (occurrence-scope o)) (scope-eq-p (list base extra) (variant-scope v))))))) (ss-positive "lexical-equivalence-native-normalization" (document (format nil "~A~A" (ss-occ (ss-data "") :id "urn:first") (ss-occ (ss-data "a&b") :id "urn:second"))) (list (list "occurrence" "a&b" dt)) :verify (lambda (map) (let ((o (first (all-occurrences map)))) (check "anytype:normalization:one-occurrence-and-identifier-union" (and (= 1 (length (all-occurrences map))) (same-identifiers-p '("urn:first" "urn:second") (item-identifiers o))))))) (let* ((child "https://anytype.example.invalid/child.xtm") (raw (bytes (document (format nil "~A" (ss-occ (ss-data "C")))) :utf-16le t)) (calls nil) (resolver (lambda (iri &rest args) (push (cons iri args) calls) (assert (equal iri child)) (values raw "same-key-is-not-document-identity")))) (ss-positive "mergeMap-separate-original-document-contexts" (document (format nil "~A" (ss-occ (ss-data "P")))) (list (list "occurrence" "P" dt) (list "occurrence" "C" dt)) :resolver resolver :verify (lambda (map) (check "anytype:mergeMap:child-IRI-local-topics-and-exact-resolver" (and (mm-local map child "child") (equal (first calls) (list child :purpose :mergemap :from-document-iri "https://anytype.example.invalid/root.xtm" :notation "http://psi.topicmaps.org/iso13250/xtm"))))))) (dolist (row (list (list "malformed" "" 'xtm-xml-error :xml-not-well-formed) (list "unbound-prefix" (document (format nil "~A" (ss-occ (ss-data "")))) 'xtm-xml-error :xml-not-well-formed) (list "relative-namespace" (document (format nil "~A" (ss-occ (ss-data "")))) 'xtm-xml-error :relative-namespace-uri) (list "DOCTYPE" (concatenate 'string "" (document "")) 'xtm-xml-error :doctype-forbidden) (list "external-entity" (concatenate 'string "]>" (document "&secret;")) 'xtm-xml-error :doctype-forbidden) (list "outside-markup" (document "") 'xtm-rejection :invalid-namespace) (list "nested-XTM-resourceData" (document (format nil "~A" (ss-occ (ss-data "X")))) 'xtm-rejection :embedded-xtm-element))) (apply #'ss-error row)) (dolist (datatype (list nil (string-datatype) (iri-datatype) "urn:custom")) (ss-error (format nil "textual-datatype-disallows-markup/~A" datatype) (document (format nil "~A" (ss-occ (ss-data "" datatype)))) 'xtm-rejection :unexpected-child)) ;; Deliberately corrupt only the private Reader association, never C14N. (dolist (mode '(wrong-index foreign-node foreign-frame wrong-document changed-octets inventory-mismatch)) (let* ((source (document (format nil "~A~A" (ss-occ (ss-data "")) (ss-occ (ss-data ""))))) (prepare (symbol-function 'topic-maps.xtm::prepare-xml-resources)) (count (symbol-function 'topic-maps.xml:resource-data-count)) (id (format nil "association/~A" mode))) (unwind-protect (progn (if (eq mode 'inventory-mismatch) (setf (symbol-function 'topic-maps.xml:resource-data-count) (lambda (ctx) (1+ (funcall count ctx)))) (setf (symbol-function 'topic-maps.xtm::prepare-xml-resources) (lambda (root original snapshot) (let* ((b (funcall prepare root original snapshot)) (node (aref (topic-maps.xtm::xml-resource-binding-nodes b) 0))) (case mode (wrong-index (setf (gethash node (topic-maps.xtm::xml-resource-binding-indices b)) 1)) (foreign-node (remhash node (topic-maps.xtm::xml-resource-binding-indices b))) (wrong-document (setf (topic-maps.xtm::xml-resource-binding-document-iri b) "urn:foreign")) (changed-octets (setf (aref snapshot 0) 0))) (if (eq mode 'foreign-frame) (funcall prepare (topic-maps.xml:parse-octets original (make-instance 'topic-maps.xtm::source-handler)) original snapshot) b))))) (ss-error id source 'xtm-xml-error :resource-data-association)) (setf (symbol-function 'topic-maps.xtm::prepare-xml-resources) prepare (symbol-function 'topic-maps.xml:resource-data-count) count)))) ;; Independently retain the old classification controls' original sources and ;; observe their new Reader outcome instead of continuing to call them ;; Unsupported. These are supplementary observations, not corpus contracts. (dolist (r *ss-retargets*) (let* ((f (topic-maps.test-foundation::json-object-members r)) (source (cdr (assoc "sourceBefore" f :test #'equal))) (map nil) (c (condition-from (lambda () (setf map (deserialize-octets (bytes source) :document-iri *document* :source-key "22s:former-unsupported-source")))))) (check (format nil "anytype-replay:~D:no-longer-anyType-Unsupported" (length *ss-replays*)) (not (typep c 'xtm-unsupported))) (push (mm-fields "sourceText" source "condition" (mm-condition c) "nativeValues" (when map (coerce (mapcar (lambda (v) (coerce v 'vector)) (ss-values map)) 'vector)) "actualCXTM" (when map (babel:octets-to-string (mm-cxtm map) :encoding :utf-8))) *ss-replays*))) (setf *ss-results* (nreverse *ss-results*) *ss-controls* (nreverse *ss-controls*) *ss-replays* (nreverse *ss-replays*) *ss-retargets* (nreverse *ss-retargets*) *ss-executed* t)) t) (defun ss-report () (mm-fields "executed" (if *ss-executed* :true :false) "XTMReaderAnyTypeIntegration" (if *ss-executed* :true :false) "originalRegistrationsAdded" 0 "positiveProbes" (coerce *ss-results* 'vector) "negativeProbes" (coerce *ss-controls* 'vector) "retargetings" (coerce *ss-retargets* 'vector) "formerSourceReplays" (coerce *ss-replays* 'vector)))