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