]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-negative-originals.lisp
Integrate qualified XML anyType values into the XTM reader
[topic-maps.git] / tests / xtm-negative-originals.lisp
1 (in-package #:topic-maps.xtm.tests)
2
3 ;; Observed again before registration in Slice 21C. Position units are the
4 ;; reader's approximate SAX line/column, not byte offsets. NIL is intentional
5 ;; for the existing unwrapped TMDM condition, which exposes no source position.
6 (defparameter *negative-original-specs*
7 '(("id-invalid" xtm-rejection :invalid-id "topic/@id must be an XML Schema ID." 2 22 "XTM 2.0 4.5, Annex A: topic/@id is required xsd:ID")
8 ("itemid-collision" topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier "Locator already identifies a different information item." nil nil "XTM 2.0 4.6; TMDM 5.1: duplicate Item Identifiers on different items")
9 ("no-version" xtm-rejection :missing-attribute "Required version attribute is absent." 1 49 "XTM 2.0 4.4, Annex A: required version=2.0")
10 ("reifier-elem-in-2.0" xtm-rejection :unexpected-element "Unexpected reifier element." 2 12 "XTM 2.0 4.3.2,4.4, Annex A: no reifier child element")
11 ("subjid-ref-in-2.0" xtm-rejection :unexpected-element "Unexpected subjectIdentifierRef element." 4 65 "XTM 2.0 4.9, Annex A: instanceOf contains topicRef+")
12 ("subjloc-ref-in-2.0" xtm-rejection :unexpected-element "Unexpected subjectLocatorRef element." 4 58 "XTM 2.0 4.9, Annex A: instanceOf contains topicRef+")
13 ("topic-no-id" xtm-rejection :missing-id "Required id attribute is absent." 2 10 "XTM 2.0 4.5, Annex A: required topic/@id")))
14
15 (defun negative-condition-data (c)
16 (when c
17 (let* ((xtm (typep c 'topic-maps.xtm:xtm-error))
18 (identifier (typep c 'item-identifier-error))
19 (variant (typep c 'variant-error))
20 (code (cond (xtm (error-code c)) (identifier (item-identifier-error-code c)) (variant (variant-error-code c)))))
21 (topic-maps.test-foundation::fields
22 "class" (string (type-of c)) "code" (when code (string-downcase (string code)))
23 "detail" (cond (xtm (topic-maps.xtm:error-detail c))
24 (identifier (topic-maps.tmdm::item-identifier-error-detail c))
25 (variant (topic-maps.tmdm::variant-error-detail c)))
26 "message" (princ-to-string c)
27 "line" (when xtm (error-line c)) "column" (when xtm (error-column c))
28 "positionUnit" (when xtm "approximate-SAX-line-column")
29 "documentIRI" (when xtm (error-document-iri c)) "sourceKey" (when xtm (error-source-key c))
30 "cause" (when (typep c 'xtm-xml-error) (negative-condition-data (topic-maps.xtm:error-cause c)))))))
31
32 (defun intended-negative-p (spec c iri key)
33 (and c (not (typep c 'xtm-unsupported)) (eq (type-of c) (second spec))
34 (cond
35 ((typep c 'xtm-rejection)
36 (and (eq (third spec) (error-code c))
37 (equal (fourth spec) (topic-maps.xtm:error-detail c))
38 (eql (fifth spec) (error-line c)) (eql (sixth spec) (error-column c))
39 (equal iri (error-document-iri c)) (equal key (error-source-key c))))
40 ((typep c 'reification-error)
41 (and (eq (third spec) (reification-error-code c))
42 (equal (fourth spec) (topic-maps.tmdm::reification-error-detail c))
43 (null (fifth spec)) (null (sixth spec))))
44 ((typep c 'variant-error)
45 (and (eq (third spec) (variant-error-code c))
46 (equal (fourth spec) (topic-maps.tmdm::variant-error-detail c))
47 (null (fifth spec)) (null (sixth spec))))
48 ((typep c 'topic-maps.tmdm::occurrence-identifier-error)
49 (and (eq (third spec) (item-identifier-error-code c))
50 (equal (fourth spec) (topic-maps.tmdm::item-identifier-error-detail c))
51 (null (fifth spec)) (null (sixth spec)))))))
52
53 (defun record-negative-original-result (case spec c iri key)
54 ;; :CTM-REJECTION is the foundation's historical generic negative-outcome
55 ;; token. It does not imply CTM parsing: these conditions come from XTM/TMDM.
56 (topic-maps.test-foundation:record-result case
57 (cond ((typep c 'xtm-unsupported) :unsupported)
58 ((intended-negative-p spec c iri key) :ctm-rejection)
59 (c :execution-error) (t :ctm-acceptance))
60 :diagnostics (list (if c (format nil "Expected ~A; actual ~A: ~A" (third spec) (type-of c) c)
61 "Negative original unexpectedly accepted."))))
62
63 (defun negative-source-facts (name root)
64 (let* ((children (topic-maps.xtm::elements root))
65 (topic (find "topic" children :key #'topic-maps.xtm::node-name :test #'equal))
66 (kids (when topic (topic-maps.xtm::elements topic))))
67 (check (concatenate 'string "negative-original:" name ":original-source-cause")
68 (and (equal "topicMap" (topic-maps.xtm::node-name root))
69 (equal "http://www.topicmaps.org/xtm/" (topic-maps.xtm::node-uri root))
70 (cond
71 ((equal name "id-invalid") (equal "2topic" (topic-maps.xtm::attr topic "id")))
72 ((equal name "itemid-collision")
73 (and (= 2 (length children)) (= 1 (length kids))
74 (equal "itemIdentity" (topic-maps.xtm::node-name (first children)))
75 (equal "itemIdentity" (topic-maps.xtm::node-name (first kids)))
76 (equal "http://example.org/#crash" (topic-maps.xtm::attr (first children) "href"))
77 (equal "http://example.org/#crash" (topic-maps.xtm::attr (first kids) "href"))
78 (equal "topic" (topic-maps.xtm::attr topic "id"))))
79 ((equal name "no-version") (and (null children) (null (topic-maps.xtm::attr root "version"))))
80 ((equal name "reifier-elem-in-2.0") (equal "reifier" (topic-maps.xtm::node-name (first children))))
81 ((member name '("subjid-ref-in-2.0" "subjloc-ref-in-2.0") :test #'equal)
82 (and (equal "instanceOf" (topic-maps.xtm::node-name (first kids)))
83 (equal (if (equal name "subjid-ref-in-2.0") "subjectIdentifierRef" "subjectLocatorRef")
84 (topic-maps.xtm::node-name (first (topic-maps.xtm::elements (first kids)))))))
85 ((equal name "topic-no-id") (null (topic-maps.xtm::attr topic "id"))))))))
86
87 (defun negative-original-contracts ()
88 (setf *negative-outputs* nil)
89 (dolist (spec *negative-original-specs*)
90 (let* ((name (first spec)) (id (format nil "xtm2/invalid/~A.xtm" name))
91 (iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
92 (raw (file-bytes (corpus-path id))) (before (copy-seq raw))
93 (case (topic-maps.test-foundation:make-test-case :id id :category :invalid
94 :source-pathname (corpus-path id) :document-iri iri :expected-outcome :ctm-rejection
95 :clauses (list (seventh spec))))
96 (xml (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler)))
97 (c (condition-from (lambda () (deserialize-octets raw :document-iri iri :source-key id)))))
98 ;; XML well-formedness is a separate fact, never an XTM schema claim.
99 (negative-source-facts name xml)
100 (record-negative-original-result case spec c iri id)
101 (setf *cases* (append *cases* (list case)))
102 (push (topic-maps.test-foundation::fields
103 "id" id "documentIRI" iri "sourceKey" id "sourceBytes" (length raw)
104 "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list))
105 "sourceText" (babel:octets-to-string raw :encoding :utf-8)
106 "xmlWellFormed" :true "normativeClause" (seventh spec)
107 "condition" (negative-condition-data c)
108 "intendedCauseReached" (if (intended-negative-p spec c iri id) :true :false)
109 "unsupported" (if (typep c 'xtm-unsupported) :true :false)
110 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *negative-outputs*)
111 (check (concatenate 'string "negative-original:" name ":intended-condition-code-detail-position-context")
112 (intended-negative-p spec c iri id))
113 (check (concatenate 'string "negative-original:" name ":no-preliminary-unsupported") (not (typep c 'xtm-unsupported)))
114 (check (concatenate 'string "negative-original:" name ":source-octets-preserved") (equalp raw before))
115 (check (concatenate 'string "negative-original:" name ":registered-pass")
116 (eq :pass (topic-maps.test-foundation:test-case-status case)))))
117 (setf *negative-outputs* (nreverse *negative-outputs*))
118 (check "negative-originals:33-unique-case-records"
119 (and (= 33 (length *cases*)) (= 7 (count :invalid *cases* :key #'topic-maps.test-foundation:test-case-category))
120 (= 33 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))))))
121
122 (defun replace-control-text (text old new &optional (start 0))
123 (let ((at (search old text :start2 start)))
124 (assert at) (concatenate 'string (subseq text 0 at) new (subseq text (+ at (length old))))))
125
126 (defun negative-original-controls ()
127 (setf *negative-controls* nil)
128 (flet ((source (name) (babel:octets-to-string (file-bytes (corpus-path (format nil "xtm2/invalid/~A.xtm" name))) :encoding :utf-8)))
129 (let* ((collision (source "itemid-collision")) (shared "http://example.org/#crash")
130 (different "http://example.org/#different"))
131 (dolist (spec (list
132 (list "valid-id" (replace-control-text (source "id-invalid") "2topic" "topic") 1 0 nil nil)
133 (list "version-present" (replace-control-text (source "no-version") "<topicMap " "<topicMap version='2.0' ") 0 0 nil nil)
134 (list "id-present-despite-si" (replace-control-text (source "topic-no-id") "<topic>" "<topic id='topic'>") 1 0 nil nil)
135 (list "map-ii-distinct" (replace-control-text collision shared different) 1 0 different shared)
136 (list "topic-ii-distinct" (replace-control-text collision shared different (1+ (search shared collision))) 1 0 shared different)
137 (list "legal-topicMap-child" (document "<itemIdentity href='#map'/><topic id='tmtopic'/>") 1 0 nil nil)
138 (list "legal-instanceOf-child" (document "<topic id='topic'><instanceOf><topicRef href='#type'/></instanceOf></topic>") 5 1 nil nil)))
139 (destructuring-bind (label text topics associations map-ii topic-ii) spec
140 (let* ((map (deserialize-octets (bytes text) :document-iri *document* :source-key (concatenate 'string "control:" label)))
141 (topic (by-id map "topic")))
142 (check (concatenate 'string "negative-control:" label ":accepted-model")
143 (and (= topics (length (topic-map-topics map))) (= associations (length (topic-map-associations map)))
144 (references-current-p map)))
145 (when map-ii
146 (check (concatenate 'string "negative-control:" label ":map-topic-distinct-eq-and-exact-identifiers")
147 (and topic (not (eq map topic)) (eq map (topic-parent topic))
148 (equal (list map-ii) (item-identifiers map))
149 (same-identifiers-p (list (concatenate 'string *document* "#topic") topic-ii) (topic-item-identifiers topic))
150 (null (topic-names topic)) (null (topic-occurrences topic)))))
151 (when (equal label "id-present-despite-si")
152 (check "negative-control:si-does-not-replace-required-id"
153 (equal '("http://psi.example.com/foo") (topic-subject-identifiers topic))))
154 (push (topic-maps.test-foundation::fields "label" label "sourceText" text
155 "condition" nil "model" (original-model-data map) "registeredAsOriginal" :false) *negative-controls*))))))
156 ;; Use real conditions, and require a matching cause rather than any ERROR.
157 (let ((spec (find "reifier-elem-in-2.0" *negative-original-specs* :key #'first :test #'equal)))
158 (dolist (row (list (list "accepted-is-fail" nil :fail)
159 (list "arbitrary-error-is-fail" (make-condition 'simple-error :format-control "local guard control") :fail)
160 (list "xml-error-is-fail" (condition-from (lambda () (read-body "<a><b></a>"))) :fail)
161 (list "unsupported-is-not-pass" (condition-from (lambda () (read-body "<mergeMap href='urn:22s:requires-explicit-resolver'/>"))) :unsupported)
162 (list "wrong-xtm-cause-is-fail" (condition-from (lambda () (read-body "<topic id='2bad'/>"))) :fail)
163 (list "same-code-wrong-element-is-fail" (condition-from (lambda () (read-body "<unknown/>"))) :fail)))
164 (destructuring-bind (label c status) row
165 (let ((case (topic-maps.test-foundation:make-test-case :id "local:negative-classification-control" :category :invalid :expected-outcome :ctm-rejection)))
166 (record-negative-original-result case spec c *document* "local:xtm-semantic-test")
167 (check (concatenate 'string "negative-control:" label)
168 (and (eq status (topic-maps.test-foundation:test-case-status case)) (not (intended-negative-p spec c *document* "local:xtm-semantic-test"))))
169 (push (topic-maps.test-foundation::fields "label" label "condition" (negative-condition-data c)
170 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case))) "registeredAsOriginal" :false) *negative-controls*)))))
171 (setf *negative-controls* (nreverse *negative-controls*)))