]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - reports/slice-22q/attempts/initial-boundary-exploratory.lisp
Qualify closed XML/C14N 1.0 infrastructure for XTM resourceData
[topic-maps.git] / reports / slice-22q / attempts / initial-boundary-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-condition (c)
35 (when c (mm-fields "class" (string (type-of c)) "detail" (princ-to-string c))))
36 (defun cq-write (path value)
37 (with-open-file (s path :direction :output :if-exists :supersede :if-does-not-exist :create)
38 (topic-maps.test-foundation::emit-json-value value s) (terpri s)))
39 (defun cq-native-probe (id value)
40 (let* ((map (make-topic-map)) (owner (intern-topic map :item-identifier "urn:cq:owner"))
41 (type (intern-topic map :subject-identifier "urn:cq:type"))
42 (name-type (intern-topic map :subject-identifier "urn:cq:name-type"))
43 (base (intern-topic map :subject-identifier "urn:cq:base"))
44 (theme (intern-topic map :subject-identifier "urn:cq:extra"))
45 (name (add-name owner "N" :type name-type :scope (list base)))
46 (count (length (topic-map-topics map)))
47 (datatype "http://www.w3.org/2001/XMLSchema#anyType")
48 (o (add-occurrence owner type value :datatype datatype :scope (list theme)))
49 (v (add-variant name value :datatype datatype :scope (list base theme)))
50 (before (ik-state map))
51 (a (topic-maps.cxtm:canonicalize-octets map :base-locator "urn:cq:base-map"))
52 (b (topic-maps.cxtm:canonicalize-octets map :base-locator "urn:cq:base-map")))
53 (check (format nil "c14n-native:~A:occurrence-value-datatype-parent-EQ" id)
54 (and (equal value (occurrence-value o)) (equal datatype (occurrence-datatype o))
55 (eq owner (occurrence-parent o)) (eq type (occurrence-type o)) (eq o (canonical-occurrence o))))
56 (check (format nil "c14n-native:~A:variant-value-datatype-parent-EQ" id)
57 (and (equal value (variant-value v)) (equal datatype (variant-datatype v))
58 (eq name (variant-parent v)) (eq v (canonical-variant v))))
59 (check (format nil "c14n-native:~A:scopes-and-no-topics-from-XML" id)
60 (and (= count (length (topic-map-topics map))) (scope-eq-p (list theme) (occurrence-scope o))
61 (scope-eq-p (list base theme) (variant-scope v))))
62 (check (format nil "c14n-native:~A:repeated-CXTM-preserves-all-properties" id)
63 (and (equalp a b) (equal before (ik-state map)) (equal value (occurrence-value o)) (equal value (variant-value v))))
64 (push (mm-fields "id" id "origin" "actual candidate bytes equal the independent oracle for this probe only"
65 "readerIntegration" :false "datatype" datatype "occurrenceValue" (occurrence-value o) "variantValue" (variant-value v)
66 "topicCountBefore" count "topicCountAfter" (length (topic-map-topics map))
67 "canonicalParentTypeScopeEQ" :true "repeatedCXTMIdentical" :true "modelUnchanged" :true
68 "actualCXTM" (babel:octets-to-string a :encoding :utf-8)) *cq-native*)))
69 (defun cq-probe (spec &optional (encoding :utf-8) bom)
70 (destructuring-bind (label source expected rule) spec
71 (let* ((id (if (eq encoding :utf-8) label (format nil "~A/~A" label encoding)))
72 (raw (bytes source encoding bom)) (copy (copy-seq raw))
73 (dom (topic-maps.xml:parse-octets raw (cxml-dom:make-dom-builder)))
74 (container (dom:document-element dom))
75 ;; Existing DOM child walker, applied experimentally to a container.
76 ;; No custom XML serialization/C14N, no namespace repair/filter.
77 (actual (dom:map-document (cxml:make-octet-vector-sink :canonical t) container :include-xmlns-attributes t))
78 (second-form (dom:map-document (cxml:make-octet-vector-sink :canonical 2) container :include-xmlns-attributes t))
79 (whole (dom:map-document (cxml:make-octet-vector-sink :canonical t) dom :include-xmlns-attributes t))
80 (want (bytes expected)) (match (equalp actual want))
81 (string (babel:octets-to-string actual :encoding :utf-8 :errorp t)))
82 (check (format nil "c14n-probe:~A:input-bytes-unchanged" id) (equalp raw copy))
83 (check (format nil "c14n-probe:~A:UTF8-string-roundtrip" id) (equalp actual (bytes string)))
84 (when *cq-known-matches*
85 (check (format nil "c14n-characterization:~A:observed-match-or-limit" id)
86 (eq match (not (null (member label *cq-known-matches* :test #'equal))))))
87 (push (mm-fields "id" id "inputXML" source "inputEncoding" (string encoding)
88 "inputHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
89 "API" "topic-maps.xml:parse-octets / cxml-dom:make-dom-builder; dom:map-document canonical sink on container child nodes"
90 "candidateOption" "cxml:make-octet-vector-sink :canonical T"
91 "subsetAPIQualified" :false "expectedXML" expected "expectedHex" (format nil "~{~2,'0X~}" (coerce want 'list))
92 "normativeReason" rule "actualXML" string "actualHex" (format nil "~{~2,'0X~}" (coerce actual 'list))
93 "expectedBytes" (length want) "actualBytes" (length actual) "firstDifferentByte" (mismatch want actual)
94 "normativeComparison" (if match "PASS-for-this-probe" "BYTE-DIFFERENCE")
95 "W3C-C14N-1.0-qualified" :false "secondCanonicalFormXML" (babel:octets-to-string second-form :encoding :utf-8)
96 "secondCanonicalFormMatches" (if (equalp second-form want) :true :false)
97 "wholeDocumentCanonicalXML" (babel:octets-to-string whole :encoding :utf-8) "condition" nil) *cq-results*)
98 (when match (cq-native-probe id string)))))
99 (defun cq-tests ()
100 (setf *cq-results* nil *cq-native* nil *cq-api* nil)
101 (dolist (spec (cq-specs)) (cq-probe spec))
102 (dolist (encoding '(:utf-16le :utf-16be))
103 (let* ((spec (copy-list (find "unicode" (cq-specs) :key #'first :test #'equal))))
104 (setf (second spec) (concatenate 'string "<?xml version='1.0' encoding='UTF-16'?>" (second spec)))
105 (cq-probe spec encoding t)))
106 (dolist (pair (list
107 (list "sink-has-no-node-set-keyword" (lambda () (cxml:make-octet-vector-sink :canonical t :node-set nil)))
108 (list "walker-has-no-node-set-keyword" (lambda ()
109 (let ((doc (topic-maps.xml:parse-octets (bytes "<x/>") (cxml-dom:make-dom-builder))))
110 (dom:map-document (cxml:make-octet-vector-sink :canonical t) doc :node-set nil))))))
111 (let ((c (condition-from (second pair))))
112 (check (concatenate 'string "c14n-api:" (first pair)) (typep c 'program-error))
113 (push (mm-fields "id" (first pair) "condition" (cq-condition c)) *cq-api*)))
114 (setf *cq-results* (nreverse *cq-results*) *cq-native* (nreverse *cq-native*) *cq-api* (nreverse *cq-api*))
115 (let ((path (uiop:getenv "TOPIC_MAPS_C14N_REPORT")))
116 (when path (cq-write path (mm-fields "kind" "slice-22q-existing-candidate-characterization-not-reader-support"
117 "W3CC14NQualified" :false "XTMReaderIntegration" :false "ownC14NImplementation" :false
118 "probes" (coerce *cq-results* 'vector) "nativeValues" (coerce *cq-native* 'vector)
119 "APILimits" (coerce *cq-api* 'vector) "checks" (coerce (reverse *checks*) 'vector)))))
120 t)