Skip to content

Commit d039ceb

Browse files
committed
Python: add test for fields
1 parent 706e9dc commit d039ceb

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def check_tests_valid_after_version(testFile, version):
7575
check_tests_valid("variable-capture.test_collections")
7676
check_tests_valid("variable-capture.by_value")
7777
check_tests_valid("variable-capture.test_library_calls")
78+
check_tests_valid("variable-capture.test_fields")
7879
check_tests_valid("module-initialization.multiphase")
7980
check_tests_valid("fieldflow.test")
8081
check_tests_valid("fieldflow.test_dict")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
5+
from testlib import expects
6+
7+
# These are defined so that we can evaluate the test code.
8+
NONSOURCE = "not a source"
9+
SOURCE = "source"
10+
11+
def is_source(x):
12+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
13+
14+
15+
def SINK(x):
16+
if is_source(x):
17+
print("OK")
18+
else:
19+
print("Unexpected flow", x)
20+
21+
22+
def SINK_F(x):
23+
if is_source(x):
24+
print("Unexpected flow", x)
25+
else:
26+
print("OK")
27+
28+
class MyObj(object):
29+
def setFoo(self, foo):
30+
self.foo = foo
31+
32+
def getFoo(self):
33+
return self.foo
34+
35+
@expects(3)
36+
def test_captured_field():
37+
foo = MyObj()
38+
foo.setFoo(NONSOURCE)
39+
40+
def test():
41+
SINK(foo.getFoo()) #$ captured
42+
43+
def read():
44+
return foo.getFoo()
45+
46+
SINK_F(read())
47+
48+
foo.setFoo(SOURCE)
49+
test()
50+
51+
SINK(read()) #$ captured

0 commit comments

Comments
 (0)