xref: /trunk/test/odf-validate/selftest.py (revision 9a8341fd38268a6e9d098ebdffda74a60de5b04e)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22# usage: selftest.py -- validator negative controls; exits 1 if any control misbehaves
23import collections, json, os, subprocess, sys
24D = os.path.dirname(os.path.abspath(__file__))
25val = os.path.join(D, "runs", "control", "val.json")
26os.makedirs(os.path.dirname(val), exist_ok=True)
27subprocess.run([sys.executable, "-I", os.path.join(D, "make-controls.py")], check=True)
28subprocess.run([sys.executable, "-I", os.path.join(D, "validate.py"), os.path.join(D, "fixtures", "control"), val], check=True, capture_output=True)
29d = json.load(open(val))
30def errs(v, pkg): return [e[3] for e in d["errors"][v] if e[0].startswith(pkg + "/")]
31checks = {
32    "good.odt clean in 1.2 and 1.4": not errs("12", "good.odt") and not errs("14", "good.odt"),
33    "bad.odt flags text:bogus in 1.2 and 1.4": all(any('"text:bogus"' in m for m in errs(v, "bad.odt")) for v in ("12", "14")),
34    "wrongver.odt flags office:version in 1.2 and 1.4": all(any('office:version' in m for m in errs(v, "wrongver.odt")) for v in ("12", "14")),
35    "13: good.odt clean in 1.3 and 1.3 extended": not errs("13", "good.odt") and not errs("13x", "good.odt"),
36    "13: bad.odt flags text:bogus in 1.3 and 1.3 extended": all(any('"text:bogus"' in m for m in errs(v, "bad.odt")) for v in ("13", "13x")),
37    "13: wrongver.odt flags office:version in 1.3 and 1.3 extended": all(any("office:version" in m for m in errs(v, "wrongver.odt")) for v in ("13", "13x")),
38    "13: new13.odt (text:drop-down) invalid in 1.2, valid in 1.3 and 1.3 extended": bool(errs("12", "new13.odt")) and not errs("13", "new13.odt") and not errs("13x", "new13.odt"),
39    "13: extforeign.odt invalid as written, valid once foreign content is removed": bool(errs("13", "extforeign.odt")) and not errs("13x", "extforeign.odt"),
40    "13: meta:creator-initials invalid in 1.2, valid in 1.3 and 1.3 extended; the note without it is clean in all": any('"meta:creator-initials"' in m for m in errs("12", "newinit.odt"))
41        and not errs("13", "newinit.odt") and not errs("13x", "newinit.odt") and not any(errs(v, "baseinit.odt") for v in ("12", "13", "13x")),
42    "13: chart:coordinate-region invalid in 1.2, valid in 1.3 and 1.3 extended; the chart without it is clean in all": any('"chart:coordinate-region"' in m for m in errs("12", "newregion.odc"))
43        and not errs("13", "newregion.odc") and not errs("13x", "newregion.odc") and not any(errs(v, "baseregion.odc") for v in ("12", "13", "13x")),
44    "13: style:scale-to-X/Y together with style:scale-to invalid in 1.3 and 1.3 extended": all(any('"style:scale-to"' in m for m in errs(v, "scaleboth.odt")) for v in ("13", "13x")),
45    "13: style:scale-to-X/Y alone and style:scale-to alone valid in 1.3 and 1.3 extended; X/Y invalid in 1.2": not any(errs(v, p) for v in ("13", "13x") for p in ("scalexy.odt", "scaleone.odt"))
46        and any('"style:scale-to-X"' in m for m in errs("12", "scalexy.odt")) and not errs("12", "scaleone.odt"),
47    "14: good.odt clean in 1.4 and 1.4 extended; a 1.3 version is rewritten like a 1.2 one": not errs("14x", "good.odt"),
48    "14: bad.odt flags text:bogus in 1.4 extended": any('"text:bogus"' in m for m in errs("14x", "bad.odt")),
49    "14: wrongver.odt (1.1) flags office:version in 1.4 extended": any("office:version" in m for m in errs("14x", "wrongver.odt")),
50    "14: extforeign.odt invalid as written, valid in 1.4 extended once foreign content is removed": bool(errs("14", "extforeign.odt")) and not errs("14x", "extforeign.odt"),
51    "14: draw:decorative invalid in 1.2 and 1.3, valid in 1.4 and 1.4 extended; the same style without it is clean in all": any('"draw:decorative"' in m for v in ("12", "13") for m in errs(v, "newdeco.odt"))
52        and all(errs(v, "newdeco.odt") for v in ("12", "13")) and not errs("14", "newdeco.odt") and not errs("14x", "newdeco.odt")
53        and not any(errs(v, "basedeco.odt") for v in ("12", "13", "14", "14x")),
54    "14: draw:handle with only the old combined attribute valid in 1.2 and 1.3, invalid in 1.4 and 1.4 extended": not any(errs(v, "handleold.odt") for v in ("12", "13"))
55        and all(errs(v, "handleold.odt") for v in ("14", "14x")),
56    "14: draw:handle with the new position or polar pair valid in 1.4 and 1.4 extended, invalid in 1.2 and 1.3": not any(errs(v, p) for v in ("14", "14x") for p in ("handlenew.odt", "handlepolar.odt"))
57        and all(errs(v, p) for v in ("12", "13") for p in ("handlenew.odt", "handlepolar.odt")),
58    "mathobj.odt MathML object skipped": d["mathml_skipped"] == ["mathobj.odt/Object 1/content.xml"] and not errs("12", "mathobj.odt"),
59    "no unparsed jing output": not any(e[0] == "?" for v in d["errors"] for e in d["errors"][v]),
60}
61ids = os.path.join(D, "runs", "control", "ids.json")
62subprocess.run([sys.executable, "-I", os.path.join(D, "idcheck.py"), os.path.join(D, "fixtures", "control"), ids], check=True, capture_output=True)
63i = json.load(open(ids))
64def ierrs(pkg): return [e[3] for e in i["errors"] if e[0].startswith(pkg + "/")]
65checks.update({
66    "idcheck: good.odt, goodid.odt (normalized ids) clean": not ierrs("good.odt") and not ierrs("goodid.odt"),
67    "idcheck: dupid.odt flags the duplicate after normalization": any("duplicates" in m for m in ierrs("dupid.odt")),
68    "idcheck: badid.odt, badid2.odt flag non-NCName ids": all(any("not an NCName" in m for m in ierrs(p)) for p in ("badid.odt", "badid2.odt")),
69    "idcheck: badns.odt flags the unbound prefix": any("unbound prefix" in m for m in ierrs("badns.odt")),
70    "idcheck: badsig.odt flags a duplicate id in a signature file": any("duplicates" in m for m in ierrs("badsig.odt")),
71    "idcheck: no errors in other packages": {e[0].split("/")[0] for e in i["errors"]} == {"dupid.odt", "badid.odt", "badid2.odt", "badns.odt", "badsig.odt"},
72})
73subprocess.run([sys.executable, "-I", os.path.join(D, "make-pkg-controls.py")], check=True)
74pk = os.path.join(D, "runs", "control", "pkg.json")
75pdir = os.path.join(D, "fixtures", "control-pkg")
76subprocess.run([sys.executable, "-I", os.path.join(D, "pkgcheck.py"), pdir, pk], check=True, capture_output=True)
77pe = json.load(open(pk))["errors"]
78expected = json.load(open(os.path.join(pdir, ".expected.json")))
79def found(pkg): return sorted({tuple(m.split("]")[0].split(": [")) for m in (e[3] for e in pe if e[0].startswith(pkg + "/"))})
80for pkg in sorted(expected):
81    checks["pkgcheck: " + pkg + " reports exactly " + (", ".join(r for _, r in map(tuple, expected[pkg])) or "nothing")] = found(pkg) == [tuple(x) for x in expected[pkg]]
82subprocess.run([sys.executable, "-I", os.path.join(D, "make-doctype-controls.py")], check=True)
83dt = os.path.join(D, "runs", "control", "dt.json")
84ddir = os.path.join(D, "fixtures", "control-doctype")
85subprocess.run([sys.executable, "-I", os.path.join(D, "doctypecheck.py"), ddir, dt], check=True, capture_output=True)
86de = json.load(open(dt))["errors"]
87dexpected = json.load(open(os.path.join(ddir, ".expected.json")))
88def dfound(doc): return sorted({tuple(m.split("]")[0].split(": [")) for m in (e[3] for e in de if e[0].startswith(doc + "/"))})
89for doc in sorted(dexpected):
90    checks["doctypecheck: " + doc + " reports exactly " + (", ".join(r for _, r in dexpected[doc]) or "nothing")] = dfound(doc) == [tuple(x) for x in dexpected[doc]]
91import importlib.util
92from xml.sax.saxutils import quoteattr
93spec = importlib.util.spec_from_file_location("formulacheck", os.path.join(D, "formulacheck.py"))
94fc = importlib.util.module_from_spec(spec)
95spec.loader.exec_module(fc)
96cases = [l.rstrip("\n").split("\t") for l in open(os.path.join(D, "formula-cases.tsv"), encoding="utf-8") if not l.startswith("#") and l.strip()]
97NSDECL = ('xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" '
98          'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" '
99          'xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:ooow="http://openoffice.org/2004/writer"')
100lines = ["<office:document-content %s><office:body>" % NSDECL]
101for st, attr, val, msg in cases:
102    val = val.replace("\\n", "\n").replace("\\u00a0", "\u00a0")
103    name, _, ctx = attr.partition("@")
104    tag = "office:text" if ctx == "text" else "office:spreadsheet"
105    lines.append("<%s><table:table-cell %s=%s/></%s>" % (tag, name, quoteattr(val, {"\n": "&#10;"}), tag))
106lines.append("</office:body></office:document-content>")
107got = collections.defaultdict(list)
108for line, col, level, rule, m in fc.check_stream("\n".join(lines).encode("utf-8"), collections.Counter()):
109    got[line - 2].append((level + ":" + rule, m))
110bad_cases = []
111for n, (st, attr, val, msg) in enumerate(cases):
112    want = [] if st == "ok" else st.split("+")
113    if sorted(r for r, _ in got[n]) != sorted(want) or (msg and not any(msg in m for _, m in got[n])):
114        bad_cases.append((n, st, attr, val, got[n]))
115for c in bad_cases[:10]:
116    print("CASE", *c)
117checks["formulacheck: all %d cases in formula-cases.tsv give exactly the expected findings" % len(cases)] = not bad_cases and len(cases) > 100
118checks.update({
119    "extended: extforeign.odt invalid as written, valid once foreign content is removed": bool(errs("12", "extforeign.odt")) and not errs("12x", "extforeign.odt"),
120    "extended: good.odt, goodid.odt clean": not errs("12x", "good.odt") and not errs("12x", "goodid.odt"),
121    "extended: bad.odt (unknown element in an ODF namespace) still flagged": any('"text:bogus"' in m for m in errs("12x", "bad.odt")),
122    "extended: wrongver.odt (attribute in an ODF namespace) still flagged": any("office:version" in m for m in errs("12x", "wrongver.odt")),
123    "extended: badns.odt (unbound prefix) still flagged": any("not bound" in m for m in errs("12x", "badns.odt")),
124})
125fspec = importlib.util.spec_from_file_location("foreign", os.path.join(D, "foreign.py"))
126fg = importlib.util.module_from_spec(fspec)
127fspec.loader.exec_module(fg)
128MIXED = fg.mixed_elements(os.path.join(D, "schema", "OpenDocument-v1.2-os-schema.rng"))
129HDR = ('<office:text xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" '
130       'xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:foo="urn:foo" xmlns:dc="http://purl.org/dc/elements/1.1/" '
131       'xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xf="http://www.w3.org/2002/xforms"')
132FOREIGN_CASES = [
133    ("foreign attribute removed", '<text:p foo:x="1" text:style-name="a">t</text:p>', '<text:p text:style-name="a">t</text:p>'),
134    ("foreign element outside a paragraph dropped with its content", '<foo:x>stuff<text:p>q</text:p></foo:x><text:p>t</text:p>', '<text:p>t</text:p>'),
135    ("foreign element in a paragraph: tags removed, content kept", '<text:p>a<foo:x>b<text:span>c</text:span></foo:x>d</text:p>', '<text:p>a<text:span>c</text:span>bd</text:p>'.replace("<text:span>c</text:span>bd", "b<text:span>c</text:span>d")),
136    ("foreign element in a span in a paragraph: tags removed", '<text:p><text:span>a<foo:x>b</foo:x></text:span></text:p>', '<text:p><text:span>ab</text:span></text:p>'),
137    ("foreign element below a frame (no character data): dropped", '<text:p><draw:frame><foo:x>b</foo:x></draw:frame></text:p>', '<text:p><draw:frame></draw:frame></text:p>'),
138    ("namespace declared on an unwrapped element is kept for its children", '<text:p><foo:x xmlns:t2="urn:oasis:names:tc:opendocument:xmlns:text:1.0"><t2:span>a</t2:span></foo:x></text:p>',
139     '<text:p><t2:span xmlns:t2="urn:oasis:names:tc:opendocument:xmlns:text:1.0">a</t2:span></text:p>'),
140    ("elements and attributes of table 4 namespaces kept", '<text:p xlink:href="u"><dc:title>t</dc:title></text:p>', '<text:p xlink:href="u"><dc:title>t</dc:title></text:p>'),
141    ("content below a table 4 element kept (wildcard content)", '<xf:model><foo:data><bar/></foo:data></xf:model>', '<xf:model><foo:data><bar></bar></foo:data></xf:model>'),
142    ("text is escaped again", '<text:p>a &amp; b &lt; c</text:p>', '<text:p>a &amp; b &lt; c</text:p>'),
143]
144for name, src, want in FOREIGN_CASES:
145    got = fg.strip_foreign(HDR + ">" + src + "</office:text>", MIXED).split("\n", 1)[1].strip()
146    inner = got[got.index(">") + 1:-len("</office:text>")] if got.startswith("<office:text") else got
147    checks["foreign.py: " + name] = inner == want
148ODFV_JAR = os.environ.get("ODFV_ODFVALIDATOR", os.path.join(D, "tools", "odfvalidator-1.1.8-incubating-jar-with-dependencies.jar"))
149if os.path.isfile(ODFV_JAR):
150    cc = os.path.join(D, "runs", "control", "cross.json")
151    subprocess.run([sys.executable, "-I", os.path.join(D, "crosscheck.py"), os.path.join(D, "fixtures", "control"), os.path.join(D, "runs", "control", "val.json"), cc], check=True, capture_output=True)
152    x = json.load(open(cc))
153    c, e = x["conformance"], x["extended"]
154    checks.update({
155        "crosscheck: odfvalidator flags bad.odt in both modes": all("bad.odt/content.xml" in m["odfvalidator_error_streams"] for m in (c, e)),
156        "crosscheck: odfvalidator flags extforeign.odt as written but not as an extended document": "extforeign.odt/content.xml" in c["odfvalidator_error_streams"] and "extforeign.odt/content.xml" not in e["odfvalidator_error_streams"],
157        "crosscheck: odfvalidator finds the duplicate xml:id that jing -i cannot": "dupid.odt/content.xml" in c["odfvalidator_error_streams"],
158        "crosscheck: good.odt clean in odfvalidator": not any("good.odt/" in k for m in (c, e) for k in m["odfvalidator_error_streams"]),
159        "crosscheck: no stream where only jing reports errors": not c["only_jing"] and not e["only_jing"],
160    })
161else:
162    print("SKIP crosscheck controls: odfvalidator not installed (fetch-odfvalidator.sh)")
163import tempfile, time
164gspec = importlib.util.spec_from_file_location("gate", os.path.join(D, "gate.py"))
165gt = importlib.util.module_from_spec(gspec)
166gspec.loader.exec_module(gt)
167vol = gt.read_volatile()
168checks["gate: gate-volatile.tsv lists the 14 documents with a reason"] = len(vol) == 14 and all(len(r) > 10 for r in vol.values())
169checks["gate: rewrite_jobs keeps sources and moves outputs"] = gt.rewrite_jobs([("/a/b.odt", "/x/0001_b.odt")], "/new") == [("/a/b.odt", "/new/0001_b.odt")]
170fake = {"0067_AddInChart.ods": {"ndiff": 1}, "0001_x.odt": {"ndiff": 0}, "0002_y.odt": {"ndiff": 3}}
171checks["gate: classify separates volatile from unexplained differences"] = gt.classify(fake, vol) == (1, ["0067_AddInChart.ods"], ["0002_y.odt"])
172gtmp = tempfile.mkdtemp()
173stub = os.path.join(gtmp, "stub.py")
174open(stub, "w").write("import sys,os,time,json\ndest=sys.argv[1]\nfor f in sys.argv[2:]:\n    d=os.path.join(dest,os.path.basename(f)+'.json')\n"
175                      "    if os.path.exists(d): continue\n    open(os.path.join(dest,'.current'),'w').write(f)\n    time.sleep(600 if 'hang' in f else 0.6)\n    json.dump({'type':'x'},open(d,'w'))\n")
176gsrc = os.path.join(gtmp, "src")
177os.makedirs(gsrc)
178for n in ("a", "b", "hang", "c"):
179    open(os.path.join(gsrc, n), "w").write("x")
180mk = lambda port, dest, files: [sys.executable, stub, dest] + files
181r1 = gt.dump_dir(gsrc, os.path.join(gtmp, "d1"), "p", 0, 2, os.path.join(gtmp, "l1"), tool=mk, poll=0.3)
182hang = json.load(open(os.path.join(gtmp, "d1", "hang.json")))
183checks["gate: a stalled document gets one HANG marker, the others are dumped"] = r1 == {"documents": 4, "stalls": 1} and hang["error"].startswith("HANG") \
184    and all("error" not in json.load(open(os.path.join(gtmp, "d1", n + ".json"))) for n in ("a", "b", "c"))
185os.remove(os.path.join(gsrc, "hang"))
186for n in ("d", "e", "f"):
187    open(os.path.join(gsrc, n), "w").write("x")
188t0 = time.time()
189r2 = gt.dump_dir(gsrc, os.path.join(gtmp, "d2"), "p", 0, 2, os.path.join(gtmp, "l2"), tool=mk, poll=0.3)
190checks["gate: a batch longer than the stall limit that keeps progressing is not killed"] = r2 == {"documents": 6, "stalls": 0} and time.time() - t0 > 2
191stub2 = os.path.join(gtmp, "stub2.py")   # a document that "kills the office": it and the rest get a bridge error, once
192open(stub2, "w").write("import sys,os,json\ndest=sys.argv[1]\nonce=os.path.join(dest,'.once')\nbad=False\nfor f in sys.argv[2:]:\n    d=os.path.join(dest,os.path.basename(f)+'.json')\n"
193                       "    if os.path.exists(d): continue\n    if os.path.basename(f)=='bad' and not os.path.exists(once): open(once,'w').write('x'); bad=True\n"
194                       "    json.dump({'error':'Binary URP bridge already disposed'} if bad else {'type':'x'},open(d,'w'))\n")
195stub3 = os.path.join(gtmp, "stub3.py")   # the same document always kills the office
196open(stub3, "w").write("import sys,os,json\ndest=sys.argv[1]\nbad=False\nfor f in sys.argv[2:]:\n    d=os.path.join(dest,os.path.basename(f)+'.json')\n"
197                       "    if os.path.exists(d): continue\n    bad = bad or os.path.basename(f)=='bad'\n"
198                       "    json.dump({'error':'Binary URP bridge already disposed'} if bad else {'type':'x'},open(d,'w'))\n")
199gsrc2 = os.path.join(gtmp, "src2")
200os.makedirs(gsrc2)
201for n in ("a", "bad", "c", "d"):
202    open(os.path.join(gsrc2, n), "w").write("x")
203mk2 = lambda port, dest, files: [sys.executable, stub2, dest] + files
204mk3 = lambda port, dest, files: [sys.executable, stub3, dest] + files
205r3 = gt.dump_dir(gsrc2, os.path.join(gtmp, "d3"), "p", 0, 5, os.path.join(gtmp, "l3"), tool=mk2, poll=0.3)
206checks["gate: dumps that only say 'bridge disposed' are redone and counted"] = r3 == {"documents": 4, "stalls": 0, "bridge_retried": 3} \
207    and not any(gt.bridge_dead(os.path.join(gtmp, "d3", n + ".json")) for n in ("a", "bad", "c", "d"))
208r4 = gt.dump_dir(gsrc2, os.path.join(gtmp, "d4"), "p", 0, 5, os.path.join(gtmp, "l4"), tool=mk3, poll=0.3)
209checks["gate: a document that always kills the office stays an error after the retries (negative control)"] = r4["documents"] == 4 \
210    and r4["bridge_retried"] == 9 and gt.bridge_dead(os.path.join(gtmp, "d4", "bad.json")) and not gt.bridge_dead(os.path.join(gtmp, "d4", "a.json"))
211for k, ok in checks.items():
212    print(("PASS " if ok else "FAIL ") + k)
213sys.exit(0 if all(checks.values()) else 1)
214