mirror of
https://github.com/github/codeql.git
synced 2025-12-22 11:46:32 +01:00
Python: Add test for dictionary flow
This commit is contained in:
73
python/ql/test/experimental/dataflow/fieldflow/test_dict.py
Normal file
73
python/ql/test/experimental/dataflow/fieldflow/test_dict.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname((__file__)))) # $ unresolved_call=sys.path.append(..)
|
||||
from testlib import expects
|
||||
|
||||
# These are defined so that we can evaluate the test code.
|
||||
NONSOURCE = "not a source"
|
||||
SOURCE = "source"
|
||||
|
||||
|
||||
def is_source(x):
|
||||
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
|
||||
|
||||
|
||||
def SINK(x):
|
||||
if is_source(x):
|
||||
print("OK")
|
||||
else:
|
||||
print("Unexpected flow", x)
|
||||
|
||||
|
||||
def SINK_F(x):
|
||||
if is_source(x):
|
||||
print("Unexpected flow", x)
|
||||
else:
|
||||
print("OK")
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Actual tests
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
|
||||
def test_dict_literal():
|
||||
d = {"key": SOURCE}
|
||||
SINK(d["key"]) # $ flow="SOURCE, l:-1 -> d['key']"
|
||||
SINK(d.get("key")) # $ MISSING: flow
|
||||
SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow
|
||||
|
||||
|
||||
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
|
||||
def test_dict_update():
|
||||
d = {}
|
||||
d["key"] = SOURCE
|
||||
SINK(d["key"]) # $ MISSING: flow
|
||||
SINK(d.get("key")) # $ MISSING: flow
|
||||
SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow
|
||||
|
||||
|
||||
@expects(2) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
|
||||
def test_dict_override():
|
||||
d = {}
|
||||
d["key"] = SOURCE
|
||||
SINK(d["key"]) # $ MISSING: flow
|
||||
|
||||
d["key"] = NONSOURCE
|
||||
SINK_F(d["key"])
|
||||
|
||||
|
||||
def test_dict_setdefault():
|
||||
d = {}
|
||||
d.setdefault("key", SOURCE)
|
||||
SINK(d["key"]) # $ MISSING: flow
|
||||
|
||||
|
||||
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
|
||||
def test_dict_nonstring_key():
|
||||
d = {}
|
||||
d[42] = SOURCE
|
||||
SINK(d[42]) # $ MISSING: flow
|
||||
SINK(d.get(42)) # $ MISSING: flow
|
||||
SINK(d.setdefault(42, NONSOURCE)) # $ MISSING: flow
|
||||
@@ -72,6 +72,7 @@ if __name__ == "__main__":
|
||||
check_tests_valid("variable-capture.collections")
|
||||
check_tests_valid("module-initialization.multiphase")
|
||||
check_tests_valid("fieldflow.test")
|
||||
check_tests_valid("fieldflow.test_dict")
|
||||
check_tests_valid_after_version("match.test", (3, 10))
|
||||
check_tests_valid("exceptions.test")
|
||||
check_tests_valid_after_version("exceptions.test_group", (3, 11))
|
||||
|
||||
Reference in New Issue
Block a user