Skip to content

Commit 277dbdf

Browse files
authored
Merge pull request #13498 from jketema/inline-4
Rework more inline expectation tests to use the parameterized module
2 parents 5afdaf8 + dba4460 commit 277dbdf

File tree

164 files changed

+687
-461
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+687
-461
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import go
22
import TestUtilities.InlineExpectationsTest
33

4-
class UntrustedFlowSourceTest extends InlineExpectationsTest {
5-
UntrustedFlowSourceTest() { this = "untrustedflowsource" }
4+
module UntrustedFlowSourceTest implements TestSig {
5+
string getARelevantTag() { result = "untrustedflowsource" }
66

7-
override string getARelevantTag() { result = "untrustedflowsource" }
8-
9-
override predicate hasActualResult(Location location, string element, string tag, string value) {
7+
predicate hasActualResult(Location location, string element, string tag, string value) {
108
tag = "untrustedflowsource" and
119
value = element and
1210
exists(UntrustedFlowSource src | value = "\"" + src.toString() + "\"" |
@@ -16,12 +14,10 @@ class UntrustedFlowSourceTest extends InlineExpectationsTest {
1614
}
1715
}
1816

19-
class HeaderWriteTest extends InlineExpectationsTest {
20-
HeaderWriteTest() { this = "headerwrite" }
21-
22-
override string getARelevantTag() { result = "headerwrite" }
17+
module HeaderWriteTest implements TestSig {
18+
string getARelevantTag() { result = "headerwrite" }
2319

24-
override predicate hasActualResult(Location location, string element, string tag, string value) {
20+
predicate hasActualResult(Location location, string element, string tag, string value) {
2521
tag = "headerwrite" and
2622
exists(Http::HeaderWrite hw, string name, string val | element = hw.toString() |
2723
hw.definesHeader(name, val) and
@@ -32,12 +28,10 @@ class HeaderWriteTest extends InlineExpectationsTest {
3228
}
3329
}
3430

35-
class LoggerTest extends InlineExpectationsTest {
36-
LoggerTest() { this = "LoggerTest" }
37-
38-
override string getARelevantTag() { result = "logger" }
31+
module LoggerTest implements TestSig {
32+
string getARelevantTag() { result = "logger" }
3933

40-
override predicate hasActualResult(Location location, string element, string tag, string value) {
34+
predicate hasActualResult(Location location, string element, string tag, string value) {
4135
exists(LoggerCall log |
4236
log.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
4337
location.getStartColumn(), location.getEndLine(), location.getEndColumn()) and
@@ -48,32 +42,32 @@ class LoggerTest extends InlineExpectationsTest {
4842
}
4943
}
5044

51-
class Config extends TaintTracking::Configuration {
52-
Config() { this = "goproxy config" }
53-
54-
override predicate isSource(DataFlow::Node n) {
45+
module Config implements DataFlow::ConfigSig {
46+
predicate isSource(DataFlow::Node n) {
5547
n = any(DataFlow::CallNode c | c.getCalleeName().matches("tainted%")).getResult()
5648
}
5749

58-
override predicate isSink(DataFlow::Node n) {
50+
predicate isSink(DataFlow::Node n) {
5951
n = any(DataFlow::CallNode cn | cn.getTarget().getName() = "sink").getAnArgument()
6052
}
6153
}
6254

63-
class TaintFlow extends InlineExpectationsTest {
64-
TaintFlow() { this = "goproxy flow" }
55+
module Flow = TaintTracking::Global<Config>;
6556

66-
override string getARelevantTag() { result = "taintflow" }
57+
module TaintFlow implements TestSig {
58+
string getARelevantTag() { result = "taintflow" }
6759

68-
override predicate hasActualResult(Location location, string element, string tag, string value) {
60+
predicate hasActualResult(Location location, string element, string tag, string value) {
6961
tag = "taintflow" and
7062
value = "" and
7163
element = "" and
72-
exists(Config c, DataFlow::Node toNode |
64+
exists(DataFlow::Node toNode |
7365
toNode
7466
.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
7567
location.getStartColumn(), location.getEndLine(), location.getEndColumn()) and
76-
c.hasFlowTo(toNode)
68+
Flow::flowTo(toNode)
7769
)
7870
}
7971
}
72+
73+
import MakeTest<MergeTests4<UntrustedFlowSourceTest, HeaderWriteTest, LoggerTest, TaintFlow>>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import go
22
import TestUtilities.InlineExpectationsTest
33

4-
class SqlTest extends InlineExpectationsTest {
5-
SqlTest() { this = "SQLTest" }
4+
module SqlTest implements TestSig {
5+
string getARelevantTag() { result = "query" }
66

7-
override string getARelevantTag() { result = "query" }
8-
9-
override predicate hasActualResult(Location location, string element, string tag, string value) {
7+
predicate hasActualResult(Location location, string element, string tag, string value) {
108
tag = "query" and
119
exists(SQL::Query q, SQL::QueryString qs | qs = q.getAQueryString() |
1210
q.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
@@ -17,12 +15,10 @@ class SqlTest extends InlineExpectationsTest {
1715
}
1816
}
1917

20-
class QueryString extends InlineExpectationsTest {
21-
QueryString() { this = "QueryString no Query" }
22-
23-
override string getARelevantTag() { result = "querystring" }
18+
module QueryString implements TestSig {
19+
string getARelevantTag() { result = "querystring" }
2420

25-
override predicate hasActualResult(Location location, string element, string tag, string value) {
21+
predicate hasActualResult(Location location, string element, string tag, string value) {
2622
tag = "querystring" and
2723
element = "" and
2824
exists(SQL::QueryString qs | not exists(SQL::Query q | qs = q.getAQueryString()) |
@@ -33,30 +29,30 @@ class QueryString extends InlineExpectationsTest {
3329
}
3430
}
3531

36-
class Config extends TaintTracking::Configuration {
37-
Config() { this = "pg-orm config" }
32+
module Config implements DataFlow::ConfigSig {
33+
predicate isSource(DataFlow::Node n) { n.asExpr() instanceof StringLit }
3834

39-
override predicate isSource(DataFlow::Node n) { n.asExpr() instanceof StringLit }
40-
41-
override predicate isSink(DataFlow::Node n) {
35+
predicate isSink(DataFlow::Node n) {
4236
n = any(DataFlow::CallNode cn | cn.getTarget().getName() = "sink").getAnArgument()
4337
}
4438
}
4539

46-
class TaintFlow extends InlineExpectationsTest {
47-
TaintFlow() { this = "pg-orm flow" }
40+
module Flow = TaintTracking::Global<Config>;
4841

49-
override string getARelevantTag() { result = "flowfrom" }
42+
module TaintFlow implements TestSig {
43+
string getARelevantTag() { result = "flowfrom" }
5044

51-
override predicate hasActualResult(Location location, string element, string tag, string value) {
45+
predicate hasActualResult(Location location, string element, string tag, string value) {
5246
tag = "flowfrom" and
5347
element = "" and
54-
exists(Config c, DataFlow::Node fromNode, DataFlow::Node toNode |
48+
exists(DataFlow::Node fromNode, DataFlow::Node toNode |
5549
toNode
5650
.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
5751
location.getStartColumn(), location.getEndLine(), location.getEndColumn()) and
58-
c.hasFlow(fromNode, toNode) and
52+
Flow::flow(fromNode, toNode) and
5953
value = fromNode.asExpr().(StringLit).getValue()
6054
)
6155
}
6256
}
57+
58+
import MakeTest<MergeTests3<SqlTest, QueryString, TaintFlow>>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures

go/ql/test/library-tests/semmle/go/frameworks/Yaml/tests.ql

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,29 @@ DataFlow::CallNode getAYamlCall() {
1111
isYamlFunction(result.getACalleeIncludingExternals().asFunction())
1212
}
1313

14-
class TaintTransitsFunctionConfig extends TaintTracking::Configuration {
15-
TaintTransitsFunctionConfig() { this = "TaintTransitsFunctionConfig" }
16-
17-
predicate isSourceSinkPair(DataFlow::Node inNode, DataFlow::Node outNode) {
18-
exists(DataFlow::CallNode cn | cn = getAYamlCall() |
19-
inNode = [cn.getAnArgument(), cn.getReceiver()] and
20-
(
21-
outNode.(DataFlow::PostUpdateNode).getPreUpdateNode() =
22-
[cn.getAnArgument(), cn.getReceiver()]
23-
or
24-
outNode = cn.getAResult()
25-
)
14+
predicate isSourceSinkPair(DataFlow::Node inNode, DataFlow::Node outNode) {
15+
exists(DataFlow::CallNode cn | cn = getAYamlCall() |
16+
inNode = [cn.getAnArgument(), cn.getReceiver()] and
17+
(
18+
outNode.(DataFlow::PostUpdateNode).getPreUpdateNode() = [cn.getAnArgument(), cn.getReceiver()]
19+
or
20+
outNode = cn.getAResult()
2621
)
27-
}
22+
)
23+
}
2824

29-
override predicate isSource(DataFlow::Node n) { this.isSourceSinkPair(n, _) }
25+
module TaintTransitsFunctionConfig implements DataFlow::ConfigSig {
26+
predicate isSource(DataFlow::Node n) { isSourceSinkPair(n, _) }
3027

31-
override predicate isSink(DataFlow::Node n) { this.isSourceSinkPair(_, n) }
28+
predicate isSink(DataFlow::Node n) { isSourceSinkPair(_, n) }
3229
}
3330

34-
class TaintFunctionModelTest extends InlineExpectationsTest {
35-
TaintFunctionModelTest() { this = "TaintFunctionModelTest" }
31+
module TaintTransitsFunctionFlow = TaintTracking::Global<TaintTransitsFunctionConfig>;
3632

37-
override string getARelevantTag() { result = "ttfnmodelstep" }
33+
module TaintFunctionModelTest implements TestSig {
34+
string getARelevantTag() { result = "ttfnmodelstep" }
3835

39-
override predicate hasActualResult(Location location, string element, string tag, string value) {
36+
predicate hasActualResult(Location location, string element, string tag, string value) {
4037
tag = "ttfnmodelstep" and
4138
(
4239
exists(TaintTracking::FunctionModel model, DataFlow::CallNode call | call = model.getACall() |
@@ -46,9 +43,9 @@ class TaintFunctionModelTest extends InlineExpectationsTest {
4643
value = "\"" + model.getAnInputNode(call) + " -> " + model.getAnOutputNode(call) + "\""
4744
)
4845
or
49-
exists(TaintTransitsFunctionConfig config, DataFlow::Node arg, DataFlow::Node output |
50-
config.hasFlow(arg, output) and
51-
config.isSourceSinkPair(arg, output) and
46+
exists(DataFlow::Node arg, DataFlow::Node output |
47+
TaintTransitsFunctionFlow::flow(arg, output) and
48+
isSourceSinkPair(arg, output) and
5249
arg.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
5350
location.getStartColumn(), location.getEndLine(), location.getEndColumn()) and
5451
element = arg.toString() and
@@ -58,12 +55,10 @@ class TaintFunctionModelTest extends InlineExpectationsTest {
5855
}
5956
}
6057

61-
class MarshalerTest extends InlineExpectationsTest {
62-
MarshalerTest() { this = "MarshalerTest" }
63-
64-
override string getARelevantTag() { result = "marshaler" }
58+
module MarshalerTest implements TestSig {
59+
string getARelevantTag() { result = "marshaler" }
6560

66-
override predicate hasActualResult(Location location, string element, string tag, string value) {
61+
predicate hasActualResult(Location location, string element, string tag, string value) {
6762
tag = "marshaler" and
6863
exists(MarshalingFunction m, DataFlow::CallNode call | call = m.getACall() |
6964
call.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
@@ -76,12 +71,10 @@ class MarshalerTest extends InlineExpectationsTest {
7671
}
7772
}
7873

79-
class UnmarshalerTest extends InlineExpectationsTest {
80-
UnmarshalerTest() { this = "UnmarshalerTest" }
74+
module UnmarshalerTest implements TestSig {
75+
string getARelevantTag() { result = "unmarshaler" }
8176

82-
override string getARelevantTag() { result = "unmarshaler" }
83-
84-
override predicate hasActualResult(Location location, string element, string tag, string value) {
77+
predicate hasActualResult(Location location, string element, string tag, string value) {
8578
tag = "unmarshaler" and
8679
exists(UnmarshalingFunction m, DataFlow::CallNode call | call = m.getACall() |
8780
call.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(),
@@ -93,3 +86,5 @@ class UnmarshalerTest extends InlineExpectationsTest {
9386
)
9487
}
9588
}
89+
90+
import MakeTest<MergeTests3<TaintFunctionModelTest, MarshalerTest, UnmarshalerTest>>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import semmle.python.dataflow.new.DataFlow
33
import TestUtilities.InlineExpectationsTest
44
private import semmle.python.dataflow.new.internal.PrintNode
55

6-
class DataFlowQueryTest extends InlineExpectationsTest {
7-
DataFlowQueryTest() { this = "DataFlowQueryTest" }
6+
module DataFlowQueryTest implements TestSig {
7+
string getARelevantTag() { result = "result" }
88

9-
override string getARelevantTag() { result = "result" }
10-
11-
override predicate hasActualResult(Location location, string element, string tag, string value) {
9+
predicate hasActualResult(Location location, string element, string tag, string value) {
1210
exists(DataFlow::Configuration cfg, DataFlow::Node sink | cfg.hasFlowTo(sink) |
1311
location = sink.getLocation() and
1412
tag = "result" and
@@ -22,7 +20,7 @@ class DataFlowQueryTest extends InlineExpectationsTest {
2220
// Sometimes a line contains both an alert and a safe sink.
2321
// In this situation, the annotation form `OK(safe sink)`
2422
// can be useful.
25-
override predicate hasOptionalResult(Location location, string element, string tag, string value) {
23+
predicate hasOptionalResult(Location location, string element, string tag, string value) {
2624
exists(DataFlow::Configuration cfg, DataFlow::Node sink |
2725
cfg.isSink(sink) or cfg.isSink(sink, _)
2826
|
@@ -34,6 +32,8 @@ class DataFlowQueryTest extends InlineExpectationsTest {
3432
}
3533
}
3634

35+
import MakeTest<DataFlowQueryTest>
36+
3737
query predicate missingAnnotationOnSink(Location location, string error, string element) {
3838
error = "ERROR, you should add `# $ MISSING: result=BAD` or `result=OK` annotation" and
3939
exists(DataFlow::Node sink |
@@ -42,13 +42,13 @@ query predicate missingAnnotationOnSink(Location location, string error, string
4242
location = sink.getLocation() and
4343
element = prettyExpr(sink.asExpr()) and
4444
not exists(DataFlow::Configuration cfg | cfg.hasFlowTo(sink)) and
45-
not exists(FalseNegativeExpectation missingResult |
45+
not exists(FalseNegativeTestExpectation missingResult |
4646
missingResult.getTag() = "result" and
4747
missingResult.getValue() = "BAD" and
4848
missingResult.getLocation().getFile() = location.getFile() and
4949
missingResult.getLocation().getStartLine() = location.getStartLine()
5050
) and
51-
not exists(GoodExpectation okResult |
51+
not exists(GoodTestExpectation okResult |
5252
okResult.getTag() = "result" and
5353
okResult.getValue() in ["OK", "OK(" + prettyNode(sink) + ")"] and
5454
okResult.getLocation().getFile() = location.getFile() and

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ import semmle.python.dataflow.new.DataFlow
33
import TestUtilities.InlineExpectationsTest
44
private import semmle.python.dataflow.new.internal.PrintNode
55

6-
abstract class FlowTest extends InlineExpectationsTest {
7-
bindingset[this]
8-
FlowTest() { any() }
6+
signature module FlowTestSig {
7+
string flowTag();
98

10-
abstract string flowTag();
11-
12-
abstract predicate relevantFlow(DataFlow::Node fromNode, DataFlow::Node toNode);
9+
predicate relevantFlow(DataFlow::Node fromNode, DataFlow::Node toNode);
10+
}
1311

14-
override string getARelevantTag() { result = this.flowTag() }
12+
private module FlowTest<FlowTestSig Impl> implements TestSig {
13+
string getARelevantTag() { result = Impl::flowTag() }
1514

16-
override predicate hasActualResult(Location location, string element, string tag, string value) {
17-
exists(DataFlow::Node fromNode, DataFlow::Node toNode | this.relevantFlow(fromNode, toNode) |
15+
predicate hasActualResult(Location location, string element, string tag, string value) {
16+
exists(DataFlow::Node fromNode, DataFlow::Node toNode | Impl::relevantFlow(fromNode, toNode) |
1817
location = toNode.getLocation() and
19-
tag = this.flowTag() and
18+
tag = Impl::flowTag() and
2019
value =
21-
"\"" + prettyNode(fromNode).replaceAll("\"", "'") + this.lineStr(fromNode, toNode) + " -> " +
20+
"\"" + prettyNode(fromNode).replaceAll("\"", "'") + lineStr(fromNode, toNode) + " -> " +
2221
prettyNode(toNode).replaceAll("\"", "'") + "\"" and
2322
element = toNode.toString()
2423
)
@@ -38,3 +37,11 @@ abstract class FlowTest extends InlineExpectationsTest {
3837
)
3938
}
4039
}
40+
41+
module MakeFlowTest<FlowTestSig Impl> {
42+
import MakeTest<FlowTest<Impl>>
43+
}
44+
45+
module MakeFlowTest2<FlowTestSig Impl1, FlowTestSig Impl2> {
46+
import MakeTest<MergeTests<FlowTest<Impl1>, FlowTest<Impl2>>>
47+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import python
22
import semmle.python.dataflow.new.DataFlow
33
import FlowTest
44

5-
class LocalFlowStepTest extends FlowTest {
6-
LocalFlowStepTest() { this = "LocalFlowStepTest" }
5+
module LocalFlowStepTest implements FlowTestSig {
6+
string flowTag() { result = "step" }
77

8-
override string flowTag() { result = "step" }
9-
10-
override predicate relevantFlow(DataFlow::Node fromNode, DataFlow::Node toNode) {
8+
predicate relevantFlow(DataFlow::Node fromNode, DataFlow::Node toNode) {
119
DataFlow::localFlowStep(fromNode, toNode)
1210
}
1311
}
12+
13+
import MakeFlowTest<LocalFlowStepTest>

0 commit comments

Comments
 (0)