clarent-methods 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- clarent/__init__.py +2 -0
- clarent/__main__.py +5 -0
- clarent/_methods/ach/CARD.md +46 -0
- clarent/_methods/ach/clarent_ach/__init__.py +6 -0
- clarent/_methods/ach/clarent_ach/__main__.py +5 -0
- clarent/_methods/ach/clarent_ach/cli.py +123 -0
- clarent/_methods/ach/clarent_ach/engine.py +214 -0
- clarent/_methods/ach/clarent_ach/i18n.py +160 -0
- clarent/_methods/ach/clarent_ach/model.py +174 -0
- clarent/_methods/ach/clarent_ach/render.py +72 -0
- clarent/_methods/ach/examples/flaky-test.es.json +156 -0
- clarent/_methods/ach/examples/flaky-test.json +156 -0
- clarent/_methods/ach/method.toml +43 -0
- clarent/_methods/ach/skill/SKILL.md +58 -0
- clarent/_methods/ach/tests/test_ach.py +231 -0
- clarent/_methods/redteam/CARD.md +66 -0
- clarent/_methods/redteam/clarent_redteam/__init__.py +6 -0
- clarent/_methods/redteam/clarent_redteam/__main__.py +5 -0
- clarent/_methods/redteam/clarent_redteam/catalog.py +192 -0
- clarent/_methods/redteam/clarent_redteam/cli.py +189 -0
- clarent/_methods/redteam/clarent_redteam/engine.py +281 -0
- clarent/_methods/redteam/clarent_redteam/i18n.py +199 -0
- clarent/_methods/redteam/clarent_redteam/model.py +320 -0
- clarent/_methods/redteam/clarent_redteam/render.py +103 -0
- clarent/_methods/redteam/examples/design-doc/design.es.md +27 -0
- clarent/_methods/redteam/examples/design-doc/design.md +27 -0
- clarent/_methods/redteam/examples/design-doc/findings.es.json +132 -0
- clarent/_methods/redteam/examples/design-doc/findings.json +132 -0
- clarent/_methods/redteam/examples/design-doc/findings.rejected.json +130 -0
- clarent/_methods/redteam/examples/pr/change.diff +34 -0
- clarent/_methods/redteam/examples/pr/description.md +13 -0
- clarent/_methods/redteam/examples/pr/findings.json +114 -0
- clarent/_methods/redteam/method.toml +43 -0
- clarent/_methods/redteam/skill/SKILL.md +52 -0
- clarent/_methods/redteam/tests/test_redteam.py +640 -0
- clarent/_methods/triz-matrix/CARD.md +50 -0
- clarent/_methods/triz-matrix/clarent_triz/__init__.py +6 -0
- clarent/_methods/triz-matrix/clarent_triz/__main__.py +5 -0
- clarent/_methods/triz-matrix/clarent_triz/cli.py +184 -0
- clarent/_methods/triz-matrix/clarent_triz/data/DATA_LICENSE.md +51 -0
- clarent/_methods/triz-matrix/clarent_triz/data/catalog.json +89 -0
- clarent/_methods/triz-matrix/clarent_triz/data/matrix.json +11920 -0
- clarent/_methods/triz-matrix/clarent_triz/engine.py +103 -0
- clarent/_methods/triz-matrix/clarent_triz/i18n.py +134 -0
- clarent/_methods/triz-matrix/clarent_triz/model.py +179 -0
- clarent/_methods/triz-matrix/clarent_triz/render.py +101 -0
- clarent/_methods/triz-matrix/examples/search-api.es.json +9 -0
- clarent/_methods/triz-matrix/examples/search-api.json +9 -0
- clarent/_methods/triz-matrix/method.toml +58 -0
- clarent/_methods/triz-matrix/skill/SKILL.md +50 -0
- clarent/_methods/triz-matrix/tests/test_triz.py +306 -0
- clarent/cli.py +95 -0
- clarent/registry.py +175 -0
- clarent_methods-0.1.0.dist-info/METADATA +227 -0
- clarent_methods-0.1.0.dist-info/RECORD +59 -0
- clarent_methods-0.1.0.dist-info/WHEEL +4 -0
- clarent_methods-0.1.0.dist-info/entry_points.txt +2 -0
- clarent_methods-0.1.0.dist-info/licenses/LICENSE +202 -0
- clarent_methods-0.1.0.dist-info/licenses/NOTICE +12 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
"""Red team tests. Stdlib only: `python -m unittest discover -s methods/redteam/tests` (from any directory)."""
|
|
2
|
+
import copy
|
|
3
|
+
import io
|
|
4
|
+
import json
|
|
5
|
+
import shutil
|
|
6
|
+
import sys
|
|
7
|
+
import tempfile
|
|
8
|
+
import unittest
|
|
9
|
+
from contextlib import redirect_stderr, redirect_stdout
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # run from any directory
|
|
13
|
+
|
|
14
|
+
from clarent_redteam import RedTeamError, analyze, i18n, main, parse
|
|
15
|
+
from clarent_redteam import catalog, engine, model
|
|
16
|
+
from clarent_redteam.render import markdown
|
|
17
|
+
|
|
18
|
+
EX = Path(__file__).resolve().parent.parent / "examples"
|
|
19
|
+
DESIGN = EX / "design-doc" / "findings.json"
|
|
20
|
+
DESIGN_ES = EX / "design-doc" / "findings.es.json"
|
|
21
|
+
REJECTED = EX / "design-doc" / "findings.rejected.json"
|
|
22
|
+
PR = EX / "pr" / "findings.json"
|
|
23
|
+
|
|
24
|
+
TARGET = (
|
|
25
|
+
"The worker retries failed jobs until they succeed.\n"
|
|
26
|
+
"Jobs are processed in the order they were queued.\n"
|
|
27
|
+
"Operators can roll back by switching the flag off.\n"
|
|
28
|
+
"Peak load is under 100 jobs per minute.\n"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def setUpModule():
|
|
33
|
+
i18n.set_lang("en")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Workdir(unittest.TestCase):
|
|
37
|
+
def setUp(self):
|
|
38
|
+
self.tmp = Path(tempfile.mkdtemp())
|
|
39
|
+
self.addCleanup(shutil.rmtree, self.tmp, ignore_errors=True)
|
|
40
|
+
(self.tmp / "t.md").write_text(TARGET, encoding="utf-8")
|
|
41
|
+
|
|
42
|
+
def data(self):
|
|
43
|
+
"""Smallest valid file: two assumptions, one attack on each."""
|
|
44
|
+
return {
|
|
45
|
+
"target": {"files": ["t.md"]},
|
|
46
|
+
"assumptions": [
|
|
47
|
+
{"id": "A1", "text": "retries end", "quote": "retries failed jobs until they succeed", "status": "unverified",
|
|
48
|
+
"check": "send a job that always fails"},
|
|
49
|
+
{"id": "A2", "text": "order holds", "quote": "processed in the order they were queued", "status": "unverified"},
|
|
50
|
+
],
|
|
51
|
+
"attacks": [
|
|
52
|
+
{"id": "X1", "class": "failure-modes", "assumption": "A1", "quote": "retries failed jobs until they succeed",
|
|
53
|
+
"attack": "a job that always fails is retried for ever and pins a worker", "impact": 3, "plausibility": 3,
|
|
54
|
+
"check": "enqueue a poison job and watch the worker"},
|
|
55
|
+
{"id": "X2", "class": "scale", "assumption": "A2", "quote": "processed in the order they were queued",
|
|
56
|
+
"attack": "ordering forces a single consumer, so throughput cannot scale out", "impact": 2, "plausibility": 2,
|
|
57
|
+
"check": "run two consumers and compare the order"},
|
|
58
|
+
],
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
def run_engine(self, d):
|
|
62
|
+
return analyze(parse(d, self.tmp))
|
|
63
|
+
|
|
64
|
+
def codes(self, res):
|
|
65
|
+
return {r.id: r.code for r in res.rejections}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# --------------------------------------------------------------------------- catalog
|
|
69
|
+
class CatalogTests(unittest.TestCase):
|
|
70
|
+
def test_six_fixed_classes_in_order(self):
|
|
71
|
+
self.assertEqual(catalog.CLASSES, ("key-assumptions", "failure-modes", "abuse", "scale", "dependency", "reversibility"))
|
|
72
|
+
self.assertEqual(set(catalog.CATALOG), set(catalog.CLASSES))
|
|
73
|
+
|
|
74
|
+
def test_every_class_is_bilingual_and_states_its_origin(self):
|
|
75
|
+
for c, e in catalog.CATALOG.items():
|
|
76
|
+
self.assertIn(e["origin"], ("handbook", "clarent"), c)
|
|
77
|
+
self.assertTrue(e["pages"], c)
|
|
78
|
+
for lang in ("en", "es"):
|
|
79
|
+
self.assertTrue(e[lang]["name"] and e[lang]["question"], (c, lang))
|
|
80
|
+
self.assertGreaterEqual(len(e[lang]["prompts"]), 3, (c, lang))
|
|
81
|
+
|
|
82
|
+
def test_origin_is_honest_about_what_the_handbook_has(self):
|
|
83
|
+
self.assertEqual({c for c, e in catalog.CATALOG.items() if e["origin"] == "clarent"},
|
|
84
|
+
{"scale", "dependency", "reversibility"})
|
|
85
|
+
|
|
86
|
+
def test_rubrics_cover_1_to_5_in_both_languages(self):
|
|
87
|
+
for key in ("impact", "plausibility"):
|
|
88
|
+
for lang in ("en", "es"):
|
|
89
|
+
self.assertEqual(sorted(catalog.RUBRIC[key][lang]), [1, 2, 3, 4, 5])
|
|
90
|
+
|
|
91
|
+
def test_severity_bands(self):
|
|
92
|
+
got = {r: catalog.severity(r) for r in (1, 5, 6, 11, 12, 19, 20, 25)}
|
|
93
|
+
self.assertEqual(got, {1: "S4", 5: "S4", 6: "S3", 11: "S3", 12: "S2", 19: "S2", 20: "S1", 25: "S1"})
|
|
94
|
+
self.assertTrue(catalog.at_least("S1", "S2"))
|
|
95
|
+
self.assertFalse(catalog.at_least("S3", "S2"))
|
|
96
|
+
|
|
97
|
+
def test_every_catalog_message_key_exists_in_both_languages(self):
|
|
98
|
+
self.assertEqual(set(i18n.M["en"]), set(i18n.M["es"]))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# --------------------------------------------------------------------------- anchoring
|
|
102
|
+
class QuoteTests(unittest.TestCase):
|
|
103
|
+
def files(self, text, name="t.md"):
|
|
104
|
+
return (model.TargetFile(name, text, model._looks_like_diff(name, text)),)
|
|
105
|
+
|
|
106
|
+
def test_whitespace_is_the_only_normalization(self):
|
|
107
|
+
f = self.files("alpha beta\n gamma\tdelta\nepsilon")
|
|
108
|
+
self.assertEqual(model.find_quote(f, "beta gamma delta epsilon"), ("t.md", 1))
|
|
109
|
+
self.assertIsNone(model.find_quote(f, "Beta gamma")) # case matters
|
|
110
|
+
self.assertIsNone(model.find_quote(f, "alpha gamma")) # words must be adjacent
|
|
111
|
+
|
|
112
|
+
def test_returns_the_line_where_the_quote_starts(self):
|
|
113
|
+
f = self.files("one\ntwo three\nfour five six\n")
|
|
114
|
+
self.assertEqual(model.find_quote(f, "five six")[1], 3)
|
|
115
|
+
self.assertEqual(model.find_quote(f, "three four")[1], 2)
|
|
116
|
+
|
|
117
|
+
def test_punctuation_counts(self):
|
|
118
|
+
f = self.files("it is fine.\n")
|
|
119
|
+
self.assertIsNotNone(model.find_quote(f, "it is fine."))
|
|
120
|
+
self.assertIsNone(model.find_quote(f, "it is fine!"))
|
|
121
|
+
|
|
122
|
+
def test_empty_quote_never_anchors(self):
|
|
123
|
+
self.assertIsNone(model.find_quote(self.files("abc"), " "))
|
|
124
|
+
|
|
125
|
+
def test_diff_quote_without_markers_anchors_to_the_changed_line(self):
|
|
126
|
+
diff = "diff --git a/x.py b/x.py\n--- a/x.py\n+++ b/x.py\n@@ -1,2 +1,3 @@\n context\n-old_call(a)\n+new_call(a)\n+other(b)\n"
|
|
127
|
+
f = self.files(diff, "x.diff")
|
|
128
|
+
self.assertEqual(model.find_quote(f, "new_call(a) other(b)"), ("x.diff", 7))
|
|
129
|
+
self.assertEqual(model.find_quote(f, "+new_call(a)")[1], 7) # with the marker also works (raw view)
|
|
130
|
+
|
|
131
|
+
def test_diff_headers_are_not_code(self):
|
|
132
|
+
diff = "diff --git a/x.py b/x.py\n--- a/x.py\n+++ b/x.py\n@@ -1 +1 @@\n+real_line_here\n"
|
|
133
|
+
f = self.files(diff, "x.diff")
|
|
134
|
+
self.assertIsNone(model.find_quote(f, "b/x.py @@ -1 +1 @@ real_line_here")) # headers are not stripped into code
|
|
135
|
+
|
|
136
|
+
def test_first_file_with_a_match_wins(self):
|
|
137
|
+
f = (model.TargetFile("a.md", "nothing here", False), model.TargetFile("b.md", "x\nthe needle is here\n", False))
|
|
138
|
+
self.assertEqual(model.find_quote(f, "the needle is here"), ("b.md", 2))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
class RefTests(Workdir):
|
|
142
|
+
def test_file_line_must_exist(self):
|
|
143
|
+
self.assertEqual(model.check_ref("t.md:2", self.tmp), (True, ""))
|
|
144
|
+
self.assertEqual(model.check_ref("t.md:1-4", self.tmp), (True, ""))
|
|
145
|
+
self.assertEqual(model.check_ref("t.md:5", self.tmp), (False, "why_line:4"))
|
|
146
|
+
self.assertEqual(model.check_ref("t.md:3-2", self.tmp)[0], False)
|
|
147
|
+
self.assertEqual(model.check_ref("t.md:0", self.tmp)[0], False)
|
|
148
|
+
self.assertEqual(model.check_ref("nope.md:1", self.tmp), (False, "why_not_found"))
|
|
149
|
+
|
|
150
|
+
def test_refs_cannot_leave_the_root(self):
|
|
151
|
+
self.assertEqual(model.check_ref("../t.md:1", self.tmp), (False, "why_outside"))
|
|
152
|
+
|
|
153
|
+
def test_free_text_refs_are_accepted_but_empty_is_not(self):
|
|
154
|
+
self.assertEqual(model.check_ref("pytest -n 4 tests/test_a.py", self.tmp), (True, ""))
|
|
155
|
+
self.assertEqual(model.check_ref("kubectl logs web-1 | grep ERROR", self.tmp), (True, ""))
|
|
156
|
+
self.assertEqual(model.check_ref(" ", self.tmp), (False, "why_empty"))
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# --------------------------------------------------------------------------- parse
|
|
160
|
+
class ParseTests(Workdir):
|
|
161
|
+
def assertRejectsInput(self, mutate):
|
|
162
|
+
d = self.data()
|
|
163
|
+
mutate(d)
|
|
164
|
+
with self.assertRaises(RedTeamError):
|
|
165
|
+
parse(d, self.tmp)
|
|
166
|
+
|
|
167
|
+
def test_valid_minimum(self):
|
|
168
|
+
f = parse(self.data(), self.tmp)
|
|
169
|
+
self.assertEqual((len(f.assumptions), len(f.attacks), f.kind), (2, 2, "design"))
|
|
170
|
+
|
|
171
|
+
def test_root_must_be_an_object(self):
|
|
172
|
+
with self.assertRaises(RedTeamError):
|
|
173
|
+
parse([], self.tmp)
|
|
174
|
+
|
|
175
|
+
def test_unknown_schema_rejected(self):
|
|
176
|
+
self.assertRejectsInput(lambda d: d.update(schema="clarent.redteam/9"))
|
|
177
|
+
|
|
178
|
+
def test_target_problems(self):
|
|
179
|
+
self.assertRejectsInput(lambda d: d.pop("target"))
|
|
180
|
+
self.assertRejectsInput(lambda d: d["target"].update(files=[]))
|
|
181
|
+
self.assertRejectsInput(lambda d: d["target"].update(files=["missing.md"]))
|
|
182
|
+
self.assertRejectsInput(lambda d: d["target"].update(kind="blog"))
|
|
183
|
+
|
|
184
|
+
def test_target_inside_the_file_cannot_leave_the_root(self):
|
|
185
|
+
(self.tmp.parent / "secret.txt").write_text("secret", encoding="utf-8")
|
|
186
|
+
self.addCleanup((self.tmp.parent / "secret.txt").unlink)
|
|
187
|
+
self.assertRejectsInput(lambda d: d["target"].update(files=["../secret.txt"]))
|
|
188
|
+
self.assertRejectsInput(lambda d: d["target"].update(files=[str(self.tmp.parent / "secret.txt")]))
|
|
189
|
+
|
|
190
|
+
def test_target_option_overrides_the_file(self):
|
|
191
|
+
d = self.data()
|
|
192
|
+
d["target"] = {"files": ["missing.md"]}
|
|
193
|
+
f = parse(d, self.tmp, targets=[str(self.tmp / "t.md")])
|
|
194
|
+
self.assertEqual(len(f.files), 1)
|
|
195
|
+
|
|
196
|
+
def test_structure_errors(self):
|
|
197
|
+
self.assertRejectsInput(lambda d: d.update(assumptions=[]))
|
|
198
|
+
self.assertRejectsInput(lambda d: d.update(attacks=[]))
|
|
199
|
+
self.assertRejectsInput(lambda d: d["assumptions"][1].update(id="A1"))
|
|
200
|
+
self.assertRejectsInput(lambda d: d["attacks"][1].update(id="X1"))
|
|
201
|
+
self.assertRejectsInput(lambda d: d["attacks"][0].update(**{"class": "sql-injection"}))
|
|
202
|
+
self.assertRejectsInput(lambda d: d["assumptions"][0].update(status="maybe"))
|
|
203
|
+
self.assertRejectsInput(lambda d: d["attacks"][0].update(assumption="A9"))
|
|
204
|
+
self.assertRejectsInput(lambda d: d["attacks"][0].pop("attack"))
|
|
205
|
+
self.assertRejectsInput(lambda d: d["assumptions"][0].update(evidence="x"))
|
|
206
|
+
self.assertRejectsInput(lambda d: d.update(skipped_classes=[{"class": "nope", "reason": "x"}]))
|
|
207
|
+
|
|
208
|
+
def test_ratings_must_be_integers_from_1_to_5(self):
|
|
209
|
+
for bad in (0, 6, -1, 2.5, "3", True, None):
|
|
210
|
+
with self.subTest(bad=bad):
|
|
211
|
+
self.assertRejectsInput(lambda d, bad=bad: d["attacks"][0].update(impact=bad))
|
|
212
|
+
self.assertRejectsInput(lambda d, bad=bad: d["attacks"][0].update(plausibility=bad))
|
|
213
|
+
|
|
214
|
+
def test_skipped_class_without_reason_warns_and_does_not_count(self):
|
|
215
|
+
d = self.data()
|
|
216
|
+
d["skipped_classes"] = [{"class": "abuse", "reason": ""}]
|
|
217
|
+
f = parse(d, self.tmp)
|
|
218
|
+
self.assertEqual(f.skipped, ())
|
|
219
|
+
self.assertTrue(any("abuse" in w for w in f.warnings))
|
|
220
|
+
|
|
221
|
+
def test_status_defaults_to_unverified(self):
|
|
222
|
+
d = self.data()
|
|
223
|
+
d["assumptions"][0].pop("status")
|
|
224
|
+
self.assertEqual(parse(d, self.tmp).assumptions[0].status, "unverified")
|
|
225
|
+
|
|
226
|
+
def test_diff_files_are_detected(self):
|
|
227
|
+
(self.tmp / "c.diff").write_text("diff --git a/x b/x\n@@ -1 +1 @@\n+hello world again\n", encoding="utf-8")
|
|
228
|
+
d = self.data()
|
|
229
|
+
d["target"] = {"kind": "pr", "files": ["t.md", "c.diff"]}
|
|
230
|
+
f = parse(d, self.tmp)
|
|
231
|
+
self.assertEqual([x.is_diff for x in f.files], [False, True])
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
# --------------------------------------------------------------------------- engine
|
|
235
|
+
class RejectionTests(Workdir):
|
|
236
|
+
def test_clean_input_has_no_rejections(self):
|
|
237
|
+
res = self.run_engine(self.data())
|
|
238
|
+
self.assertEqual(res.rejections, [])
|
|
239
|
+
self.assertEqual(len(res.attacks), 2)
|
|
240
|
+
|
|
241
|
+
def test_attack_without_quote_is_rejected(self):
|
|
242
|
+
d = self.data(); d["attacks"][0]["quote"] = ""
|
|
243
|
+
self.assertEqual(self.codes(self.run_engine(d)), {"X1": "quote_missing"})
|
|
244
|
+
|
|
245
|
+
def test_attack_with_invented_quote_is_rejected(self):
|
|
246
|
+
d = self.data(); d["attacks"][0]["quote"] = "the worker retries failed jobs ten times and then stops"
|
|
247
|
+
self.assertEqual(self.codes(self.run_engine(d)), {"X1": "quote_not_found"})
|
|
248
|
+
|
|
249
|
+
def test_short_quote_is_rejected(self):
|
|
250
|
+
d = self.data(); d["attacks"][0]["quote"] = "retries"
|
|
251
|
+
self.assertEqual(self.codes(self.run_engine(d)), {"X1": "quote_short"})
|
|
252
|
+
|
|
253
|
+
def test_quote_of_exactly_the_minimum_length_is_accepted(self):
|
|
254
|
+
(self.tmp / "t.md").write_text("abcdefghijkl and more text here\n", encoding="utf-8")
|
|
255
|
+
d = self.data()
|
|
256
|
+
d["assumptions"] = [d["assumptions"][0] | {"quote": "abcdefghijkl"}]
|
|
257
|
+
d["attacks"] = [d["attacks"][0] | {"quote": "abcdefghijkl"}]
|
|
258
|
+
self.assertEqual(self.run_engine(d).rejections, [])
|
|
259
|
+
d["attacks"][0]["quote"] = "abcdefghijk"
|
|
260
|
+
self.assertEqual(self.codes(self.run_engine(d)), {"X1": "quote_short"})
|
|
261
|
+
|
|
262
|
+
def test_assumption_with_invented_quote_is_rejected_and_its_attacks_cascade(self):
|
|
263
|
+
d = self.data(); d["assumptions"][0]["quote"] = "the worker never retries anything at all"
|
|
264
|
+
self.assertEqual(self.codes(self.run_engine(d)), {"A1": "quote_not_found", "X1": "assumption_rejected"})
|
|
265
|
+
|
|
266
|
+
def test_verified_and_contradicted_need_evidence(self):
|
|
267
|
+
for status in ("verified", "contradicted"):
|
|
268
|
+
with self.subTest(status=status):
|
|
269
|
+
d = self.data(); d["assumptions"][0]["status"] = status
|
|
270
|
+
self.assertEqual(self.codes(self.run_engine(d))["A1"], "no_evidence")
|
|
271
|
+
|
|
272
|
+
def test_unverified_needs_no_evidence(self):
|
|
273
|
+
self.assertNotIn("A2", self.codes(self.run_engine(self.data())))
|
|
274
|
+
|
|
275
|
+
def test_evidence_with_a_ref_to_a_missing_line_rejects_the_assumption(self):
|
|
276
|
+
d = self.data()
|
|
277
|
+
d["assumptions"][0].update(status="contradicted", evidence=[{"ref": "t.md:40", "observation": "x"}])
|
|
278
|
+
res = self.run_engine(d)
|
|
279
|
+
self.assertEqual(self.codes(res)["A1"], "ref_invalid")
|
|
280
|
+
self.assertIn("4 lines", next(r.detail for r in res.rejections if r.id == "A1"))
|
|
281
|
+
|
|
282
|
+
def test_evidence_with_a_ref_to_a_missing_file_is_rejected(self):
|
|
283
|
+
d = self.data()
|
|
284
|
+
d["assumptions"][0].update(status="verified", evidence=[{"ref": "gone.py:3", "observation": "x"}])
|
|
285
|
+
self.assertEqual(self.codes(self.run_engine(d))["A1"], "ref_invalid")
|
|
286
|
+
|
|
287
|
+
def test_evidence_without_observation_or_ref_is_rejected(self):
|
|
288
|
+
for ev in ({"ref": "t.md:1", "observation": ""}, {"ref": "", "observation": "saw it"}, {}):
|
|
289
|
+
with self.subTest(ev=ev):
|
|
290
|
+
d = self.data(); d["assumptions"][0].update(status="verified", evidence=[ev])
|
|
291
|
+
self.assertEqual(self.codes(self.run_engine(d))["A1"], "ref_invalid")
|
|
292
|
+
|
|
293
|
+
def test_free_text_evidence_is_accepted(self):
|
|
294
|
+
d = self.data()
|
|
295
|
+
d["assumptions"][0].update(status="verified", evidence=[{"ref": "pytest -k retry -x", "observation": "3 passed"}])
|
|
296
|
+
self.assertEqual(self.run_engine(d).rejections, [])
|
|
297
|
+
|
|
298
|
+
def test_attack_evidence_refs_are_checked_too(self):
|
|
299
|
+
d = self.data(); d["attacks"][0]["evidence"] = [{"ref": "t.md:99", "observation": "x"}]
|
|
300
|
+
self.assertEqual(self.codes(self.run_engine(d)), {"X1": "ref_invalid"})
|
|
301
|
+
|
|
302
|
+
def test_attack_without_check_is_rejected(self):
|
|
303
|
+
d = self.data(); d["attacks"][0]["check"] = ""
|
|
304
|
+
self.assertEqual(self.codes(self.run_engine(d)), {"X1": "check_missing"})
|
|
305
|
+
|
|
306
|
+
def test_near_duplicate_attacks_count_once(self):
|
|
307
|
+
d = self.data()
|
|
308
|
+
twin = copy.deepcopy(d["attacks"][0]); twin["id"] = "X3"
|
|
309
|
+
twin["attack"] = "A job that always fails is retried for ever and pins a worker!"
|
|
310
|
+
d["attacks"].append(twin)
|
|
311
|
+
res = self.run_engine(d)
|
|
312
|
+
self.assertEqual(self.codes(res), {"X3": "duplicate"})
|
|
313
|
+
self.assertIn("X1", res.rejections[0].detail)
|
|
314
|
+
|
|
315
|
+
def test_same_text_on_another_class_or_assumption_is_not_a_duplicate(self):
|
|
316
|
+
d = self.data()
|
|
317
|
+
twin = copy.deepcopy(d["attacks"][0]); twin["id"] = "X3"; twin["class"] = "scale"
|
|
318
|
+
other = copy.deepcopy(d["attacks"][0]); other["id"] = "X4"; other["assumption"] = "A2"
|
|
319
|
+
d["attacks"] += [twin, other]
|
|
320
|
+
self.assertEqual(self.run_engine(d).rejections, [])
|
|
321
|
+
|
|
322
|
+
def test_rejections_do_not_enter_scores_or_matrix(self):
|
|
323
|
+
d = self.data(); d["attacks"][0]["quote"] = "this sentence is not in the target at all"
|
|
324
|
+
res = self.run_engine(d)
|
|
325
|
+
self.assertEqual([a.id for a in res.attacks], ["X2"])
|
|
326
|
+
row = next(r for r in res.assumptions if r.id == "A1")
|
|
327
|
+
self.assertTrue(all(not ids for ids in row.attacks_by_class.values()))
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
class ScoringTests(Workdir):
|
|
331
|
+
def test_effective_plausibility_follows_the_assumption_status(self):
|
|
332
|
+
self.assertEqual(engine.effective_plausibility(1, "contradicted"), 5)
|
|
333
|
+
self.assertEqual(engine.effective_plausibility(5, "contradicted"), 5)
|
|
334
|
+
self.assertEqual(engine.effective_plausibility(5, "verified"), 2)
|
|
335
|
+
self.assertEqual(engine.effective_plausibility(1, "verified"), 1) # never raised
|
|
336
|
+
self.assertEqual(engine.effective_plausibility(4, "unverified"), 4)
|
|
337
|
+
|
|
338
|
+
def test_risk_is_impact_times_effective_plausibility(self):
|
|
339
|
+
d = self.data()
|
|
340
|
+
d["assumptions"][0].update(status="contradicted", evidence=[{"ref": "t.md:1", "observation": "x"}])
|
|
341
|
+
d["assumptions"][1].update(status="verified", evidence=[{"ref": "t.md:2", "observation": "x"}])
|
|
342
|
+
d["attacks"][1].update(impact=4, plausibility=5)
|
|
343
|
+
res = self.run_engine(d)
|
|
344
|
+
by = {a.id: a for a in res.attacks}
|
|
345
|
+
self.assertEqual((by["X1"].risk, by["X1"].effective_plausibility), (15, 5))
|
|
346
|
+
self.assertEqual((by["X2"].risk, by["X2"].effective_plausibility), (8, 2))
|
|
347
|
+
|
|
348
|
+
def test_ranking_breaks_ties_by_impact_then_evidence_then_id(self):
|
|
349
|
+
d = self.data()
|
|
350
|
+
d["attacks"][0].update(impact=3, plausibility=4) # 12
|
|
351
|
+
d["attacks"][1].update(impact=4, plausibility=3) # 12, higher impact
|
|
352
|
+
self.assertEqual([a.id for a in self.run_engine(d).attacks], ["X2", "X1"])
|
|
353
|
+
d["attacks"][1].update(impact=3, plausibility=4) # 12 and 12, same impact
|
|
354
|
+
d["attacks"][1]["evidence"] = [{"ref": "t.md:1", "observation": "x"}]
|
|
355
|
+
self.assertEqual([a.id for a in self.run_engine(d).attacks], ["X2", "X1"]) # more evidence first
|
|
356
|
+
d["attacks"][1]["evidence"] = []
|
|
357
|
+
self.assertEqual([a.id for a in self.run_engine(d).attacks], ["X1", "X2"]) # then id
|
|
358
|
+
|
|
359
|
+
def test_top_has_at_most_three_and_follows_the_ranking(self):
|
|
360
|
+
d = self.data()
|
|
361
|
+
for i in range(3, 7):
|
|
362
|
+
x = copy.deepcopy(d["attacks"][0]); x["id"] = f"X{i}"; x["class"] = catalog.CLASSES[i % 6]
|
|
363
|
+
x["attack"] = f"distinct failure number {i} with its own wording and details {i * 7}"
|
|
364
|
+
x["impact"] = i - 1
|
|
365
|
+
d["attacks"].append(x)
|
|
366
|
+
res = self.run_engine(d)
|
|
367
|
+
self.assertEqual(len(res.top), 3)
|
|
368
|
+
self.assertEqual([a.id for a in res.top], [a.id for a in res.attacks[:3]])
|
|
369
|
+
self.assertEqual([a.rank for a in res.attacks], list(range(1, len(res.attacks) + 1)))
|
|
370
|
+
|
|
371
|
+
def test_worst_severity(self):
|
|
372
|
+
res = self.run_engine(self.data())
|
|
373
|
+
self.assertEqual(res.worst_severity, "S3") # 3x3 = 9
|
|
374
|
+
d = self.data(); d["attacks"][0].update(impact=5, plausibility=4)
|
|
375
|
+
self.assertEqual(self.run_engine(d).worst_severity, "S1")
|
|
376
|
+
|
|
377
|
+
def test_deterministic(self):
|
|
378
|
+
a = engine.to_dict(self.run_engine(self.data()))
|
|
379
|
+
b = engine.to_dict(self.run_engine(self.data()))
|
|
380
|
+
self.assertEqual(json.dumps(a, sort_keys=True), json.dumps(b, sort_keys=True))
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
class MatrixAndWarningTests(Workdir):
|
|
384
|
+
def test_matrix_places_attacks_by_assumption_and_class(self):
|
|
385
|
+
res = self.run_engine(self.data())
|
|
386
|
+
row = {r.id: r for r in res.assumptions}
|
|
387
|
+
self.assertEqual(row["A1"].attacks_by_class["failure-modes"], ("X1",))
|
|
388
|
+
self.assertEqual(row["A2"].attacks_by_class["scale"], ("X2",))
|
|
389
|
+
self.assertEqual(row["A1"].attacks_by_class["abuse"], ())
|
|
390
|
+
self.assertEqual(row["A1"].where, "t.md:1")
|
|
391
|
+
|
|
392
|
+
def test_uncovered_classes_warn_unless_skipped_with_a_reason(self):
|
|
393
|
+
res = self.run_engine(self.data())
|
|
394
|
+
self.assertEqual(sum("has no accepted attack" in w for w in res.warnings), 4) # 6 classes - 2 covered
|
|
395
|
+
d = self.data()
|
|
396
|
+
d["skipped_classes"] = [{"class": c, "reason": "not applicable here"} for c in ("key-assumptions", "abuse", "dependency", "reversibility")]
|
|
397
|
+
res = self.run_engine(d)
|
|
398
|
+
self.assertEqual(sum("has no accepted attack" in w for w in res.warnings), 0)
|
|
399
|
+
cov = {c.class_id: c for c in res.coverage}
|
|
400
|
+
self.assertEqual(cov["abuse"].skipped_reason, "not applicable here")
|
|
401
|
+
|
|
402
|
+
def test_skipping_a_class_that_has_attacks_warns(self):
|
|
403
|
+
d = self.data(); d["skipped_classes"] = [{"class": "scale", "reason": "x"}]
|
|
404
|
+
self.assertTrue(any("skipped and also has attacks" in w for w in self.run_engine(d).warnings))
|
|
405
|
+
|
|
406
|
+
def test_assumption_without_attacks_warns(self):
|
|
407
|
+
d = self.data(); d["attacks"] = d["attacks"][:1]
|
|
408
|
+
self.assertTrue(any("A2 has no accepted attack" in w for w in self.run_engine(d).warnings))
|
|
409
|
+
|
|
410
|
+
def test_few_assumptions_and_few_attacks_warn(self):
|
|
411
|
+
w = self.run_engine(self.data()).warnings
|
|
412
|
+
self.assertTrue(any("accepted assumptions" in x for x in w))
|
|
413
|
+
self.assertTrue(any("top list has fewer than 3" in x for x in w))
|
|
414
|
+
|
|
415
|
+
def test_unverified_assumption_without_check_warns(self):
|
|
416
|
+
w = self.run_engine(self.data()).warnings
|
|
417
|
+
self.assertTrue(any("A2 is unverified and has no 'check'" in x for x in w))
|
|
418
|
+
self.assertFalse(any("A1 is unverified and has no 'check'" in x for x in w))
|
|
419
|
+
|
|
420
|
+
def test_top_attacks_on_unverified_assumptions_are_flagged(self):
|
|
421
|
+
w = self.run_engine(self.data()).warnings
|
|
422
|
+
self.assertTrue(any("Top attacks rest on unverified assumptions (A1, A2)" in x for x in w))
|
|
423
|
+
|
|
424
|
+
def test_verify_next_lists_unverified_assumptions_by_linked_risk(self):
|
|
425
|
+
d = self.data()
|
|
426
|
+
d["attacks"][1].update(impact=5, plausibility=4)
|
|
427
|
+
v = self.run_engine(d).verify_next
|
|
428
|
+
self.assertEqual([x.assumption for x in v], ["A2", "A1"])
|
|
429
|
+
self.assertEqual(v[0].max_risk, 20)
|
|
430
|
+
|
|
431
|
+
def test_verified_and_contradicted_assumptions_are_not_verify_next(self):
|
|
432
|
+
d = self.data()
|
|
433
|
+
d["assumptions"][0].update(status="contradicted", evidence=[{"ref": "t.md:1", "observation": "x"}])
|
|
434
|
+
self.assertEqual([x.assumption for x in self.run_engine(d).verify_next], ["A2"])
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
# --------------------------------------------------------------------------- the shipped examples
|
|
438
|
+
class ExampleTests(unittest.TestCase):
|
|
439
|
+
def analyze_file(self, path):
|
|
440
|
+
return analyze(parse(json.loads(path.read_text(encoding="utf-8")), path.parent))
|
|
441
|
+
|
|
442
|
+
def test_design_example_golden(self):
|
|
443
|
+
res = self.analyze_file(DESIGN)
|
|
444
|
+
self.assertEqual(res.rejections, [])
|
|
445
|
+
self.assertEqual(res.status_counts(), {"verified": 1, "unverified": 3, "contradicted": 2})
|
|
446
|
+
self.assertEqual([a.id for a in res.top], ["X3", "X1", "X2"])
|
|
447
|
+
self.assertEqual([(a.id, a.risk, a.severity) for a in res.attacks],
|
|
448
|
+
[("X3", 25, "S1"), ("X1", 20, "S1"), ("X2", 16, "S2"), ("X4", 9, "S3"),
|
|
449
|
+
("X5", 9, "S3"), ("X6", 8, "S3"), ("X7", 8, "S3")])
|
|
450
|
+
self.assertTrue(all(c.attack_ids for c in res.coverage)) # all six classes applied
|
|
451
|
+
self.assertEqual([v.assumption for v in res.verify_next], ["A6", "A2", "A3"])
|
|
452
|
+
|
|
453
|
+
def test_design_example_anchors_point_at_real_lines(self):
|
|
454
|
+
res = self.analyze_file(DESIGN)
|
|
455
|
+
where = {a.id: a.where for a in res.attacks}
|
|
456
|
+
self.assertEqual(where["X1"], "design.md:19")
|
|
457
|
+
self.assertEqual(where["X3"], "design.md:26") # the quote spans a line break
|
|
458
|
+
|
|
459
|
+
def test_spanish_example_gives_the_same_ranking(self):
|
|
460
|
+
es = self.analyze_file(DESIGN_ES)
|
|
461
|
+
self.assertEqual(es.rejections, [])
|
|
462
|
+
self.assertEqual([(a.id, a.risk) for a in es.attacks], [(a.id, a.risk) for a in self.analyze_file(DESIGN).attacks])
|
|
463
|
+
|
|
464
|
+
def test_pr_example_golden(self):
|
|
465
|
+
res = self.analyze_file(PR)
|
|
466
|
+
self.assertEqual(res.rejections, [])
|
|
467
|
+
self.assertEqual([a.id for a in res.top], ["X1", "X2", "X3"])
|
|
468
|
+
self.assertEqual(res.worst_severity, "S2")
|
|
469
|
+
cov = {c.class_id: c for c in res.coverage}
|
|
470
|
+
self.assertTrue(cov["reversibility"].skipped_reason)
|
|
471
|
+
self.assertFalse(any("has no accepted attack" in w for w in res.warnings))
|
|
472
|
+
self.assertEqual({a.id: a.where for a in res.attacks}["X1"], "change.diff:15") # anchored on the added line
|
|
473
|
+
|
|
474
|
+
def test_rejected_example_is_refused_for_the_documented_reasons(self):
|
|
475
|
+
res = self.analyze_file(REJECTED)
|
|
476
|
+
self.assertEqual({r.id: r.code for r in res.rejections}, {
|
|
477
|
+
"A3": "quote_not_found", "A4": "no_evidence", "A5": "ref_invalid",
|
|
478
|
+
"X3": "assumption_rejected", "X4": "quote_short", "X5": "duplicate", "X6": "check_missing",
|
|
479
|
+
"X7": "assumption_rejected", "X8": "assumption_rejected"})
|
|
480
|
+
self.assertEqual([a.id for a in res.attacks], ["X1", "X2"])
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
# --------------------------------------------------------------------------- CLI and report
|
|
484
|
+
def run(*argv):
|
|
485
|
+
out, err = io.StringIO(), io.StringIO()
|
|
486
|
+
with redirect_stdout(out), redirect_stderr(err):
|
|
487
|
+
code = main(list(argv))
|
|
488
|
+
return code, out.getvalue(), err.getvalue()
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
class CliTests(Workdir):
|
|
492
|
+
def setUp(self):
|
|
493
|
+
super().setUp()
|
|
494
|
+
i18n.set_lang("en")
|
|
495
|
+
self.path = self.tmp / "f.json"
|
|
496
|
+
self.path.write_text(json.dumps(self.data()), encoding="utf-8")
|
|
497
|
+
|
|
498
|
+
def tearDown(self):
|
|
499
|
+
i18n.set_lang("en")
|
|
500
|
+
|
|
501
|
+
def test_check_markdown_has_the_key_sections(self):
|
|
502
|
+
code, out, _ = run("check", str(DESIGN))
|
|
503
|
+
self.assertEqual(code, 0)
|
|
504
|
+
for h in ("## Summary", "## Top 3 attacks by impact", "## Assumption matrix", "## Verify next",
|
|
505
|
+
"## All accepted attacks", "## Class coverage", "not a probability"):
|
|
506
|
+
self.assertIn(h, out)
|
|
507
|
+
self.assertNotIn("## Rejected findings", out)
|
|
508
|
+
|
|
509
|
+
def test_check_json_is_stable_and_complete(self):
|
|
510
|
+
code, out, _ = run("check", str(DESIGN), "--format", "json")
|
|
511
|
+
d = json.loads(out)
|
|
512
|
+
self.assertEqual((code, d["schema"], d["top"], d["worst_severity"]), (0, "clarent.redteam.report/1", ["X3", "X1", "X2"], "S1"))
|
|
513
|
+
self.assertEqual(d["counts"], {"verified": 1, "unverified": 3, "contradicted": 2, "attacks": 7, "rejected": 0})
|
|
514
|
+
self.assertEqual(len(d["coverage"]), 6)
|
|
515
|
+
|
|
516
|
+
def test_rejections_exit_1_and_are_listed(self):
|
|
517
|
+
code, out, _ = run("check", str(REJECTED))
|
|
518
|
+
self.assertEqual(code, 1)
|
|
519
|
+
self.assertIn("## Rejected findings", out)
|
|
520
|
+
self.assertIn("line out of range (the file has 27 lines)", out)
|
|
521
|
+
|
|
522
|
+
def test_validate_exit_codes(self):
|
|
523
|
+
self.assertEqual(run("validate", str(DESIGN))[0], 0)
|
|
524
|
+
code, out, _ = run("validate", str(REJECTED))
|
|
525
|
+
self.assertEqual(code, 1)
|
|
526
|
+
self.assertIn("rejected assumption A3", out)
|
|
527
|
+
|
|
528
|
+
def test_fail_on_severity(self):
|
|
529
|
+
self.assertEqual(run("check", str(DESIGN), "--fail-on", "S1")[0], 1)
|
|
530
|
+
self.assertEqual(run("check", str(PR), "--fail-on", "S1")[0], 0) # worst is S2
|
|
531
|
+
self.assertEqual(run("check", str(PR), "--fail-on", "S2")[0], 1)
|
|
532
|
+
code, _, err = run("check", str(PR), "--fail-on", "S9")
|
|
533
|
+
self.assertEqual(code, 2)
|
|
534
|
+
self.assertIn("--fail-on", err)
|
|
535
|
+
|
|
536
|
+
def test_strict_fails_on_warnings(self):
|
|
537
|
+
self.assertEqual(run("check", str(self.path))[0], 0)
|
|
538
|
+
self.assertEqual(run("check", str(self.path), "--strict")[0], 1)
|
|
539
|
+
|
|
540
|
+
def test_out_writes_a_file(self):
|
|
541
|
+
out = self.tmp / "report.md"
|
|
542
|
+
code, stdout, _ = run("check", str(DESIGN), "--out", str(out))
|
|
543
|
+
self.assertEqual(code, 0)
|
|
544
|
+
self.assertIn("report written", stdout)
|
|
545
|
+
self.assertIn("# Red team", out.read_text(encoding="utf-8"))
|
|
546
|
+
|
|
547
|
+
def test_top_prints_one_line_per_attack(self):
|
|
548
|
+
code, out, _ = run("top", str(DESIGN))
|
|
549
|
+
self.assertEqual(code, 0)
|
|
550
|
+
self.assertEqual(len(out.strip().splitlines()), 3)
|
|
551
|
+
self.assertTrue(out.startswith("1. X3 [S1 risk=25] (reversibility)"))
|
|
552
|
+
|
|
553
|
+
def test_target_option_replaces_the_files_in_the_findings(self):
|
|
554
|
+
other = self.tmp / "other.md"
|
|
555
|
+
other.write_text("nothing relevant is written in this file\n", encoding="utf-8")
|
|
556
|
+
code, out, _ = run("check", str(self.path), "--target", str(other))
|
|
557
|
+
self.assertEqual(code, 1) # every quote is now missing
|
|
558
|
+
self.assertIn("does not appear in the target", out)
|
|
559
|
+
|
|
560
|
+
def test_root_option_moves_ref_resolution(self):
|
|
561
|
+
d = self.data()
|
|
562
|
+
d["assumptions"][0].update(status="verified", evidence=[{"ref": "sub/t.md:1", "observation": "x"}])
|
|
563
|
+
(self.tmp / "sub").mkdir()
|
|
564
|
+
(self.tmp / "sub" / "t.md").write_text("line\n", encoding="utf-8")
|
|
565
|
+
self.path.write_text(json.dumps(d), encoding="utf-8")
|
|
566
|
+
self.assertEqual(run("validate", str(self.path))[0], 0)
|
|
567
|
+
self.assertEqual(run("validate", str(self.path), "--root", str(self.tmp / "sub"))[0], 1) # sub/sub/t.md does not exist
|
|
568
|
+
|
|
569
|
+
def test_bad_input_gives_a_clean_error_not_a_traceback(self):
|
|
570
|
+
bad = self.tmp / "bad.json"
|
|
571
|
+
bad.write_text("{not json", encoding="utf-8")
|
|
572
|
+
for argv in (("check", str(bad)), ("check", str(self.tmp / "nope.json")), ("validate", str(bad))):
|
|
573
|
+
code, out, err = run(*argv)
|
|
574
|
+
self.assertEqual(code, 2, argv)
|
|
575
|
+
self.assertTrue(err.startswith("error:"), argv)
|
|
576
|
+
self.assertNotIn("Traceback", err)
|
|
577
|
+
|
|
578
|
+
def test_init_creates_a_file_that_runs(self):
|
|
579
|
+
out = self.tmp / "new" / "rt.json"
|
|
580
|
+
code, stdout, _ = run("init", str(out), "--target", str(self.tmp / "t.md"))
|
|
581
|
+
self.assertEqual(code, 0)
|
|
582
|
+
data = json.loads(out.read_text(encoding="utf-8"))
|
|
583
|
+
self.assertEqual(data["schema"], "clarent.redteam/1")
|
|
584
|
+
self.assertEqual(data["target"]["files"], ["t.md"])
|
|
585
|
+
self.assertEqual(data["root"], "..") # the target is outside cwd: its own folder is the root
|
|
586
|
+
self.assertEqual(run("validate", str(out))[0], 0)
|
|
587
|
+
self.assertEqual(run("init", str(out), "--target", str(self.tmp / "t.md"))[0], 2) # refuses to overwrite
|
|
588
|
+
self.assertEqual(run("init", str(out), "--target", str(self.tmp / "t.md"), "--force")[0], 0)
|
|
589
|
+
|
|
590
|
+
def test_init_needs_a_readable_target_with_a_usable_line(self):
|
|
591
|
+
self.assertEqual(run("init", str(self.tmp / "a.json"), "--target", str(self.tmp / "missing.md"))[0], 2)
|
|
592
|
+
(self.tmp / "tiny.md").write_text("hi\n", encoding="utf-8")
|
|
593
|
+
self.assertEqual(run("init", str(self.tmp / "b.json"), "--target", str(self.tmp / "tiny.md"))[0], 2)
|
|
594
|
+
|
|
595
|
+
def test_catalog_lists_the_six_classes_and_the_scales(self):
|
|
596
|
+
code, out, _ = run("catalog")
|
|
597
|
+
self.assertEqual(code, 0)
|
|
598
|
+
for c in catalog.CLASSES:
|
|
599
|
+
self.assertIn(c, out)
|
|
600
|
+
self.assertIn("impact (1-5):", out)
|
|
601
|
+
self.assertIn("plausibility (1-5):", out)
|
|
602
|
+
self.assertIn("p. 151", out)
|
|
603
|
+
|
|
604
|
+
def test_spanish_output_and_same_numbers(self):
|
|
605
|
+
code, es, _ = run("--lang", "es", "check", str(DESIGN_ES), "--format", "json")
|
|
606
|
+
en = json.loads(run("--lang", "en", "check", str(DESIGN_ES), "--format", "json")[1])
|
|
607
|
+
self.assertEqual(json.loads(es)["top"], en["top"])
|
|
608
|
+
code, md, _ = run("--lang", "es", "check", str(DESIGN_ES))
|
|
609
|
+
self.assertIn("## Matriz de supuestos", md)
|
|
610
|
+
self.assertIn("contradicho", md)
|
|
611
|
+
self.assertIn("Reversibilidad", md)
|
|
612
|
+
|
|
613
|
+
def test_unsupported_language_rejected(self):
|
|
614
|
+
with self.assertRaises(SystemExit), redirect_stderr(io.StringIO()):
|
|
615
|
+
main(["--lang", "xx", "catalog"])
|
|
616
|
+
|
|
617
|
+
def test_table_cells_escape_pipes_and_newlines(self):
|
|
618
|
+
d = self.data()
|
|
619
|
+
d["attacks"][0]["attack"] = "a | b\nbroken table risk"
|
|
620
|
+
self.path.write_text(json.dumps(d), encoding="utf-8")
|
|
621
|
+
_, out, _ = run("check", str(self.path))
|
|
622
|
+
self.assertIn("a \\| b broken table risk", out)
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
class CoreIntegrationTests(unittest.TestCase):
|
|
626
|
+
def test_redteam_is_discovered_and_runs_through_the_core(self):
|
|
627
|
+
root = Path(__file__).resolve().parents[3]
|
|
628
|
+
sys.path.insert(0, str(root / "src"))
|
|
629
|
+
from clarent import cli, registry
|
|
630
|
+
self.assertIn("redteam", [m.id for m in registry.discover(root / "methods")])
|
|
631
|
+
m = next(m for m in registry.discover(root / "methods") if m.id == "redteam")
|
|
632
|
+
self.assertEqual(registry.doctor(m), [])
|
|
633
|
+
out = io.StringIO()
|
|
634
|
+
with redirect_stdout(out):
|
|
635
|
+
code = cli.main(["redteam", "check", str(DESIGN), "--format", "json"])
|
|
636
|
+
self.assertEqual((code, json.loads(out.getvalue())["top"]), (0, ["X3", "X1", "X2"]))
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
if __name__ == "__main__":
|
|
640
|
+
unittest.main()
|