(in-package #:topic-maps.xtm.tests) ;; Characterization only: use the existing walker/sink without implementing ;; namespace repair, node-set projection, sorting, escaping or C14N ourselves. (defvar *cq-results* nil) (defvar *cq-native* nil) (defvar *cq-api* nil) (defparameter *cq-known-matches* nil) ; frozen only after outside-repository run (defun cq-wrap (content &optional (attributes "")) (format nil "~A" attributes content)) (defun cq-specs () (list (list "simple" (cq-wrap "Hello") "Hello" "XTM 4.16.3; C14N 2.3: element/text, omit container") (list "attributes-ba" (cq-wrap "") "" "C14N 2.3: unqualified attribute order and expanded empty element") (list "attributes-ab" (cq-wrap "") "" "Same node properties as attributes-ba") (list "inherited-prefix" (cq-wrap "A" " xmlns:p='urn:p' xmlns:u='urn:unused'") "A" "XTM 4.16.3: needed inherited prefix retained; unused mapping removed") (list "unused-local-namespace" (cq-wrap "A") "A" "XTM 4.16.3: unused namespace absent from selected namespace set") (list "inherited-default" (cq-wrap "A" " xmlns='urn:default'") "A" "C14N 2.3-2.4: selected child must retain inherited default namespace") (list "nested" (cq-wrap "Z") "Z" "C14N 2.3: nested elements, attributes, empty element") (list "escaping" (cq-wrap "A&B<C>" ") (format nil "A&B<C>\"~C~C " #\Tab #\Newline) "C14N 2.3: attribute vs text escapes, hexadecimal character references") (list "whitespace-line-endings" (cq-wrap (format nil " a~C~C b~C c~C~C" #\Return #\Newline #\Return #\Tab #\Newline)) (format nil " a~% b~% c~C~%" #\Tab) "XML 2.11; C14N 2.1/2.3: normalize CR/CRLF, retain text whitespace") (list "comments" (cq-wrap "AB") "AB" "XTM 4.16.3: comments=false") (list "lexical-cdata" (cq-wrap "") "a&b" "C14N 2.1/2.3: references, CDATA and tag spelling have equal selected values") (list "lexical-escaped" (cq-wrap "a&b") "a&b" "Same selected node properties as lexical-cdata") (list "unicode" (cq-wrap "Grüße 水 🦊") "Grüße 水 🦊" "C14N 2.1: strict UTF-8 output for scalar character data") (list "processing-instruction" (cq-wrap "AB") "AB" "XTM selects element/attribute/character nodes, not PI nodes") (list "namespace-attribute-order" (cq-wrap "") "" "C14N 2.3: namespace nodes before attributes, attributes by namespace URI/local name") (list "redundant-binding" (cq-wrap "") "" "C14N 2.3: suppress already rendered identical namespace binding"))) (defun cq-parse-dom (raw) ;; Diagnostic-only CXML configuration. The productive XML wrapper remains ;; unchanged and is NOT claimed compatible with DOM. Same closed-handler, ;; no DOCTYPE, no resolver fallback; DOM requires xmlns attributes and URIs. (assert (equal (symbol-value 'cxml::*topic-maps-encoding-patch*) "19d0b-1")) (assert (equal (symbol-value 'runes-encoding::*topic-maps-encoding-patch*) "19d0b-1")) (let ((sax:*namespace-processing* t) (sax:*include-xmlns-attributes* t) (sax:*use-xmlns-namespace* t) (cxml:*catalog* nil) (cxml:*cache-all-dtds* nil) (cxml:*dtd-cache* (cxml:make-dtd-cache))) (cxml:parse-octets raw (make-instance 'topic-maps.xml::closed-handler :chained-handler (cxml-dom:make-dom-builder)) :validate nil :disallow-internal-subset t :entity-resolver #'topic-maps.xml::deny-external-resource))) (defun cq-condition (c) (when c (mm-fields "class" (string (type-of c)) "detail" (princ-to-string c)))) (defun cq-write (path value) (with-open-file (s path :direction :output :if-exists :supersede :if-does-not-exist :create) (topic-maps.test-foundation::emit-json-value value s) (terpri s))) (defun cq-native-probe (id value) (let* ((map (make-topic-map)) (owner (intern-topic map :item-identifier "urn:cq:owner")) (type (intern-topic map :subject-identifier "urn:cq:type")) (name-type (intern-topic map :subject-identifier "urn:cq:name-type")) (base (intern-topic map :subject-identifier "urn:cq:base")) (theme (intern-topic map :subject-identifier "urn:cq:extra")) (name (add-name owner "N" :type name-type :scope (list base))) (count (length (topic-map-topics map))) (datatype "http://www.w3.org/2001/XMLSchema#anyType") (o (add-occurrence owner type value :datatype datatype :scope (list theme))) (v (add-variant name value :datatype datatype :scope (list base theme))) (before (ik-state map)) (a (topic-maps.cxtm:canonicalize-octets map :base-locator "urn:cq:base-map")) (b (topic-maps.cxtm:canonicalize-octets map :base-locator "urn:cq:base-map"))) (check (format nil "c14n-native:~A:occurrence-value-datatype-parent-EQ" id) (and (equal value (occurrence-value o)) (equal datatype (occurrence-datatype o)) (eq owner (occurrence-parent o)) (eq type (occurrence-type o)) (eq o (canonical-occurrence o)))) (check (format nil "c14n-native:~A:variant-value-datatype-parent-EQ" id) (and (equal value (variant-value v)) (equal datatype (variant-datatype v)) (eq name (variant-parent v)) (eq v (canonical-variant v)))) (check (format nil "c14n-native:~A:scopes-and-no-topics-from-XML" id) (and (= count (length (topic-map-topics map))) (scope-eq-p (list theme) (occurrence-scope o)) (scope-eq-p (list base theme) (variant-scope v)))) (check (format nil "c14n-native:~A:repeated-CXTM-preserves-all-properties" id) (and (equalp a b) (equal before (ik-state map)) (equal value (occurrence-value o)) (equal value (variant-value v)))) (push (mm-fields "id" id "origin" "actual candidate bytes equal the independent oracle for this probe only" "readerIntegration" :false "datatype" datatype "occurrenceValue" (occurrence-value o) "variantValue" (variant-value v) "topicCountBefore" count "topicCountAfter" (length (topic-map-topics map)) "canonicalParentTypeScopeEQ" :true "repeatedCXTMIdentical" :true "modelUnchanged" :true "actualCXTM" (babel:octets-to-string a :encoding :utf-8)) *cq-native*))) (defun cq-probe (spec &optional (encoding :utf-8) bom) (destructuring-bind (label source expected rule) spec (let* ((id (if (eq encoding :utf-8) label (format nil "~A/~A" label encoding))) (raw (bytes source encoding bom)) (copy (copy-seq raw)) (dom (cq-parse-dom raw)) (container (dom:document-element dom)) ;; Existing DOM child walker, applied experimentally to a container. ;; No custom XML serialization/C14N, no namespace repair/filter. (actual (dom:map-document (cxml:make-octet-vector-sink :canonical t) container :include-xmlns-attributes t)) (second-form (dom:map-document (cxml:make-octet-vector-sink :canonical 2) container :include-xmlns-attributes t)) (whole (dom:map-document (cxml:make-octet-vector-sink :canonical t) dom :include-xmlns-attributes t)) (want (bytes expected)) (match (equalp actual want)) (string (babel:octets-to-string actual :encoding :utf-8 :errorp t))) (check (format nil "c14n-probe:~A:input-bytes-unchanged" id) (equalp raw copy)) (check (format nil "c14n-probe:~A:UTF8-string-roundtrip" id) (equalp actual (bytes string))) (when *cq-known-matches* (check (format nil "c14n-characterization:~A:observed-match-or-limit" id) (eq match (not (null (member label *cq-known-matches* :test #'equal)))))) (push (mm-fields "id" id "inputXML" source "inputEncoding" (string encoding) "inputHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "API" "diagnostic CXML DOM configuration with closed-handler/DOCTYPE denial; dom:map-document canonical sink on container child nodes" "candidateOption" "cxml:make-octet-vector-sink :canonical T" "subsetAPIQualified" :false "expectedXML" expected "expectedHex" (format nil "~{~2,'0X~}" (coerce want 'list)) "normativeReason" rule "actualXML" string "actualHex" (format nil "~{~2,'0X~}" (coerce actual 'list)) "expectedBytes" (length want) "actualBytes" (length actual) "firstDifferentByte" (mismatch want actual) "normativeComparison" (if match "PASS-for-this-probe" "BYTE-DIFFERENCE") "W3C-C14N-1.0-qualified" :false "secondCanonicalFormXML" (babel:octets-to-string second-form :encoding :utf-8) "secondCanonicalFormMatches" (if (equalp second-form want) :true :false) "wholeDocumentCanonicalXML" (babel:octets-to-string whole :encoding :utf-8) "condition" nil) *cq-results*) (when match (cq-native-probe id string))))) (defun cq-tests () (setf *cq-results* nil *cq-native* nil *cq-api* nil) (let ((c (condition-from (lambda () (topic-maps.xml:parse-octets (bytes "") (cxml-dom:make-dom-builder)))))) (check "c14n-api:productive-boundary-DOM-incompatibility-observed" (and c (search "SAX configuration is incompatible with DOM" (princ-to-string c)))) (push (mm-fields "id" "productive-byte-boundary-DOM" "condition" (cq-condition c)) *cq-api*)) (dolist (text '("" "")) (let ((c (condition-from (lambda () (cq-parse-dom (bytes text)))))) (check (format nil "c14n-api:diagnostic-DOM-DOCTYPE-denied:~A" text) (typep c 'topic-maps.xml:doctype-forbidden)) (push (mm-fields "id" text "condition" (cq-condition c)) *cq-api*))) (dolist (spec (cq-specs)) (cq-probe spec)) (dolist (encoding '(:utf-16le :utf-16be)) (let* ((spec (copy-list (find "unicode" (cq-specs) :key #'first :test #'equal)))) (setf (second spec) (concatenate 'string "" (second spec))) (cq-probe spec encoding t))) (dolist (pair (list (list "sink-has-no-node-set-keyword" (lambda () (cxml:make-octet-vector-sink :canonical t :node-set nil))) (list "walker-has-no-node-set-keyword" (lambda () (let ((doc (cq-parse-dom (bytes "")))) (dom:map-document (cxml:make-octet-vector-sink :canonical t) doc :node-set nil)))))) (let ((c (condition-from (second pair)))) (check (concatenate 'string "c14n-api:" (first pair)) (typep c 'program-error)) (push (mm-fields "id" (first pair) "condition" (cq-condition c)) *cq-api*))) (setf *cq-results* (nreverse *cq-results*) *cq-native* (nreverse *cq-native*) *cq-api* (nreverse *cq-api*)) (let ((path (uiop:getenv "TOPIC_MAPS_C14N_REPORT"))) (when path (cq-write path (mm-fields "kind" "slice-22q-existing-candidate-characterization-not-reader-support" "W3CC14NQualified" :false "XTMReaderIntegration" :false "ownC14NImplementation" :false "probes" (coerce *cq-results* 'vector) "nativeValues" (coerce *cq-native* 'vector) "APILimits" (coerce *cq-api* 'vector) "checks" (coerce (reverse *checks*) 'vector))))) t)