Option C Tools
Loading...
Searching...
No Matches
test_dbg_assert_safety.py
Go to the documentation of this file.
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
# tests/tests_linter/test_dbg_assert_safety.py
4
5
"""
6
Purpose
7
-------
8
Validate the linter's ``dbg_assert_safety`` rule (OI-403 / AP-16).
9
10
Responsibilities
11
----------------
12
- Confirm that ``_dbg_assert`` calls inside security-sensitive functions
13
are flagged.
14
- Confirm that ``_dbg_assert`` calls inside security-sensitive classes
15
(via method scope) are flagged.
16
- Confirm that ``_dbg_assert`` calls inside modules with security-sensitive
17
filenames are flagged.
18
- Confirm that ``_dbg_assert`` calls in ordinary contexts are not flagged.
19
20
Diagnostics
21
-----------
22
Domain: LINTER-TESTS
23
L2: test lifecycle
24
L3: assertion details
25
L4: deep tracing
26
27
Contracts
28
---------
29
Inputs: inline source strings
30
Outputs: pass/fail assertions
31
"""
32
33
from
oct.linter
import
oct_lint
34
35
36
def
test_dbg_assert_in_auth_function_flagged
():
37
"""_dbg_assert inside a function whose name contains 'authenticate' is flagged."""
38
source = (
39
'def authenticate_user(token):\n'
40
' _dbg_assert("AUTH", token is not None, "token required")\n'
41
' return True\n'
42
)
43
ok, msg = oct_lint.check_dbg_assert_safety(source)
44
assert
not
ok
45
assert
"authenticate_user"
in
msg
46
assert
"AP-16"
in
msg
47
48
49
def
test_dbg_assert_in_harmless_function_passes
():
50
"""_dbg_assert inside a harmless function is fine."""
51
source = (
52
'def compute_sum(a, b):\n'
53
' _dbg_assert("MATH", isinstance(a, int), "a must be int")\n'
54
' return a + b\n'
55
)
56
ok, msg = oct_lint.check_dbg_assert_safety(source)
57
assert
ok, f
"Expected pass, got: {msg}"
58
59
60
def
test_dbg_assert_in_class_method_of_auth_class_flagged
():
61
"""_dbg_assert inside a method of a security-sensitive class is flagged."""
62
source = (
63
'class AuthManager:\n'
64
' def check(self, token):\n'
65
' _dbg_assert("AUTH", token, "need token")\n'
66
' return True\n'
67
)
68
ok, msg = oct_lint.check_dbg_assert_safety(source)
69
assert
not
ok
70
assert
"AuthManager"
in
msg
71
72
73
def
test_dbg_assert_in_security_module_flagged
():
74
"""_dbg_assert in a module whose filename is security-sensitive is flagged."""
75
source = (
76
'def helper():\n'
77
' _dbg_assert("SEC", True, "ok")\n'
78
)
79
ok, msg = oct_lint.check_dbg_assert_safety(source, filename=
"password_check.py"
)
80
assert
not
ok
81
assert
"password_check"
in
msg
82
83
84
def
test_no_dbg_assert_calls_passes
():
85
"""Source without any _dbg_assert calls passes trivially."""
86
source = (
87
'def authenticate_user(token):\n'
88
' if not token:\n'
89
' raise ValueError("token required")\n'
90
' return True\n'
91
)
92
ok, msg = oct_lint.check_dbg_assert_safety(source)
93
assert
ok
94
95
96
def
test_dbg_assert_attribute_call_flagged
():
97
"""Attribute-style ``oc_diagnostics._dbg_assert`` call is also flagged."""
98
source = (
99
'def validate_credentials(user, pw):\n'
100
' oc_diagnostics._dbg_assert("AUTH", user, "user required")\n'
101
' return True\n'
102
)
103
ok, msg = oct_lint.check_dbg_assert_safety(source)
104
assert
not
ok
105
assert
"validate_credentials"
in
msg
oct.linter
Definition
__init__.py:1
tests_linter.test_dbg_assert_safety.test_no_dbg_assert_calls_passes
test_no_dbg_assert_calls_passes()
Definition
test_dbg_assert_safety.py:84
tests_linter.test_dbg_assert_safety.test_dbg_assert_in_auth_function_flagged
test_dbg_assert_in_auth_function_flagged()
Definition
test_dbg_assert_safety.py:36
tests_linter.test_dbg_assert_safety.test_dbg_assert_attribute_call_flagged
test_dbg_assert_attribute_call_flagged()
Definition
test_dbg_assert_safety.py:96
tests_linter.test_dbg_assert_safety.test_dbg_assert_in_security_module_flagged
test_dbg_assert_in_security_module_flagged()
Definition
test_dbg_assert_safety.py:73
tests_linter.test_dbg_assert_safety.test_dbg_assert_in_harmless_function_passes
test_dbg_assert_in_harmless_function_passes()
Definition
test_dbg_assert_safety.py:49
tests_linter.test_dbg_assert_safety.test_dbg_assert_in_class_method_of_auth_class_flagged
test_dbg_assert_in_class_method_of_auth_class_flagged()
Definition
test_dbg_assert_safety.py:60
tests
tests_linter
test_dbg_assert_safety.py
Generated by
1.15.0