Skip to content

Commit ae9f584

Browse files
authored
Merge pull request #4159 from RasmusWL/python-port-dataflow-tests
Python: port dataflow tests
2 parents eea8934 + 720e8c4 commit ae9f584

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

python/ql/test/experimental/dataflow/coverage/classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# These tests should cover all the class calls that we hope to support.
66
# It is based on https://docs.python.org/3/reference/datamodel.html, and headings refer there.
77
#
8-
# All functions starting with "test_" should run and print `"OK"`.
8+
# All functions starting with "test_" should run and execute `print("OK")` one or more times.
99
# This can be checked by running validTest.py.
1010

1111
import asyncio

python/ql/test/experimental/dataflow/coverage/dataflow.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ edges
131131
| test.py:276:28:276:33 | ControlFlowNode for SOURCE | test.py:276:10:276:34 | ControlFlowNode for second() |
132132
| test.py:335:12:335:17 | ControlFlowNode for SOURCE | test.py:335:10:335:18 | ControlFlowNode for f() |
133133
| test.py:339:28:339:33 | ControlFlowNode for SOURCE | test.py:339:10:339:34 | ControlFlowNode for second() |
134+
| test.py:372:9:372:14 | ControlFlowNode for SOURCE | test.py:374:10:374:10 | ControlFlowNode for a |
135+
| test.py:372:9:372:14 | ControlFlowNode for SOURCE | test.py:379:10:379:10 | ControlFlowNode for b |
134136
nodes
135137
| datamodel.py:13:1:13:6 | GSSA Variable SOURCE | semmle.label | GSSA Variable SOURCE |
136138
| datamodel.py:13:10:13:17 | ControlFlowNode for Str | semmle.label | ControlFlowNode for Str |
@@ -260,6 +262,9 @@ nodes
260262
| test.py:335:12:335:17 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
261263
| test.py:339:10:339:34 | ControlFlowNode for second() | semmle.label | ControlFlowNode for second() |
262264
| test.py:339:28:339:33 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
265+
| test.py:372:9:372:14 | ControlFlowNode for SOURCE | semmle.label | ControlFlowNode for SOURCE |
266+
| test.py:374:10:374:10 | ControlFlowNode for a | semmle.label | ControlFlowNode for a |
267+
| test.py:379:10:379:10 | ControlFlowNode for b | semmle.label | ControlFlowNode for b |
263268
#select
264269
| datamodel.py:38:6:38:17 | ControlFlowNode for f() | datamodel.py:13:10:13:17 | ControlFlowNode for Str | datamodel.py:38:6:38:17 | ControlFlowNode for f() | <message> |
265270
| datamodel.py:38:6:38:17 | ControlFlowNode for f() | datamodel.py:38:8:38:13 | ControlFlowNode for SOURCE | datamodel.py:38:6:38:17 | ControlFlowNode for f() | <message> |
@@ -301,3 +306,5 @@ nodes
301306
| test.py:276:10:276:34 | ControlFlowNode for second() | test.py:276:28:276:33 | ControlFlowNode for SOURCE | test.py:276:10:276:34 | ControlFlowNode for second() | <message> |
302307
| test.py:335:10:335:18 | ControlFlowNode for f() | test.py:335:12:335:17 | ControlFlowNode for SOURCE | test.py:335:10:335:18 | ControlFlowNode for f() | <message> |
303308
| test.py:339:10:339:34 | ControlFlowNode for second() | test.py:339:28:339:33 | ControlFlowNode for SOURCE | test.py:339:10:339:34 | ControlFlowNode for second() | <message> |
309+
| test.py:374:10:374:10 | ControlFlowNode for a | test.py:372:9:372:14 | ControlFlowNode for SOURCE | test.py:374:10:374:10 | ControlFlowNode for a | <message> |
310+
| test.py:379:10:379:10 | ControlFlowNode for b | test.py:372:9:372:14 | ControlFlowNode for SOURCE | test.py:379:10:379:10 | ControlFlowNode for b | <message> |

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Functions whose name ends with "_with_local_flow" will also be tested for local flow.
88
#
9-
# All functions starting with "test_" should run and print `"OK"`.
9+
# All functions starting with "test_" should run and execute `print("OK")` one or more times.
1010
# This can be checked by running validTest.py.
1111

1212
# These are defined so that we can evaluate the test code.
@@ -366,3 +366,39 @@ def test_lambda_extra_keyword():
366366
def test_lambda_extra_keyword_flow():
367367
f_extra_keyword_flow = lambda **a : [*a][0] # return the name of the first extra keyword argument
368368
SINK(f_extra_keyword_flow(**{SOURCE: None})) # Flow missing
369+
370+
371+
def test_swap():
372+
a = SOURCE
373+
b = NONSOURCE
374+
SINK(a)
375+
SINK_F(b)
376+
377+
a, b = b, a
378+
SINK_F(a)
379+
SINK(b)
380+
381+
382+
def test_deep_callgraph():
383+
# port of python/ql/test/library-tests/taint/general/deep.py
384+
385+
def f1(arg):
386+
return arg
387+
388+
def f2(arg):
389+
return f1(arg)
390+
391+
def f3(arg):
392+
return f2(arg)
393+
394+
def f4(arg):
395+
return f3(arg)
396+
397+
def f5(arg):
398+
return f4(arg)
399+
400+
def f6(arg):
401+
return f5(arg)
402+
403+
x = f6(SOURCE)
404+
SINK(x) # Flow missing

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
def check_output(s, f):
2-
if s == "OK\n":
1+
def check_output(outtext, f):
2+
if outtext and all(s == "OK" for s in outtext.splitlines()):
33
pass
44
else:
5-
raise RuntimeError("Function failed", s, f)
5+
raise RuntimeError("Function failed", outtext, f)
66

77
def check_test_function(f):
88
from io import StringIO

0 commit comments

Comments
 (0)