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"""
6Purpose
7-------
8Validate the linter's ``dbg_assert_safety`` rule (OI-403 / AP-16).
9
10Responsibilities
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
20Diagnostics
21-----------
22Domain: LINTER-TESTS
23L2: test lifecycle
24L3: assertion details
25L4: deep tracing
26
27Contracts
28---------
29Inputs: inline source strings
30Outputs: pass/fail assertions
31"""
32
33from oct.linter import oct_lint
34
35
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
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
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
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
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
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