Skip to content

Commit 6e31f64

Browse files
committed
Python: Add test for dictionary flow
1 parent 4e49df1 commit 6e31f64

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(os.path.dirname(os.path.dirname((__file__)))) # $ unresolved_call=sys.path.append(..)
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+
12+
def is_source(x):
13+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
14+
15+
16+
def SINK(x):
17+
if is_source(x):
18+
print("OK")
19+
else:
20+
print("Unexpected flow", x)
21+
22+
23+
def SINK_F(x):
24+
if is_source(x):
25+
print("Unexpected flow", x)
26+
else:
27+
print("OK")
28+
29+
30+
# ------------------------------------------------------------------------------
31+
# Actual tests
32+
# ------------------------------------------------------------------------------
33+
34+
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
35+
def test_dict_literal():
36+
d = {"key": SOURCE}
37+
SINK(d["key"]) # $ flow="SOURCE, l:-1 -> d['key']"
38+
SINK(d.get("key")) # $ MISSING: flow
39+
SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow
40+
41+
42+
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
43+
def test_dict_update():
44+
d = {}
45+
d["key"] = SOURCE
46+
SINK(d["key"]) # $ MISSING: flow
47+
SINK(d.get("key")) # $ MISSING: flow
48+
SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow
49+
50+
51+
@expects(2) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
52+
def test_dict_override():
53+
d = {}
54+
d["key"] = SOURCE
55+
SINK(d["key"]) # $ MISSING: flow
56+
57+
d["key"] = NONSOURCE
58+
SINK_F(d["key"])
59+
60+
61+
def test_dict_setdefault():
62+
d = {}
63+
d.setdefault("key", SOURCE)
64+
SINK(d["key"]) # $ MISSING: flow
65+
66+
67+
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
68+
def test_dict_nonstring_key():
69+
d = {}
70+
d[42] = SOURCE
71+
SINK(d[42]) # $ MISSING: flow
72+
SINK(d.get(42)) # $ MISSING: flow
73+
SINK(d.setdefault(42, NONSOURCE)) # $ MISSING: flow

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def check_tests_valid_after_version(testFile, version):
7272
check_tests_valid("variable-capture.collections")
7373
check_tests_valid("module-initialization.multiphase")
7474
check_tests_valid("fieldflow.test")
75+
check_tests_valid("fieldflow.test_dict")
7576
check_tests_valid_after_version("match.test", (3, 10))
7677
check_tests_valid("exceptions.test")
7778
check_tests_valid_after_version("exceptions.test_group", (3, 11))

0 commit comments

Comments
 (0)