]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - reports/slice-22q/exploratory.lisp
examples: explore project work through native Topic Maps (slice 22U)
[topic-maps.git] / reports / slice-22q / exploratory.lisp
1 (in-package #:topic-maps.xtm.tests)
2 ;; Characterization only: use the existing walker/sink without implementing
3 ;; namespace repair, node-set projection, sorting, escaping or C14N ourselves.
4 (defvar *cq-results* nil)
5 (defvar *cq-native* nil)
6 (defvar *cq-api* nil)
7 (defparameter *cq-known-matches* nil) ; frozen only after outside-repository run
8 (defun cq-wrap (content &optional (attributes ""))
9 (format nil "<xtm:resourceData xmlns:xtm='http://www.topicmaps.org/xtm/' datatype='http://www.w3.org/2001/XMLSchema#anyType'~A>~A</xtm:resourceData>" attributes content))
10 (defun cq-specs ()
11 (list
12 (list "simple" (cq-wrap "<x>Hello</x>") "<x>Hello</x>" "XTM 4.16.3; C14N 2.3: element/text, omit container")
13 (list "attributes-ba" (cq-wrap "<x b='2' a='1'/>") "<x a=\"1\" b=\"2\"></x>" "C14N 2.3: unqualified attribute order and expanded empty element")
14 (list "attributes-ab" (cq-wrap "<x a='1' b='2'></x>") "<x a=\"1\" b=\"2\"></x>" "Same node properties as attributes-ba")
15 (list "inherited-prefix" (cq-wrap "<p:x>A</p:x>" " xmlns:p='urn:p' xmlns:u='urn:unused'") "<p:x xmlns:p=\"urn:p\">A</p:x>" "XTM 4.16.3: needed inherited prefix retained; unused mapping removed")
16 (list "unused-local-namespace" (cq-wrap "<p:x xmlns:p='urn:p' xmlns:u='urn:unused'>A</p:x>") "<p:x xmlns:p=\"urn:p\">A</p:x>" "XTM 4.16.3: unused namespace absent from selected namespace set")
17 (list "inherited-default" (cq-wrap "<x>A</x>" " xmlns='urn:default'") "<x xmlns=\"urn:default\">A</x>" "C14N 2.3-2.4: selected child must retain inherited default namespace")
18 (list "nested" (cq-wrap "<x><y b='2' a='1'>Z</y><empty/></x>") "<x><y a=\"1\" b=\"2\">Z</y><empty></empty></x>" "C14N 2.3: nested elements, attributes, empty element")
19 (list "escaping" (cq-wrap "<x a='&amp;&lt;&quot;&#x9;&#xA;&#xD;'>A&amp;B&lt;C&gt;&quot;&#x9;&#xA;&#xD;</x>")
20 (format nil "<x a=\"&amp;&lt;&quot;&#x9;&#xA;&#xD;\">A&amp;B&lt;C&gt;\"~C~C&#xD;</x>" #\Tab #\Newline)
21 "C14N 2.3: attribute vs text escapes, hexadecimal character references")
22 (list "whitespace-line-endings" (cq-wrap (format nil "<x> a~C~C b~C c~C~C</x>" #\Return #\Newline #\Return #\Tab #\Newline))
23 (format nil "<x> a~% b~% c~C~%</x>" #\Tab) "XML 2.11; C14N 2.1/2.3: normalize CR/CRLF, retain text whitespace")
24 (list "comments" (cq-wrap "<x>A<!--excluded-->B</x>") "<x>AB</x>" "XTM 4.16.3: comments=false")
25 (list "lexical-cdata" (cq-wrap "<x a='&#65;'><y/><![CDATA[a&b]]></x>") "<x a=\"A\"><y></y>a&amp;b</x>" "C14N 2.1/2.3: references, CDATA and tag spelling have equal selected values")
26 (list "lexical-escaped" (cq-wrap "<x a=\"A\"><y></y>a&amp;b</x>") "<x a=\"A\"><y></y>a&amp;b</x>" "Same selected node properties as lexical-cdata")
27 (list "unicode" (cq-wrap "<x>Grüße 水 🦊</x>") "<x>Grüße 水 🦊</x>" "C14N 2.1: strict UTF-8 output for scalar character data")
28 (list "processing-instruction" (cq-wrap "<x>A<?p excluded?>B</x>") "<x>AB</x>" "XTM selects element/attribute/character nodes, not PI nodes")
29 (list "namespace-attribute-order" (cq-wrap "<x xmlns:z='urn:a' xmlns:a='urn:z' z:b='1' a:a='2' plain='0'/>")
30 "<x xmlns:a=\"urn:z\" xmlns:z=\"urn:a\" plain=\"0\" z:b=\"1\" a:a=\"2\"></x>"
31 "C14N 2.3: namespace nodes before attributes, attributes by namespace URI/local name")
32 (list "redundant-binding" (cq-wrap "<p:x xmlns:p='urn:p'><p:y xmlns:p='urn:p'/></p:x>")
33 "<p:x xmlns:p=\"urn:p\"><p:y></p:y></p:x>" "C14N 2.3: suppress already rendered identical namespace binding")))
34 (defun cq-parse-dom (raw)
35 ;; Diagnostic-only CXML configuration. The productive XML wrapper remains
36 ;; unchanged and is NOT claimed compatible with DOM. Same closed-handler,
37 ;; no DOCTYPE, no resolver fallback; DOM requires xmlns attributes and URIs.
38 (assert (equal (symbol-value 'cxml::*topic-maps-encoding-patch*) "19d0b-1"))
39 (assert (equal (symbol-value 'runes-encoding::*topic-maps-encoding-patch*) "19d0b-1"))
40 (let ((sax:*namespace-processing* t) (sax:*include-xmlns-attributes* t)
41 (sax:*use-xmlns-namespace* t) (cxml:*catalog* nil) (cxml:*cache-all-dtds* nil)
42 (cxml:*dtd-cache* (cxml:make-dtd-cache)))
43 (cxml:parse-octets raw
44 (make-instance 'topic-maps.xml::closed-handler :chained-handler (cxml-dom:make-dom-builder))
45 :validate nil :disallow-internal-subset t :entity-resolver #'topic-maps.xml::deny-external-resource)))
46 (defun cq-condition (c)
47 (when c (mm-fields "class" (string (type-of c)) "detail" (princ-to-string c))))
48 (defun cq-write (path value)
49 (with-open-file (s path :direction :output :if-exists :supersede :if-does-not-exist :create)
50 (topic-maps.test-foundation::emit-json-value value s) (terpri s)))
51 (defun cq-native-probe (id value)
52 (let* ((map (make-topic-map)) (owner (intern-topic map :item-identifier "urn:cq:owner"))
53 (type (intern-topic map :subject-identifier "urn:cq:type"))
54 (name-type (intern-topic map :subject-identifier "urn:cq:name-type"))
55 (base (intern-topic map :subject-identifier "urn:cq:base"))
56 (theme (intern-topic map :subject-identifier "urn:cq:extra"))
57 (name (add-name owner "N" :type name-type :scope (list base)))
58 (count (length (topic-map-topics map)))
59 (datatype "http://www.w3.org/2001/XMLSchema#anyType")
60 (o (add-occurrence owner type value :datatype datatype :scope (list theme)))
61 (v (add-variant name value :datatype datatype :scope (list base theme)))
62 (before (ik-state map))
63 (a (topic-maps.cxtm:canonicalize-octets map :base-locator "urn:cq:base-map"))
64 (b (topic-maps.cxtm:canonicalize-octets map :base-locator "urn:cq:base-map")))
65 (check (format nil "c14n-native:~A:occurrence-value-datatype-parent-EQ" id)
66 (and (equal value (occurrence-value o)) (equal datatype (occurrence-datatype o))
67 (eq owner (occurrence-parent o)) (eq type (occurrence-type o)) (eq o (canonical-occurrence o))))
68 (check (format nil "c14n-native:~A:variant-value-datatype-parent-EQ" id)
69 (and (equal value (variant-value v)) (equal datatype (variant-datatype v))
70 (eq name (variant-parent v)) (eq v (canonical-variant v))))
71 (check (format nil "c14n-native:~A:scopes-and-no-topics-from-XML" id)
72 (and (= count (length (topic-map-topics map))) (scope-eq-p (list theme) (occurrence-scope o))
73 (scope-eq-p (list base theme) (variant-scope v))))
74 (check (format nil "c14n-native:~A:repeated-CXTM-preserves-all-properties" id)
75 (and (equalp a b) (equal before (ik-state map)) (equal value (occurrence-value o)) (equal value (variant-value v))))
76 (push (mm-fields "id" id "origin" "actual candidate bytes equal the independent oracle for this probe only"
77 "readerIntegration" :false "datatype" datatype "occurrenceValue" (occurrence-value o) "variantValue" (variant-value v)
78 "topicCountBefore" count "topicCountAfter" (length (topic-map-topics map))
79 "canonicalParentTypeScopeEQ" :true "repeatedCXTMIdentical" :true "modelUnchanged" :true
80 "actualCXTM" (babel:octets-to-string a :encoding :utf-8)) *cq-native*)))
81 (defun cq-probe (spec &optional (encoding :utf-8) bom)
82 (destructuring-bind (label source expected rule) spec
83 (let* ((id (if (eq encoding :utf-8) label (format nil "~A/~A" label encoding)))
84 (raw (bytes source encoding bom)) (copy (copy-seq raw))
85 (dom (cq-parse-dom raw))
86 (container (dom:document-element dom))
87 ;; Existing DOM child walker, applied experimentally to a container.
88 ;; No custom XML serialization/C14N, no namespace repair/filter.
89 (actual (dom:map-document (cxml:make-octet-vector-sink :canonical t) container :include-xmlns-attributes t))
90 (second-form (dom:map-document (cxml:make-octet-vector-sink :canonical 2) container :include-xmlns-attributes t))
91 (whole (dom:map-document (cxml:make-octet-vector-sink :canonical t) dom :include-xmlns-attributes t))
92 (want (bytes expected)) (match (equalp actual want))
93 (string (babel:octets-to-string actual :encoding :utf-8 :errorp t)))
94 (check (format nil "c14n-probe:~A:input-bytes-unchanged" id) (equalp raw copy))
95 (check (format nil "c14n-probe:~A:UTF8-string-roundtrip" id) (equalp actual (bytes string)))
96 (when *cq-known-matches*
97 (check (format nil "c14n-characterization:~A:observed-match-or-limit" id)
98 (eq match (not (null (member label *cq-known-matches* :test #'equal))))))
99 (push (mm-fields "id" id "inputXML" source "inputEncoding" (string encoding)
100 "inputHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
101 "API" "diagnostic CXML DOM configuration with closed-handler/DOCTYPE denial; dom:map-document canonical sink on container child nodes"
102 "candidateOption" "cxml:make-octet-vector-sink :canonical T"
103 "subsetAPIQualified" :false "expectedXML" expected "expectedHex" (format nil "~{~2,'0X~}" (coerce want 'list))
104 "normativeReason" rule "actualXML" string "actualHex" (format nil "~{~2,'0X~}" (coerce actual 'list))
105 "expectedBytes" (length want) "actualBytes" (length actual) "firstDifferentByte" (mismatch want actual)
106 "normativeComparison" (if match "PASS-for-this-probe" "BYTE-DIFFERENCE")
107 "W3C-C14N-1.0-qualified" :false "secondCanonicalFormXML" (babel:octets-to-string second-form :encoding :utf-8)
108 "secondCanonicalFormMatches" (if (equalp second-form want) :true :false)
109 "wholeDocumentCanonicalXML" (babel:octets-to-string whole :encoding :utf-8) "condition" nil) *cq-results*)
110 (when match (cq-native-probe id string)))))
111 (defun cq-tests ()
112 (setf *cq-results* nil *cq-native* nil *cq-api* nil)
113 (let ((c (condition-from (lambda () (topic-maps.xml:parse-octets (bytes "<x/>") (cxml-dom:make-dom-builder))))))
114 (check "c14n-api:productive-boundary-DOM-incompatibility-observed"
115 (and c (search "SAX configuration is incompatible with DOM" (princ-to-string c))))
116 (push (mm-fields "id" "productive-byte-boundary-DOM" "condition" (cq-condition c)) *cq-api*))
117 (dolist (text '("<!DOCTYPE x><x/>" "<!DOCTYPE x SYSTEM 'file:///tmp/no-cq-read'><x/>"))
118 (let ((c (condition-from (lambda () (cq-parse-dom (bytes text))))))
119 (check (format nil "c14n-api:diagnostic-DOM-DOCTYPE-denied:~A" text) (typep c 'topic-maps.xml:doctype-forbidden))
120 (push (mm-fields "id" text "condition" (cq-condition c)) *cq-api*)))
121 (dolist (spec (cq-specs)) (cq-probe spec))
122 (dolist (encoding '(:utf-16le :utf-16be))
123 (let* ((spec (copy-list (find "unicode" (cq-specs) :key #'first :test #'equal))))
124 (setf (second spec) (concatenate 'string "<?xml version='1.0' encoding='UTF-16'?>" (second spec)))
125 (cq-probe spec encoding t)))
126 (dolist (pair (list
127 (list "sink-has-no-node-set-keyword" (lambda () (cxml:make-octet-vector-sink :canonical t :node-set nil)))
128 (list "walker-has-no-node-set-keyword" (lambda ()
129 (let ((doc (cq-parse-dom (bytes "<x/>"))))
130 (dom:map-document (cxml:make-octet-vector-sink :canonical t) doc :node-set nil))))))
131 (let ((c (condition-from (second pair))))
132 (check (concatenate 'string "c14n-api:" (first pair)) (typep c 'program-error))
133 (push (mm-fields "id" (first pair) "condition" (cq-condition c)) *cq-api*)))
134 (setf *cq-results* (nreverse *cq-results*) *cq-native* (nreverse *cq-native*) *cq-api* (nreverse *cq-api*))
135 (let ((path (uiop:getenv "TOPIC_MAPS_C14N_REPORT")))
136 (when path (cq-write path (mm-fields "kind" "slice-22q-existing-candidate-characterization-not-reader-support"
137 "W3CC14NQualified" :false "XTMReaderIntegration" :false "ownC14NImplementation" :false
138 "probes" (coerce *cq-results* 'vector) "nativeValues" (coerce *cq-native* 'vector)
139 "APILimits" (coerce *cq-api* 'vector) "checks" (coerce (reverse *checks*) 'vector)))))
140 t)