Skip to content

Commit 61ad5d0

Browse files
committed
Python: Allow printing PostUpdateNode in ConceptsTest.qll
See how this works in `test_json.py`
1 parent 9dbb364 commit 61ad5d0

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

python/ql/test/experimental/dataflow/TestUtil/PrintNode.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ string prettyExpr(Expr e) {
2626
result = prettyExpr(e.(Attribute).getObject()) + "." + e.(Attribute).getName()
2727
}
2828

29+
/**
30+
* Gets pretty-printed version of the DataFlow::Node `node`
31+
*/
32+
bindingset[node]
2933
string prettyNode(DataFlow::Node node) {
3034
if exists(node.asExpr()) then result = prettyExpr(node.asExpr()) else result = node.toString()
3135
}
36+
37+
/**
38+
* Gets pretty-printed version of the DataFlow::Node `node`, that is suitable for use
39+
* with `TestUtilities.InlineExpectationsTest` (that is, no spaces unless required).
40+
*/
41+
bindingset[node]
42+
string prettyNodeForInlineTest(DataFlow::Node node) {
43+
exists(node.asExpr()) and
44+
result = prettyExpr(node.asExpr())
45+
or
46+
exists(Expr e | e = node.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() |
47+
// since PostUpdateNode both has space in the `[post <thing>]` annotation, and does
48+
// not pretty print the pre-update node, we do custom handling of this.
49+
result = "[post]" + prettyExpr(e)
50+
)
51+
or
52+
not exists(node.asExpr()) and
53+
not exists(Expr e | e = node.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr()) and
54+
result = node.toString()
55+
}

python/ql/test/experimental/meta/ConceptsTest.qll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SystemCommandExecutionTest extends InlineExpectationsTest {
1515
command = sce.getCommand() and
1616
location = command.getLocation() and
1717
element = command.toString() and
18-
value = prettyExpr(command.asExpr()) and
18+
value = prettyNodeForInlineTest(command) and
1919
tag = "getCommand"
2020
)
2121
}
@@ -34,7 +34,7 @@ class DecodingTest extends InlineExpectationsTest {
3434
exists(DataFlow::Node data |
3535
location = data.getLocation() and
3636
element = data.toString() and
37-
value = prettyExpr(data.asExpr()) and
37+
value = prettyNodeForInlineTest(data) and
3838
(
3939
data = d.getAnInput() and
4040
tag = "decodeInput"
@@ -72,7 +72,7 @@ class EncodingTest extends InlineExpectationsTest {
7272
exists(DataFlow::Node data |
7373
location = data.getLocation() and
7474
element = data.toString() and
75-
value = prettyExpr(data.asExpr()) and
75+
value = prettyNodeForInlineTest(data) and
7676
(
7777
data = e.getAnInput() and
7878
tag = "encodeInput"
@@ -105,7 +105,7 @@ class CodeExecutionTest extends InlineExpectationsTest {
105105
code = ce.getCode() and
106106
location = code.getLocation() and
107107
element = code.toString() and
108-
value = prettyExpr(code.asExpr()) and
108+
value = prettyNodeForInlineTest(code) and
109109
tag = "getCode"
110110
)
111111
}
@@ -123,7 +123,7 @@ class SqlExecutionTest extends InlineExpectationsTest {
123123
sql = e.getSql() and
124124
location = e.getLocation() and
125125
element = sql.toString() and
126-
value = prettyExpr(sql.asExpr()) and
126+
value = prettyNodeForInlineTest(sql) and
127127
tag = "getSql"
128128
)
129129
}
@@ -206,7 +206,7 @@ class HttpServerHttpResponseTest extends InlineExpectationsTest {
206206
exists(HTTP::Server::HttpResponse response |
207207
location = response.getLocation() and
208208
element = response.toString() and
209-
value = prettyExpr(response.getBody().asExpr()) and
209+
value = prettyNodeForInlineTest(response.getBody()) and
210210
tag = "responseBody"
211211
)
212212
or
@@ -245,7 +245,7 @@ class HttpServerHttpRedirectResponseTest extends InlineExpectationsTest {
245245
exists(HTTP::Server::HttpRedirectResponse redirect |
246246
location = redirect.getLocation() and
247247
element = redirect.toString() and
248-
value = prettyExpr(redirect.getRedirectLocation().asExpr()) and
248+
value = prettyNodeForInlineTest(redirect.getRedirectLocation()) and
249249
tag = "redirectLocation"
250250
)
251251
)
@@ -263,7 +263,7 @@ class FileSystemAccessTest extends InlineExpectationsTest {
263263
path = a.getAPathArgument() and
264264
location = a.getLocation() and
265265
element = path.toString() and
266-
value = prettyExpr(path.asExpr()) and
266+
value = prettyNodeForInlineTest(path) and
267267
tag = "getAPathArgument"
268268
)
269269
}
@@ -297,7 +297,7 @@ class SafeAccessCheckTest extends InlineExpectationsTest {
297297
location = c.getLocation() and
298298
(
299299
element = checks.toString() and
300-
value = prettyExpr(checks.asExpr()) and
300+
value = prettyNodeForInlineTest(checks) and
301301
tag = "checks"
302302
or
303303
element = branch.toString() and

python/ql/test/library-tests/frameworks/stdlib/test_json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test():
1717

1818
# load/dump with file-like
1919
tainted_filelike = StringIO()
20-
json.dump(ts, tainted_filelike) # $ encodeFormat=JSON encodeInput=ts
20+
json.dump(ts, tainted_filelike) # $ encodeOutput=[post]tainted_filelike encodeFormat=JSON encodeInput=ts
2121

2222
tainted_filelike.seek(0)
2323
ensure_tainted(
@@ -27,7 +27,7 @@ def test():
2727

2828
# load/dump with file-like using keyword-args
2929
tainted_filelike = StringIO()
30-
json.dump(obj=ts, fp=tainted_filelike) # $ encodeFormat=JSON encodeInput=ts
30+
json.dump(obj=ts, fp=tainted_filelike) # $ encodeOutput=[post]tainted_filelike encodeFormat=JSON encodeInput=ts
3131

3232
tainted_filelike.seek(0)
3333
ensure_tainted(

0 commit comments

Comments
 (0)