serial-claim-audit 0.1.0__tar.gz
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.
- serial_claim_audit-0.1.0/LICENSE +21 -0
- serial_claim_audit-0.1.0/MANIFEST.in +2 -0
- serial_claim_audit-0.1.0/PKG-INFO +139 -0
- serial_claim_audit-0.1.0/README.md +129 -0
- serial_claim_audit-0.1.0/pyproject.toml +17 -0
- serial_claim_audit-0.1.0/serial_claim_audit/__init__.py +2 -0
- serial_claim_audit-0.1.0/serial_claim_audit/__main__.py +43 -0
- serial_claim_audit-0.1.0/serial_claim_audit/audit.py +121 -0
- serial_claim_audit-0.1.0/serial_claim_audit/phrases.py +3 -0
- serial_claim_audit-0.1.0/serial_claim_audit.egg-info/PKG-INFO +139 -0
- serial_claim_audit-0.1.0/serial_claim_audit.egg-info/SOURCES.txt +25 -0
- serial_claim_audit-0.1.0/serial_claim_audit.egg-info/dependency_links.txt +1 -0
- serial_claim_audit-0.1.0/serial_claim_audit.egg-info/entry_points.txt +2 -0
- serial_claim_audit-0.1.0/serial_claim_audit.egg-info/top_level.txt +1 -0
- serial_claim_audit-0.1.0/setup.cfg +4 -0
- serial_claim_audit-0.1.0/tests/__init__.py +0 -0
- serial_claim_audit-0.1.0/tests/cases.py +36 -0
- serial_claim_audit-0.1.0/tests/fixtures/.env +1 -0
- serial_claim_audit-0.1.0/tests/fixtures/bad/README.md +1 -0
- serial_claim_audit-0.1.0/tests/fixtures/bad/app.py +1 -0
- serial_claim_audit-0.1.0/tests/fixtures/good/README.md +1 -0
- serial_claim_audit-0.1.0/tests/fixtures/good/app.py +2 -0
- serial_claim_audit-0.1.0/tests/fixtures/no-serial/README.md +1 -0
- serial_claim_audit-0.1.0/tests/fixtures/no-serial/app.py +2 -0
- serial_claim_audit-0.1.0/tests/fixtures/testonly/README.md +1 -0
- serial_claim_audit-0.1.0/tests/fixtures/testonly/tests/test_port.py +2 -0
- serial_claim_audit-0.1.0/tests/test_audit.py +178 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 serial-claim-audit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: serial-claim-audit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Offline static audit of documented wired port claims
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# serial-claim-audit
|
|
12
|
+
|
|
13
|
+
Version 0.1.0. Python >=3.9. MIT licensed. No runtime dependencies.
|
|
14
|
+
|
|
15
|
+
An offline static checker comparing documented wired port claims with Python
|
|
16
|
+
AST evidence. Audited code is never executed; no hardware or provider package
|
|
17
|
+
is needed.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
python3 -m pip install .
|
|
21
|
+
serial-claim-audit ./project --json
|
|
22
|
+
python3 -m serial_claim_audit ./project --verbose
|
|
23
|
+
python3 -m unittest discover
|
|
24
|
+
python3 -m build
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Root README.md and Markdown beneath any docs path component supply claims.
|
|
28
|
+
Fenced blocks are excluded; inline code counts. Matching lowercases text,
|
|
29
|
+
expands contractions ending in n't, converts hyphens to spaces, and joins
|
|
30
|
+
whitespace within blank-line-separated paragraphs. Word boundaries and exact
|
|
31
|
+
phrases apply, longest first; negative spans are removed before positive checks.
|
|
32
|
+
|
|
33
|
+
## Phrase vocabulary
|
|
34
|
+
|
|
35
|
+
Only the following templates count:
|
|
36
|
+
|
|
37
|
+
```text
|
|
38
|
+
connects to serial devices
|
|
39
|
+
connect to serial devices
|
|
40
|
+
connects over serial
|
|
41
|
+
connect over serial
|
|
42
|
+
communicates over serial
|
|
43
|
+
communicate over serial
|
|
44
|
+
communicates via serial
|
|
45
|
+
communicate via serial
|
|
46
|
+
reads from serial port
|
|
47
|
+
read from serial port
|
|
48
|
+
writes to serial port
|
|
49
|
+
write to serial port
|
|
50
|
+
reads data from serial port
|
|
51
|
+
read data from serial port
|
|
52
|
+
writes data to serial port
|
|
53
|
+
write data to serial port
|
|
54
|
+
sends data over serial
|
|
55
|
+
send data over serial
|
|
56
|
+
receives data over serial
|
|
57
|
+
receive data over serial
|
|
58
|
+
opens serial ports
|
|
59
|
+
open serial ports
|
|
60
|
+
serial port support
|
|
61
|
+
serial device support
|
|
62
|
+
serial connectivity
|
|
63
|
+
supports serial ports
|
|
64
|
+
support serial ports
|
|
65
|
+
supports serial
|
|
66
|
+
support serial
|
|
67
|
+
uses serial port
|
|
68
|
+
use serial port
|
|
69
|
+
uses serial
|
|
70
|
+
use serial
|
|
71
|
+
does not connect to serial devices
|
|
72
|
+
does not connect over serial
|
|
73
|
+
does not communicate over serial
|
|
74
|
+
does not communicate via serial
|
|
75
|
+
does not read from serial port
|
|
76
|
+
does not write to serial port
|
|
77
|
+
does not send data over serial
|
|
78
|
+
does not receive data over serial
|
|
79
|
+
does not support serial ports
|
|
80
|
+
does not support serial
|
|
81
|
+
does not use serial port
|
|
82
|
+
does not use serial
|
|
83
|
+
serial port not supported
|
|
84
|
+
serial not supported
|
|
85
|
+
no serial port support
|
|
86
|
+
without serial port support
|
|
87
|
+
no serial support
|
|
88
|
+
without serial support
|
|
89
|
+
no serial connectivity
|
|
90
|
+
without serial connectivity
|
|
91
|
+
no serial
|
|
92
|
+
without serial
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Production evidence consists of provider-resolved pyserial constructors or
|
|
96
|
+
factories, asynchronous provider connection calls, and transfers on their
|
|
97
|
+
results. Imports and aliases are resolved per module; local class/function
|
|
98
|
+
names shadow imported names. Bare imports, strings, ordinary file opens,
|
|
99
|
+
unbound transfers, USB handles, printer objects, logging functions, decorators,
|
|
100
|
+
comments and docstrings do not establish evidence.
|
|
101
|
+
|
|
102
|
+
## Rules and CLI
|
|
103
|
+
|
|
104
|
+
`documented-serial-claim-false` reports HIGH when a positive template lacks
|
|
105
|
+
production evidence, or MEDIUM when evidence exists only in test source.
|
|
106
|
+
The negative rule below reports HIGH when a negative template
|
|
107
|
+
contradicts production evidence.
|
|
108
|
+
|
|
109
|
+
```text
|
|
110
|
+
documented-no-serial-claim-false
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
At most one finding per family is emitted.
|
|
114
|
+
Absent claims always pass. With no source, a positive template fails and a
|
|
115
|
+
negative template passes.
|
|
116
|
+
|
|
117
|
+
The positional root defaults to the current directory. `--json` emits valid
|
|
118
|
+
JSON containing verdict and findings with rule, severity, and message.
|
|
119
|
+
`--verbose` adds scan counts to human output. Repeat `--ignore GLOB` to exclude
|
|
120
|
+
relative paths. `--fail-on HIGH` is the default; MEDIUM findings are reported
|
|
121
|
+
without failure. `--fail-on MEDIUM` also fails on MEDIUM. Exit status is 0 for
|
|
122
|
+
PASS, 2 for FAIL, and 1 for an invalid or unreadable root. `--version` prints
|
|
123
|
+
0.1.0. Both entry points accept the same options.
|
|
124
|
+
|
|
125
|
+
## Limitations
|
|
126
|
+
|
|
127
|
+
Only the exact templates listed in the fenced block are recognized. A bare
|
|
128
|
+
protocol word, user cable setup instructions, USB reach, printing, Bluetooth
|
|
129
|
+
links, and file exports are outside this vocabulary. System terminal helpers
|
|
130
|
+
invoked through subprocesses are outside the evidence model: shell command
|
|
131
|
+
names in string literals never count.
|
|
132
|
+
|
|
133
|
+
This is syntax analysis, not runtime verification. Calls may be unreachable,
|
|
134
|
+
ports may be invalid, and control flow, dynamic imports, rebinding, and imported
|
|
135
|
+
wrappers are not evaluated. Syntax-invalid modules provide no evidence.
|
|
136
|
+
Source under tests, test-prefixed files and test-suffixed files is test scope;
|
|
137
|
+
fixtures, docs, and examples are excluded from evidence. Symlinked files and
|
|
138
|
+
directories are skipped. Environment files are never opened. No network or
|
|
139
|
+
hardware inspection occurs.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# serial-claim-audit
|
|
2
|
+
|
|
3
|
+
Version 0.1.0. Python >=3.9. MIT licensed. No runtime dependencies.
|
|
4
|
+
|
|
5
|
+
An offline static checker comparing documented wired port claims with Python
|
|
6
|
+
AST evidence. Audited code is never executed; no hardware or provider package
|
|
7
|
+
is needed.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
python3 -m pip install .
|
|
11
|
+
serial-claim-audit ./project --json
|
|
12
|
+
python3 -m serial_claim_audit ./project --verbose
|
|
13
|
+
python3 -m unittest discover
|
|
14
|
+
python3 -m build
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Root README.md and Markdown beneath any docs path component supply claims.
|
|
18
|
+
Fenced blocks are excluded; inline code counts. Matching lowercases text,
|
|
19
|
+
expands contractions ending in n't, converts hyphens to spaces, and joins
|
|
20
|
+
whitespace within blank-line-separated paragraphs. Word boundaries and exact
|
|
21
|
+
phrases apply, longest first; negative spans are removed before positive checks.
|
|
22
|
+
|
|
23
|
+
## Phrase vocabulary
|
|
24
|
+
|
|
25
|
+
Only the following templates count:
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
connects to serial devices
|
|
29
|
+
connect to serial devices
|
|
30
|
+
connects over serial
|
|
31
|
+
connect over serial
|
|
32
|
+
communicates over serial
|
|
33
|
+
communicate over serial
|
|
34
|
+
communicates via serial
|
|
35
|
+
communicate via serial
|
|
36
|
+
reads from serial port
|
|
37
|
+
read from serial port
|
|
38
|
+
writes to serial port
|
|
39
|
+
write to serial port
|
|
40
|
+
reads data from serial port
|
|
41
|
+
read data from serial port
|
|
42
|
+
writes data to serial port
|
|
43
|
+
write data to serial port
|
|
44
|
+
sends data over serial
|
|
45
|
+
send data over serial
|
|
46
|
+
receives data over serial
|
|
47
|
+
receive data over serial
|
|
48
|
+
opens serial ports
|
|
49
|
+
open serial ports
|
|
50
|
+
serial port support
|
|
51
|
+
serial device support
|
|
52
|
+
serial connectivity
|
|
53
|
+
supports serial ports
|
|
54
|
+
support serial ports
|
|
55
|
+
supports serial
|
|
56
|
+
support serial
|
|
57
|
+
uses serial port
|
|
58
|
+
use serial port
|
|
59
|
+
uses serial
|
|
60
|
+
use serial
|
|
61
|
+
does not connect to serial devices
|
|
62
|
+
does not connect over serial
|
|
63
|
+
does not communicate over serial
|
|
64
|
+
does not communicate via serial
|
|
65
|
+
does not read from serial port
|
|
66
|
+
does not write to serial port
|
|
67
|
+
does not send data over serial
|
|
68
|
+
does not receive data over serial
|
|
69
|
+
does not support serial ports
|
|
70
|
+
does not support serial
|
|
71
|
+
does not use serial port
|
|
72
|
+
does not use serial
|
|
73
|
+
serial port not supported
|
|
74
|
+
serial not supported
|
|
75
|
+
no serial port support
|
|
76
|
+
without serial port support
|
|
77
|
+
no serial support
|
|
78
|
+
without serial support
|
|
79
|
+
no serial connectivity
|
|
80
|
+
without serial connectivity
|
|
81
|
+
no serial
|
|
82
|
+
without serial
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Production evidence consists of provider-resolved pyserial constructors or
|
|
86
|
+
factories, asynchronous provider connection calls, and transfers on their
|
|
87
|
+
results. Imports and aliases are resolved per module; local class/function
|
|
88
|
+
names shadow imported names. Bare imports, strings, ordinary file opens,
|
|
89
|
+
unbound transfers, USB handles, printer objects, logging functions, decorators,
|
|
90
|
+
comments and docstrings do not establish evidence.
|
|
91
|
+
|
|
92
|
+
## Rules and CLI
|
|
93
|
+
|
|
94
|
+
`documented-serial-claim-false` reports HIGH when a positive template lacks
|
|
95
|
+
production evidence, or MEDIUM when evidence exists only in test source.
|
|
96
|
+
The negative rule below reports HIGH when a negative template
|
|
97
|
+
contradicts production evidence.
|
|
98
|
+
|
|
99
|
+
```text
|
|
100
|
+
documented-no-serial-claim-false
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
At most one finding per family is emitted.
|
|
104
|
+
Absent claims always pass. With no source, a positive template fails and a
|
|
105
|
+
negative template passes.
|
|
106
|
+
|
|
107
|
+
The positional root defaults to the current directory. `--json` emits valid
|
|
108
|
+
JSON containing verdict and findings with rule, severity, and message.
|
|
109
|
+
`--verbose` adds scan counts to human output. Repeat `--ignore GLOB` to exclude
|
|
110
|
+
relative paths. `--fail-on HIGH` is the default; MEDIUM findings are reported
|
|
111
|
+
without failure. `--fail-on MEDIUM` also fails on MEDIUM. Exit status is 0 for
|
|
112
|
+
PASS, 2 for FAIL, and 1 for an invalid or unreadable root. `--version` prints
|
|
113
|
+
0.1.0. Both entry points accept the same options.
|
|
114
|
+
|
|
115
|
+
## Limitations
|
|
116
|
+
|
|
117
|
+
Only the exact templates listed in the fenced block are recognized. A bare
|
|
118
|
+
protocol word, user cable setup instructions, USB reach, printing, Bluetooth
|
|
119
|
+
links, and file exports are outside this vocabulary. System terminal helpers
|
|
120
|
+
invoked through subprocesses are outside the evidence model: shell command
|
|
121
|
+
names in string literals never count.
|
|
122
|
+
|
|
123
|
+
This is syntax analysis, not runtime verification. Calls may be unreachable,
|
|
124
|
+
ports may be invalid, and control flow, dynamic imports, rebinding, and imported
|
|
125
|
+
wrappers are not evaluated. Syntax-invalid modules provide no evidence.
|
|
126
|
+
Source under tests, test-prefixed files and test-suffixed files is test scope;
|
|
127
|
+
fixtures, docs, and examples are excluded from evidence. Symlinked files and
|
|
128
|
+
directories are skipped. Environment files are never opened. No network or
|
|
129
|
+
hardware inspection occurs.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "serial-claim-audit"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Offline static audit of documented wired port claims"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
|
|
13
|
+
[project.scripts]
|
|
14
|
+
serial-claim-audit = "serial_claim_audit.__main__:main"
|
|
15
|
+
|
|
16
|
+
[tool.setuptools.packages.find]
|
|
17
|
+
include = ["serial_claim_audit*"]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Command line interface."""
|
|
2
|
+
import argparse
|
|
3
|
+
import json
|
|
4
|
+
from . import __version__
|
|
5
|
+
from .audit import audit
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ArgumentParser(argparse.ArgumentParser):
|
|
9
|
+
def error(self, message):
|
|
10
|
+
self.print_usage()
|
|
11
|
+
self.exit(1, "Error: " + message + "\n")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main(argv=None):
|
|
15
|
+
parser = ArgumentParser(description='Audit documented wired port claims offline.')
|
|
16
|
+
parser.add_argument('root', nargs='?', default='.')
|
|
17
|
+
parser.add_argument('--json', action='store_true')
|
|
18
|
+
parser.add_argument('--verbose', action='store_true')
|
|
19
|
+
parser.add_argument('--ignore', action='append', default=[])
|
|
20
|
+
parser.add_argument('--fail-on', choices=('HIGH', 'MEDIUM'), default='HIGH')
|
|
21
|
+
parser.add_argument('--version', action='version', version=__version__)
|
|
22
|
+
args = parser.parse_args(argv)
|
|
23
|
+
try:
|
|
24
|
+
result = audit(args.root, args.ignore, args.fail_on)
|
|
25
|
+
except (ValueError, OSError) as exc:
|
|
26
|
+
if args.json:
|
|
27
|
+
print(json.dumps({'error': str(exc)}))
|
|
28
|
+
else:
|
|
29
|
+
print('Error: ' + str(exc))
|
|
30
|
+
return 1
|
|
31
|
+
if args.json:
|
|
32
|
+
print(json.dumps(result))
|
|
33
|
+
else:
|
|
34
|
+
print(result['verdict'])
|
|
35
|
+
for finding in result['findings']:
|
|
36
|
+
print('{severity} {rule}: {message}'.format(**finding))
|
|
37
|
+
if args.verbose:
|
|
38
|
+
print('Scanned {} files'.format(result['scanned_files']))
|
|
39
|
+
return 2 if result['verdict'] == 'FAIL' else 0
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
if __name__ == '__main__':
|
|
43
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Static documentation and provider evidence analysis; never executes source."""
|
|
2
|
+
import ast
|
|
3
|
+
import fnmatch
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from .phrases import SERIAL, NOSERIAL
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def normalize(text):
|
|
11
|
+
return re.sub(r"\s+", " ", text.lower().replace("n't", " not").replace("-", " "))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def claims(text):
|
|
15
|
+
prose = []
|
|
16
|
+
fence = None
|
|
17
|
+
for line in text.splitlines():
|
|
18
|
+
match = re.match(r"^\s{0,3}(`{3,}|~{3,})", line)
|
|
19
|
+
if match:
|
|
20
|
+
mark = match.group(1)
|
|
21
|
+
if fence is None:
|
|
22
|
+
fence = mark
|
|
23
|
+
elif mark[0] == fence[0] and len(mark) >= len(fence):
|
|
24
|
+
fence = None
|
|
25
|
+
continue
|
|
26
|
+
if fence is None:
|
|
27
|
+
prose.append(line)
|
|
28
|
+
positive = negative = False
|
|
29
|
+
for paragraph in re.split(r"\n\s*\n", "\n".join(prose)):
|
|
30
|
+
text = normalize(paragraph)
|
|
31
|
+
for phrase in NOSERIAL:
|
|
32
|
+
pattern = r"\b" + re.escape(phrase) + r"\b"
|
|
33
|
+
if re.search(pattern, text):
|
|
34
|
+
negative = True
|
|
35
|
+
text = re.sub(pattern, " ", text)
|
|
36
|
+
positive |= any(re.search(r"\b" + re.escape(p) + r"\b", text) is not None for p in SERIAL)
|
|
37
|
+
return positive, negative
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def evidence(source):
|
|
41
|
+
try:
|
|
42
|
+
tree = ast.parse(source)
|
|
43
|
+
except (SyntaxError, ValueError):
|
|
44
|
+
return False
|
|
45
|
+
imports = {}
|
|
46
|
+
shadowed = set()
|
|
47
|
+
for node in ast.walk(tree):
|
|
48
|
+
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
|
|
49
|
+
shadowed.add(node.name)
|
|
50
|
+
elif isinstance(node, ast.Import):
|
|
51
|
+
for item in node.names:
|
|
52
|
+
imports[item.asname or item.name.split('.')[0]] = item.name if item.asname else item.name.split('.')[0]
|
|
53
|
+
elif isinstance(node, ast.ImportFrom) and node.module:
|
|
54
|
+
for item in node.names:
|
|
55
|
+
imports[item.asname or item.name] = node.module + '.' + item.name
|
|
56
|
+
def resolve(node):
|
|
57
|
+
if isinstance(node, ast.Name):
|
|
58
|
+
return None if node.id in shadowed else imports.get(node.id)
|
|
59
|
+
if isinstance(node, ast.Attribute):
|
|
60
|
+
base = resolve(node.value)
|
|
61
|
+
return base + '.' + node.attr if base else None
|
|
62
|
+
return None
|
|
63
|
+
def route(node):
|
|
64
|
+
if not isinstance(node, ast.Call):
|
|
65
|
+
return False
|
|
66
|
+
name = resolve(node.func)
|
|
67
|
+
if not name:
|
|
68
|
+
return False
|
|
69
|
+
parts = name.split('.')
|
|
70
|
+
return ((parts[0] == 'serial' and parts[-1] in ('Serial', 'serial_for_url')) or
|
|
71
|
+
(parts[0] == 'serial_asyncio' and parts[-1] == 'create_serial_connection'))
|
|
72
|
+
# Every transfer receiver bound to a provider result necessarily contains
|
|
73
|
+
# an A/B/C call in this same module, so that call also establishes D.
|
|
74
|
+
return any(route(node) for node in ast.walk(tree))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def audit(root, ignore=(), fail_on='HIGH'):
|
|
78
|
+
root = Path(root)
|
|
79
|
+
if not root.is_dir():
|
|
80
|
+
raise ValueError('root must be an existing readable directory')
|
|
81
|
+
if not os.access(root, os.R_OK | os.X_OK):
|
|
82
|
+
raise ValueError('root is unreadable')
|
|
83
|
+
positive = negative = prod = test = False
|
|
84
|
+
scanned = 0
|
|
85
|
+
for directory, dirs, files in os.walk(root):
|
|
86
|
+
dirs[:] = sorted(d for d in dirs if not (Path(directory) / d).is_symlink()
|
|
87
|
+
and not any(fnmatch.fnmatch((Path(directory) / d).relative_to(root).as_posix(), g) for g in ignore))
|
|
88
|
+
for filename in sorted(files):
|
|
89
|
+
path = Path(directory) / filename
|
|
90
|
+
relative = path.relative_to(root)
|
|
91
|
+
parts = relative.parts
|
|
92
|
+
if path.is_symlink() or any(fnmatch.fnmatch(relative.as_posix(), g) for g in ignore):
|
|
93
|
+
continue
|
|
94
|
+
is_doc = relative.as_posix() == 'README.md' or ('docs' in parts[:-1] and path.suffix.lower() == '.md')
|
|
95
|
+
is_test = 'tests' in parts[:-1] or filename.startswith('test_') or filename.endswith('_test.py')
|
|
96
|
+
excluded = any(p in ('fixtures', 'docs', 'examples') for p in parts[:-1])
|
|
97
|
+
if not is_doc and not (path.suffix == '.py' and not excluded):
|
|
98
|
+
continue
|
|
99
|
+
try:
|
|
100
|
+
content = path.read_text(encoding='utf-8')
|
|
101
|
+
except (OSError, UnicodeError) as exc:
|
|
102
|
+
raise ValueError('cannot read ' + relative.as_posix()) from exc
|
|
103
|
+
scanned += 1
|
|
104
|
+
if is_doc:
|
|
105
|
+
p, n = claims(content)
|
|
106
|
+
positive |= p
|
|
107
|
+
negative |= n
|
|
108
|
+
elif evidence(content):
|
|
109
|
+
if is_test:
|
|
110
|
+
test = True
|
|
111
|
+
else:
|
|
112
|
+
prod = True
|
|
113
|
+
findings = []
|
|
114
|
+
if positive and not prod:
|
|
115
|
+
findings.append(dict(rule='documented-serial-claim-false', severity='MEDIUM' if test else 'HIGH',
|
|
116
|
+
message='Documented wired port capability has evidence only in tests.' if test else 'Documented wired port capability lacks production evidence.'))
|
|
117
|
+
if negative and prod:
|
|
118
|
+
findings.append(dict(rule='documented-no-serial-claim-false', severity='HIGH',
|
|
119
|
+
message='Documented absence of wired port capability contradicts production evidence.'))
|
|
120
|
+
failed = any(f['severity'] == 'HIGH' or fail_on == 'MEDIUM' for f in findings)
|
|
121
|
+
return dict(verdict='FAIL' if failed else 'PASS', findings=findings, scanned_files=scanned)
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"""Exact documented phrase families."""
|
|
2
|
+
SERIAL = ('reads data from serial port', 'connects to serial devices', 'read data from serial port', 'writes data to serial port', 'connect to serial devices', 'write data to serial port', 'receives data over serial', 'communicates over serial', 'receive data over serial', 'communicate over serial', 'communicates via serial', 'communicate via serial', 'reads from serial port', 'sends data over serial', 'read from serial port', 'writes to serial port', 'send data over serial', 'serial device support', 'supports serial ports', 'connects over serial', 'write to serial port', 'support serial ports', 'connect over serial', 'serial port support', 'serial connectivity', 'opens serial ports', 'open serial ports', 'uses serial port', 'supports serial', 'use serial port', 'support serial', 'uses serial', 'use serial')
|
|
3
|
+
NOSERIAL = ('does not connect to serial devices', 'does not receive data over serial', 'does not communicate over serial', 'does not communicate via serial', 'does not read from serial port', 'does not send data over serial', 'does not write to serial port', 'does not support serial ports', 'does not connect over serial', 'without serial port support', 'without serial connectivity', 'serial port not supported', 'does not use serial port', 'does not support serial', 'no serial port support', 'without serial support', 'no serial connectivity', 'serial not supported', 'does not use serial', 'no serial support', 'without serial', 'no serial')
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: serial-claim-audit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Offline static audit of documented wired port claims
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# serial-claim-audit
|
|
12
|
+
|
|
13
|
+
Version 0.1.0. Python >=3.9. MIT licensed. No runtime dependencies.
|
|
14
|
+
|
|
15
|
+
An offline static checker comparing documented wired port claims with Python
|
|
16
|
+
AST evidence. Audited code is never executed; no hardware or provider package
|
|
17
|
+
is needed.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
python3 -m pip install .
|
|
21
|
+
serial-claim-audit ./project --json
|
|
22
|
+
python3 -m serial_claim_audit ./project --verbose
|
|
23
|
+
python3 -m unittest discover
|
|
24
|
+
python3 -m build
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Root README.md and Markdown beneath any docs path component supply claims.
|
|
28
|
+
Fenced blocks are excluded; inline code counts. Matching lowercases text,
|
|
29
|
+
expands contractions ending in n't, converts hyphens to spaces, and joins
|
|
30
|
+
whitespace within blank-line-separated paragraphs. Word boundaries and exact
|
|
31
|
+
phrases apply, longest first; negative spans are removed before positive checks.
|
|
32
|
+
|
|
33
|
+
## Phrase vocabulary
|
|
34
|
+
|
|
35
|
+
Only the following templates count:
|
|
36
|
+
|
|
37
|
+
```text
|
|
38
|
+
connects to serial devices
|
|
39
|
+
connect to serial devices
|
|
40
|
+
connects over serial
|
|
41
|
+
connect over serial
|
|
42
|
+
communicates over serial
|
|
43
|
+
communicate over serial
|
|
44
|
+
communicates via serial
|
|
45
|
+
communicate via serial
|
|
46
|
+
reads from serial port
|
|
47
|
+
read from serial port
|
|
48
|
+
writes to serial port
|
|
49
|
+
write to serial port
|
|
50
|
+
reads data from serial port
|
|
51
|
+
read data from serial port
|
|
52
|
+
writes data to serial port
|
|
53
|
+
write data to serial port
|
|
54
|
+
sends data over serial
|
|
55
|
+
send data over serial
|
|
56
|
+
receives data over serial
|
|
57
|
+
receive data over serial
|
|
58
|
+
opens serial ports
|
|
59
|
+
open serial ports
|
|
60
|
+
serial port support
|
|
61
|
+
serial device support
|
|
62
|
+
serial connectivity
|
|
63
|
+
supports serial ports
|
|
64
|
+
support serial ports
|
|
65
|
+
supports serial
|
|
66
|
+
support serial
|
|
67
|
+
uses serial port
|
|
68
|
+
use serial port
|
|
69
|
+
uses serial
|
|
70
|
+
use serial
|
|
71
|
+
does not connect to serial devices
|
|
72
|
+
does not connect over serial
|
|
73
|
+
does not communicate over serial
|
|
74
|
+
does not communicate via serial
|
|
75
|
+
does not read from serial port
|
|
76
|
+
does not write to serial port
|
|
77
|
+
does not send data over serial
|
|
78
|
+
does not receive data over serial
|
|
79
|
+
does not support serial ports
|
|
80
|
+
does not support serial
|
|
81
|
+
does not use serial port
|
|
82
|
+
does not use serial
|
|
83
|
+
serial port not supported
|
|
84
|
+
serial not supported
|
|
85
|
+
no serial port support
|
|
86
|
+
without serial port support
|
|
87
|
+
no serial support
|
|
88
|
+
without serial support
|
|
89
|
+
no serial connectivity
|
|
90
|
+
without serial connectivity
|
|
91
|
+
no serial
|
|
92
|
+
without serial
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Production evidence consists of provider-resolved pyserial constructors or
|
|
96
|
+
factories, asynchronous provider connection calls, and transfers on their
|
|
97
|
+
results. Imports and aliases are resolved per module; local class/function
|
|
98
|
+
names shadow imported names. Bare imports, strings, ordinary file opens,
|
|
99
|
+
unbound transfers, USB handles, printer objects, logging functions, decorators,
|
|
100
|
+
comments and docstrings do not establish evidence.
|
|
101
|
+
|
|
102
|
+
## Rules and CLI
|
|
103
|
+
|
|
104
|
+
`documented-serial-claim-false` reports HIGH when a positive template lacks
|
|
105
|
+
production evidence, or MEDIUM when evidence exists only in test source.
|
|
106
|
+
The negative rule below reports HIGH when a negative template
|
|
107
|
+
contradicts production evidence.
|
|
108
|
+
|
|
109
|
+
```text
|
|
110
|
+
documented-no-serial-claim-false
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
At most one finding per family is emitted.
|
|
114
|
+
Absent claims always pass. With no source, a positive template fails and a
|
|
115
|
+
negative template passes.
|
|
116
|
+
|
|
117
|
+
The positional root defaults to the current directory. `--json` emits valid
|
|
118
|
+
JSON containing verdict and findings with rule, severity, and message.
|
|
119
|
+
`--verbose` adds scan counts to human output. Repeat `--ignore GLOB` to exclude
|
|
120
|
+
relative paths. `--fail-on HIGH` is the default; MEDIUM findings are reported
|
|
121
|
+
without failure. `--fail-on MEDIUM` also fails on MEDIUM. Exit status is 0 for
|
|
122
|
+
PASS, 2 for FAIL, and 1 for an invalid or unreadable root. `--version` prints
|
|
123
|
+
0.1.0. Both entry points accept the same options.
|
|
124
|
+
|
|
125
|
+
## Limitations
|
|
126
|
+
|
|
127
|
+
Only the exact templates listed in the fenced block are recognized. A bare
|
|
128
|
+
protocol word, user cable setup instructions, USB reach, printing, Bluetooth
|
|
129
|
+
links, and file exports are outside this vocabulary. System terminal helpers
|
|
130
|
+
invoked through subprocesses are outside the evidence model: shell command
|
|
131
|
+
names in string literals never count.
|
|
132
|
+
|
|
133
|
+
This is syntax analysis, not runtime verification. Calls may be unreachable,
|
|
134
|
+
ports may be invalid, and control flow, dynamic imports, rebinding, and imported
|
|
135
|
+
wrappers are not evaluated. Syntax-invalid modules provide no evidence.
|
|
136
|
+
Source under tests, test-prefixed files and test-suffixed files is test scope;
|
|
137
|
+
fixtures, docs, and examples are excluded from evidence. Symlinked files and
|
|
138
|
+
directories are skipped. Environment files are never opened. No network or
|
|
139
|
+
hardware inspection occurs.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
serial_claim_audit/__init__.py
|
|
6
|
+
serial_claim_audit/__main__.py
|
|
7
|
+
serial_claim_audit/audit.py
|
|
8
|
+
serial_claim_audit/phrases.py
|
|
9
|
+
serial_claim_audit.egg-info/PKG-INFO
|
|
10
|
+
serial_claim_audit.egg-info/SOURCES.txt
|
|
11
|
+
serial_claim_audit.egg-info/dependency_links.txt
|
|
12
|
+
serial_claim_audit.egg-info/entry_points.txt
|
|
13
|
+
serial_claim_audit.egg-info/top_level.txt
|
|
14
|
+
tests/__init__.py
|
|
15
|
+
tests/cases.py
|
|
16
|
+
tests/test_audit.py
|
|
17
|
+
tests/fixtures/.env
|
|
18
|
+
tests/fixtures/bad/README.md
|
|
19
|
+
tests/fixtures/bad/app.py
|
|
20
|
+
tests/fixtures/good/README.md
|
|
21
|
+
tests/fixtures/good/app.py
|
|
22
|
+
tests/fixtures/no-serial/README.md
|
|
23
|
+
tests/fixtures/no-serial/app.py
|
|
24
|
+
tests/fixtures/testonly/README.md
|
|
25
|
+
tests/fixtures/testonly/tests/test_port.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
serial_claim_audit
|
|
File without changes
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
GOOD = {
|
|
2
|
+
'serial': 'from serial import Serial\nSerial("/dev/ttyUSB0")',
|
|
3
|
+
'attribute': 'import serial\nserial.Serial("port")',
|
|
4
|
+
'alias': 'import serial as ser\nser.Serial("port")',
|
|
5
|
+
'write': 'from serial import Serial\np = Serial("port")\np.write(b"x")',
|
|
6
|
+
'read': 'from serial import Serial\np = Serial("port")\np.read(1)',
|
|
7
|
+
'factory': 'import serial\nserial.serial_for_url("loop://")',
|
|
8
|
+
'asyncio': 'import serial_asyncio\nserial_asyncio.create_serial_connection(loop, protocol, "port")',
|
|
9
|
+
'chained': 'import serial\nserial.Serial("port").write(b"x")',
|
|
10
|
+
'import_alias': 'from serial import Serial as Port\nPort("p")',
|
|
11
|
+
'factory_alias': 'from serial import serial_for_url as factory\nfactory("p")',
|
|
12
|
+
'async_alias': 'from serial_asyncio import create_serial_connection as connect\nconnect(1,2,3)',
|
|
13
|
+
'async_module_alias': 'import serial_asyncio as sa\nsa.create_serial_connection(1,2,3)',
|
|
14
|
+
'dotted': 'import serial.tools\nserial.Serial("p")',
|
|
15
|
+
'submodule': 'from serial.serialposix import Serial\nSerial("p")',
|
|
16
|
+
'self_bound': 'from serial import Serial\nself.port = Serial("p")\nself.port.readline()',
|
|
17
|
+
}
|
|
18
|
+
BAD = {
|
|
19
|
+
'empty': 'print("hello")',
|
|
20
|
+
'import': 'import serial',
|
|
21
|
+
'async_import': 'import serial_asyncio',
|
|
22
|
+
'unbound': 'port.write(b"x")',
|
|
23
|
+
'string': 'PORT = "/dev/ttyUSB0"',
|
|
24
|
+
'open': 'open("/dev/ttyUSB0")',
|
|
25
|
+
'subprocess': 'import subprocess\nsubprocess.run(["stty", "-F", "/dev/ttyUSB0"])',
|
|
26
|
+
'usb': 'import usb.core\nusb.core.find(idVendor=1)',
|
|
27
|
+
'escpos': 'from escpos.printer import Serial\np = Serial("p")\np.text("x")',
|
|
28
|
+
'local': 'class Serial: pass\nSerial()',
|
|
29
|
+
'shadow_class': 'from serial import Serial\nclass Serial: pass\nSerial()',
|
|
30
|
+
'shadow_function': 'from serial import serial_for_url\ndef serial_for_url(x): return x\nserial_for_url("p")',
|
|
31
|
+
'other_import': 'from django import serializers as serial\nserial.Serial()',
|
|
32
|
+
'connect_log': 'def connect_device():\n print("connected")\nconnect_device()',
|
|
33
|
+
'decorator': '@app.get("/port")\ndef status(): return True',
|
|
34
|
+
'comment': '# import serial; serial.Serial("p")\n"""serial.Serial("p")"""',
|
|
35
|
+
'invalid': 'invalid syntax !',
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# marker only; never read
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Supports serial
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import serial
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Supports serial
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
No serial
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Supports serial
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import re
|
|
3
|
+
import subprocess
|
|
4
|
+
import tempfile
|
|
5
|
+
import unittest
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from unittest.mock import patch
|
|
8
|
+
from serial_claim_audit.audit import audit, claims, evidence
|
|
9
|
+
from serial_claim_audit.phrases import SERIAL, NOSERIAL
|
|
10
|
+
from tests.cases import GOOD, BAD
|
|
11
|
+
|
|
12
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AuditTests(unittest.TestCase):
|
|
16
|
+
def project(self, doc, source=None, path='app.py'):
|
|
17
|
+
temporary = tempfile.TemporaryDirectory()
|
|
18
|
+
self.addCleanup(temporary.cleanup)
|
|
19
|
+
root = Path(temporary.name)
|
|
20
|
+
(root / 'README.md').write_text(doc)
|
|
21
|
+
if source is not None:
|
|
22
|
+
target = root / path
|
|
23
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
24
|
+
target.write_text(source)
|
|
25
|
+
return root
|
|
26
|
+
|
|
27
|
+
def assert_high(self, result, rule='documented-serial-claim-false'):
|
|
28
|
+
self.assertEqual(result['verdict'], 'FAIL')
|
|
29
|
+
self.assertEqual(len(result['findings']), 1)
|
|
30
|
+
self.assertEqual(result['findings'][0]['severity'], 'HIGH')
|
|
31
|
+
self.assertEqual(result['findings'][0]['rule'], rule)
|
|
32
|
+
|
|
33
|
+
def test_positive_phrases(self):
|
|
34
|
+
self.assertEqual(len(SERIAL), 33)
|
|
35
|
+
for phrase in SERIAL:
|
|
36
|
+
with self.subTest(phrase=phrase):
|
|
37
|
+
self.assertEqual(claims(phrase), (True, False))
|
|
38
|
+
self.assert_high(audit(self.project(phrase)))
|
|
39
|
+
|
|
40
|
+
def test_negative_phrases(self):
|
|
41
|
+
self.assertEqual(len(NOSERIAL), 22)
|
|
42
|
+
for phrase in NOSERIAL:
|
|
43
|
+
with self.subTest(phrase=phrase):
|
|
44
|
+
self.assertEqual(claims(phrase), (False, True))
|
|
45
|
+
self.assertEqual(audit(self.project(phrase))['findings'], [])
|
|
46
|
+
self.assert_high(audit(self.project(phrase, GOOD['serial'])), 'documented-no-serial-claim-false')
|
|
47
|
+
|
|
48
|
+
def test_no_claim(self):
|
|
49
|
+
self.assertEqual(audit(self.project('A device tool', GOOD['serial']))['findings'], [])
|
|
50
|
+
|
|
51
|
+
def test_user_instructions(self):
|
|
52
|
+
for doc in ('plug in the serial cable first', 'connect the serial cable before starting'):
|
|
53
|
+
self.assertEqual(audit(self.project(doc))['verdict'], 'PASS')
|
|
54
|
+
|
|
55
|
+
def test_bare_word(self):
|
|
56
|
+
self.assertEqual(claims('# Serial'), (False, False))
|
|
57
|
+
|
|
58
|
+
def test_fences(self):
|
|
59
|
+
for fence in ('```', '~~~~'):
|
|
60
|
+
self.assertEqual(claims(fence+'text\nsupports serial\n'+fence), (False, False))
|
|
61
|
+
|
|
62
|
+
def test_inline(self):
|
|
63
|
+
self.assert_high(audit(self.project('It `supports serial`.')))
|
|
64
|
+
|
|
65
|
+
def test_normalization(self):
|
|
66
|
+
self.assertEqual(claims("Doesn't communicate\nover-serial"), (False, True))
|
|
67
|
+
self.assertEqual(claims('SUPPORTS-SERIAL'), (True, False))
|
|
68
|
+
|
|
69
|
+
def test_boundaries(self):
|
|
70
|
+
self.assertEqual(claims('misuses serially'), (False, False))
|
|
71
|
+
self.assertEqual(claims('supports\n\nserial'), (False, False))
|
|
72
|
+
|
|
73
|
+
def test_two_families(self):
|
|
74
|
+
result = audit(self.project('Supports serial. No serial.'))
|
|
75
|
+
self.assertEqual(len(result['findings']), 1)
|
|
76
|
+
|
|
77
|
+
def test_docs_scope(self):
|
|
78
|
+
root = self.project('neutral', GOOD['serial'])
|
|
79
|
+
path = root / 'nested/docs/guide.md'
|
|
80
|
+
path.parent.mkdir(parents=True)
|
|
81
|
+
path.write_text('No serial')
|
|
82
|
+
self.assert_high(audit(root), 'documented-no-serial-claim-false')
|
|
83
|
+
self.assertEqual(audit(root, ['nested/docs/*'])['verdict'], 'PASS')
|
|
84
|
+
|
|
85
|
+
def test_excluded_evidence(self):
|
|
86
|
+
for path in ('fixtures/a.py', 'docs/a.py', 'examples/a.py'):
|
|
87
|
+
self.assert_high(audit(self.project('supports serial', GOOD['serial'], path)))
|
|
88
|
+
|
|
89
|
+
def test_test_scope(self):
|
|
90
|
+
for path in ('tests/a.py', 'test_port.py', 'port_test.py'):
|
|
91
|
+
root = self.project('supports serial', GOOD['serial'], path)
|
|
92
|
+
result = audit(root)
|
|
93
|
+
self.assertEqual(result['verdict'], 'PASS')
|
|
94
|
+
self.assertEqual(result['findings'][0]['severity'], 'MEDIUM')
|
|
95
|
+
self.assertEqual(result['findings'][0]['rule'], 'documented-serial-claim-false')
|
|
96
|
+
self.assertEqual(audit(root, fail_on='MEDIUM')['verdict'], 'FAIL')
|
|
97
|
+
|
|
98
|
+
def test_symlinks(self):
|
|
99
|
+
root = self.project('supports serial')
|
|
100
|
+
(root / 'linked.py').symlink_to(ROOT / 'tests/fixtures/good/app.py')
|
|
101
|
+
(root / 'linked').symlink_to(ROOT / 'tests/fixtures/good', target_is_directory=True)
|
|
102
|
+
self.assert_high(audit(root))
|
|
103
|
+
|
|
104
|
+
def test_env_never_opened(self):
|
|
105
|
+
original = Path.open
|
|
106
|
+
def guarded(path, *args, **kwargs):
|
|
107
|
+
self.assertNotEqual(path.name, '.env')
|
|
108
|
+
return original(path, *args, **kwargs)
|
|
109
|
+
with patch.object(Path, 'open', guarded):
|
|
110
|
+
self.assertEqual(audit(ROOT)['verdict'], 'PASS')
|
|
111
|
+
|
|
112
|
+
def test_self_audit(self):
|
|
113
|
+
self.assertEqual(claims((ROOT / 'README.md').read_text()), (False, False))
|
|
114
|
+
self.assertEqual(audit(ROOT)['findings'], [])
|
|
115
|
+
|
|
116
|
+
def cli(self, *args):
|
|
117
|
+
return subprocess.run(['python3', '-m', 'serial_claim_audit', *map(str, args)], capture_output=True, text=True)
|
|
118
|
+
|
|
119
|
+
def test_cli_version(self):
|
|
120
|
+
result = self.cli('--version')
|
|
121
|
+
self.assertEqual((result.returncode, result.stdout.strip()), (0, '0.1.0'))
|
|
122
|
+
|
|
123
|
+
def test_cli_usage_error(self):
|
|
124
|
+
self.assertEqual(self.cli("--unknown-option").returncode, 1)
|
|
125
|
+
|
|
126
|
+
def test_cli_bad_path(self):
|
|
127
|
+
self.assertEqual(self.cli(ROOT / 'nonexistent').returncode, 1)
|
|
128
|
+
|
|
129
|
+
def test_cli_json(self):
|
|
130
|
+
result = self.cli(ROOT / 'tests/fixtures/bad', '--json')
|
|
131
|
+
self.assertEqual(result.returncode, 2)
|
|
132
|
+
self.assert_high(json.loads(result.stdout))
|
|
133
|
+
|
|
134
|
+
def test_cli_medium(self):
|
|
135
|
+
root = ROOT / 'tests/fixtures/testonly'
|
|
136
|
+
self.assertEqual(self.cli(root).returncode, 0)
|
|
137
|
+
self.assertEqual(self.cli(root, '--fail-on', 'MEDIUM').returncode, 2)
|
|
138
|
+
|
|
139
|
+
def test_cli_ignore_verbose(self):
|
|
140
|
+
result = self.cli(ROOT / 'tests/fixtures/bad', '--ignore', 'README.md', '--verbose')
|
|
141
|
+
self.assertEqual(result.returncode, 0)
|
|
142
|
+
self.assertIn('Scanned', result.stdout)
|
|
143
|
+
|
|
144
|
+
def test_packaging(self):
|
|
145
|
+
config = (ROOT / 'pyproject.toml').read_text()
|
|
146
|
+
self.assertIn('serial-claim-audit = "serial_claim_audit.__main__:main"', config)
|
|
147
|
+
self.assertIn('requires-python = ">=3.9"', config)
|
|
148
|
+
self.assertIn('MIT License', (ROOT / 'LICENSE').read_text())
|
|
149
|
+
self.assertIn('recursive-include tests', (ROOT / 'MANIFEST.in').read_text())
|
|
150
|
+
for name in ('good', 'bad', 'testonly', 'no-serial'):
|
|
151
|
+
self.assertTrue((ROOT / 'tests/fixtures' / name / 'README.md').exists())
|
|
152
|
+
|
|
153
|
+
def test_source_hygiene(self):
|
|
154
|
+
for base in ('serial_claim_audit', 'tests'):
|
|
155
|
+
for path in (ROOT / base).rglob('*.py'):
|
|
156
|
+
text = path.read_text()
|
|
157
|
+
self.assertIsNone(re.search(r'(?m)^\s*(?:\$ )?python' + r'\s+-m', text))
|
|
158
|
+
self.assertIsNone(re.search('AKIA' + r'[A-Z0-9]{16}|-----BEGIN ' + 'PRIVATE KEY-----', text))
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def good_test(source):
|
|
162
|
+
def test(self):
|
|
163
|
+
self.assertTrue(evidence(source))
|
|
164
|
+
self.assertEqual(audit(self.project('supports serial', source))['findings'], [])
|
|
165
|
+
return test
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def bad_test(source):
|
|
169
|
+
def test(self):
|
|
170
|
+
self.assertFalse(evidence(source))
|
|
171
|
+
self.assert_high(audit(self.project('supports serial', source)))
|
|
172
|
+
return test
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
for name, source in GOOD.items():
|
|
176
|
+
setattr(AuditTests, 'test_good_' + name, good_test(source))
|
|
177
|
+
for name, source in BAD.items():
|
|
178
|
+
setattr(AuditTests, 'test_bad_' + name, bad_test(source))
|