Skip to content

Commit 18041fd

Browse files
committed
Python: Expand string-const-compare tests
Also moved file to reflect that. Added tests of + `!=` + `in` + `not in`
1 parent 08bcba9 commit 18041fd

File tree

3 files changed

+165
-76
lines changed

3 files changed

+165
-76
lines changed
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
| test_string_eq.py:16 | ok | const_eq_clears_taint | ts |
2-
| test_string_eq.py:18 | ok | const_eq_clears_taint | ts |
3-
| test_string_eq.py:20 | ok | const_eq_clears_taint | ts |
4-
| test_string_eq.py:27 | fail | const_eq_clears_taint2 | ts |
5-
| test_string_eq.py:33 | fail | const_eq_clears_taint3 | ts |
6-
| test_string_eq.py:35 | ok | const_eq_clears_taint3 | ts |
7-
| test_string_eq.py:41 | ok | non_const_eq_preserves_taint | ts |
8-
| test_string_eq.py:43 | ok | non_const_eq_preserves_taint | ts |
9-
| test_string_eq.py:53 | fail | const_eq_through_func | ts |
10-
| test_string_eq.py:55 | ok | const_eq_through_func | ts |
11-
| test_string_eq.py:57 | ok | const_eq_through_func | ts |
1+
| test_string_const_compare.py:16 | ok | test_eq | ts |
2+
| test_string_const_compare.py:18 | ok | test_eq | ts |
3+
| test_string_const_compare.py:20 | ok | test_eq | ts |
4+
| test_string_const_compare.py:27 | ok | test_eq_unsafe | ts |
5+
| test_string_const_compare.py:29 | ok | test_eq_unsafe | ts |
6+
| test_string_const_compare.py:35 | fail | test_eq_with_or | ts |
7+
| test_string_const_compare.py:37 | ok | test_eq_with_or | ts |
8+
| test_string_const_compare.py:43 | ok | test_non_eq1 | ts |
9+
| test_string_const_compare.py:45 | fail | test_non_eq1 | ts |
10+
| test_string_const_compare.py:51 | ok | test_non_eq2 | ts |
11+
| test_string_const_compare.py:53 | fail | test_non_eq2 | ts |
12+
| test_string_const_compare.py:59 | fail | test_in_list | ts |
13+
| test_string_const_compare.py:61 | ok | test_in_list | ts |
14+
| test_string_const_compare.py:67 | fail | test_in_tuple | ts |
15+
| test_string_const_compare.py:69 | ok | test_in_tuple | ts |
16+
| test_string_const_compare.py:75 | fail | test_in_set | ts |
17+
| test_string_const_compare.py:77 | ok | test_in_set | ts |
18+
| test_string_const_compare.py:83 | ok | test_in_unsafe1 | ts |
19+
| test_string_const_compare.py:85 | ok | test_in_unsafe1 | ts |
20+
| test_string_const_compare.py:91 | ok | test_in_unsafe2 | ts |
21+
| test_string_const_compare.py:93 | ok | test_in_unsafe2 | ts |
22+
| test_string_const_compare.py:99 | ok | test_not_in1 | ts |
23+
| test_string_const_compare.py:101 | fail | test_not_in1 | ts |
24+
| test_string_const_compare.py:107 | ok | test_not_in2 | ts |
25+
| test_string_const_compare.py:109 | fail | test_not_in2 | ts |
26+
| test_string_const_compare.py:119 | fail | test_eq_thorugh_func | ts |
27+
| test_string_const_compare.py:121 | ok | test_eq_thorugh_func | ts |
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Add taintlib to PATH so it can be imported during runtime without any hassle
2+
import sys; import os; sys.path.append(os.path.dirname(os.path.dirname((__file__))))
3+
from taintlib import *
4+
5+
# This has no runtime impact, but allows autocomplete to work
6+
from typing import TYPE_CHECKING
7+
if TYPE_CHECKING:
8+
from ..taintlib import *
9+
10+
11+
# Actual tests
12+
13+
def test_eq():
14+
ts = TAINTED_STRING
15+
if ts == "safe":
16+
ensure_not_tainted(ts)
17+
else:
18+
ensure_tainted(ts)
19+
# ts should still be tainted after exiting the if block
20+
ensure_tainted(ts)
21+
22+
23+
def test_eq_unsafe(x="foo"):
24+
"""This test-case might seem strange, but it was a FP in our old points-to based analysis."""
25+
ts = TAINTED_STRING
26+
if ts == ts:
27+
ensure_tainted(ts)
28+
if ts == x:
29+
ensure_tainted(ts)
30+
31+
32+
def test_eq_with_or():
33+
ts = TAINTED_STRING
34+
if ts == "safe" or ts == "also_safe":
35+
ensure_not_tainted(ts)
36+
else:
37+
ensure_tainted(ts)
38+
39+
40+
def test_non_eq1():
41+
ts = TAINTED_STRING
42+
if ts != "safe":
43+
ensure_tainted(ts)
44+
else:
45+
ensure_not_tainted(ts)
46+
47+
48+
def test_non_eq2():
49+
ts = TAINTED_STRING
50+
if not ts == "safe":
51+
ensure_tainted(ts)
52+
else:
53+
ensure_not_tainted(ts)
54+
55+
56+
def test_in_list():
57+
ts = TAINTED_STRING
58+
if ts in ["safe", "also_safe"]:
59+
ensure_not_tainted(ts)
60+
else:
61+
ensure_tainted(ts)
62+
63+
64+
def test_in_tuple():
65+
ts = TAINTED_STRING
66+
if ts in ("safe", "also_safe"):
67+
ensure_not_tainted(ts)
68+
else:
69+
ensure_tainted(ts)
70+
71+
72+
def test_in_set():
73+
ts = TAINTED_STRING
74+
if ts in {"safe", "also_safe"}:
75+
ensure_not_tainted(ts)
76+
else:
77+
ensure_tainted(ts)
78+
79+
80+
def test_in_unsafe1(xs):
81+
ts = TAINTED_STRING
82+
if ts in xs:
83+
ensure_tainted(ts)
84+
else:
85+
ensure_tainted(ts)
86+
87+
88+
def test_in_unsafe2(x):
89+
ts = TAINTED_STRING
90+
if ts in ["safe", x]:
91+
ensure_tainted(ts)
92+
else:
93+
ensure_tainted(ts)
94+
95+
96+
def test_not_in1():
97+
ts = TAINTED_STRING
98+
if ts not in ["safe", "also_safe"]:
99+
ensure_tainted(ts)
100+
else:
101+
ensure_not_tainted(ts)
102+
103+
104+
def test_not_in2():
105+
ts = TAINTED_STRING
106+
if not ts in ["safe", "also_safe"]:
107+
ensure_tainted(ts)
108+
else:
109+
ensure_not_tainted(ts)
110+
111+
112+
def is_safe(x):
113+
return x == "safe"
114+
115+
116+
def test_eq_thorugh_func():
117+
ts = TAINTED_STRING
118+
if is_safe(ts):
119+
ensure_not_tainted(ts)
120+
else:
121+
ensure_tainted(ts)
122+
123+
124+
# Make tests runable
125+
126+
test_eq()
127+
test_eq_unsafe()
128+
test_eq_with_or()
129+
test_non_eq1()
130+
test_non_eq2()
131+
test_in_list()
132+
test_in_tuple()
133+
test_in_set()
134+
test_in_unsafe1(["unsafe", "foo"])
135+
test_in_unsafe2("unsafe")
136+
test_not_in1()
137+
test_not_in2()
138+
test_eq_thorugh_func()

python/ql/test/experimental/dataflow/tainttracking/commonSanitizer/test_string_eq.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)