Skip to content

Commit 71830ab

Browse files
committed
Python: remaining c# tests, except lambdas
both via nonlocal and via dict
1 parent 27b4c67 commit 71830ab

File tree

3 files changed

+173
-2
lines changed

3 files changed

+173
-2
lines changed

python/ql/test/experimental/dataflow/validTest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ def check_tests_valid(testFile):
5353
check_tests_valid("coverage.classes")
5454
check_tests_valid("coverage.test")
5555
check_tests_valid("coverage.argumentPassing")
56-
check_tests_valid("variable-capture.test")
56+
check_tests_valid("variable-capture.nonlocal")
57+
check_tests_valid("variable-capture.dict")
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
2+
# This can be checked by running validTest.py.
3+
4+
import sys
5+
import os
6+
7+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
8+
from testlib import *
9+
10+
# These are defined so that we can evaluate the test code.
11+
NONSOURCE = "not a source"
12+
SOURCE = "source"
13+
14+
def is_source(x):
15+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
16+
17+
18+
def SINK(x):
19+
if is_source(x):
20+
print("OK")
21+
else:
22+
print("Unexpected flow", x)
23+
24+
25+
def SINK_F(x):
26+
if is_source(x):
27+
print("Unexpected flow", x)
28+
else:
29+
print("OK")
30+
31+
32+
def In(tainted):
33+
def captureIn1():
34+
sinkI1 = tainted
35+
SINK(sinkI1) #$ MISSING:captured
36+
captureIn1()
37+
38+
def captureIn2():
39+
def m():
40+
sinkI2 = tainted
41+
SINK(sinkI2) #$ MISSING:captured
42+
m()
43+
captureIn2()
44+
45+
# captureIn3 = lambda arg:(
46+
# sinkI3 = tainted;
47+
# check(sinkI3);
48+
# return arg)
49+
# [ captureIn3(x) for x in " " ]
50+
51+
def captureIn1NotCalled():
52+
nonSink0 = tainted
53+
SINK_F(nonSink0)
54+
55+
def captureIn2NotCalled():
56+
def m():
57+
nonSink0 = tainted
58+
SINK_F(nonSink0)
59+
captureIn2NotCalled()
60+
61+
@expects(2)
62+
def test_In():
63+
In(SOURCE)
64+
65+
def Out():
66+
sinkO1 = { "x": "" }
67+
def captureOut1():
68+
sinkO1["x"] = "source"
69+
captureOut1()
70+
SINK(sinkO1["x"]) #$ MISSING:captured
71+
72+
sinkO2 = { "x": "" }
73+
def captureOut2():
74+
def m():
75+
sinkO2["x"] = "source"
76+
m()
77+
captureOut2()
78+
SINK(sinkO2["x"]) #$ MISSING:captured
79+
80+
nonSink0 = { "x": "" }
81+
def captureOut1NotCalled():
82+
nonSink0["x"] = "source"
83+
SINK_F(nonSink0["x"])
84+
85+
def captureOut2NotCalled():
86+
def m():
87+
nonSink0["x"] = "source"
88+
captureOut2NotCalled()
89+
SINK_F(nonSink0["x"])
90+
91+
@expects(4)
92+
def test_Out():
93+
Out()
94+
95+
def Through(tainted):
96+
sinkO1 = { "x": "" }
97+
def captureOut1():
98+
sinkO1["x"] = tainted
99+
captureOut1()
100+
SINK(sinkO1["x"]) #$ MISSING:captured
101+
102+
sinkO2 = { "x": "" }
103+
def captureOut2():
104+
def m():
105+
sinkO2["x"] = tainted
106+
m()
107+
captureOut2()
108+
SINK(sinkO2["x"]) #$ MISSING:captured
109+
110+
nonSink0 = { "x": "" }
111+
def captureOut1NotCalled():
112+
nonSink0["x"] = tainted
113+
SINK_F(nonSink0["x"])
114+
115+
def captureOut2NotCalled():
116+
def m():
117+
nonSink0["x"] = tainted
118+
captureOut2NotCalled()
119+
SINK_F(nonSink0["x"])
120+
121+
@expects(4)
122+
def test_Through():
123+
Through(SOURCE)

python/ql/test/experimental/dataflow/variable-capture/test.py renamed to python/ql/test/experimental/dataflow/variable-capture/nonlocal.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,53 @@ def m():
7979
captureOut2()
8080
SINK(sinkO2) #$ MISSING:captured
8181

82-
@expects(2)
82+
nonSink0 = ""
83+
def captureOut1NotCalled():
84+
nonlocal nonSink0
85+
nonSink0 = "source"
86+
SINK_F(nonSink0)
87+
88+
def captureOut2NotCalled():
89+
def m():
90+
nonlocal nonSink0
91+
nonSink0 = "source"
92+
captureOut2NotCalled()
93+
SINK_F(nonSink0)
94+
95+
@expects(4)
8396
def test_Out():
8497
Out()
98+
99+
def Through(tainted):
100+
sinkO1 = ""
101+
def captureOut1():
102+
nonlocal sinkO1
103+
sinkO1 = tainted
104+
captureOut1()
105+
SINK(sinkO1) #$ MISSING:captured
106+
107+
sinkO2 = ""
108+
def captureOut2():
109+
def m():
110+
nonlocal sinkO2
111+
sinkO2 = tainted
112+
m()
113+
captureOut2()
114+
SINK(sinkO2) #$ MISSING:captured
115+
116+
nonSink0 = ""
117+
def captureOut1NotCalled():
118+
nonlocal nonSink0
119+
nonSink0 = tainted
120+
SINK_F(nonSink0)
121+
122+
def captureOut2NotCalled():
123+
def m():
124+
nonlocal nonSink0
125+
nonSink0 = tainted
126+
captureOut2NotCalled()
127+
SINK_F(nonSink0)
128+
129+
@expects(4)
130+
def test_Through():
131+
Through(SOURCE)

0 commit comments

Comments
 (0)