Skip to content

Commit fc69ace

Browse files
committed
Java: Add test.
1 parent 9133ada commit fc69ace

File tree

5 files changed

+530
-8
lines changed

5 files changed

+530
-8
lines changed

java/ql/src/utils/flowtestcasegenerator/FlowTestCaseSupportMethods.qll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,22 @@ private class EnumerationGetMethod extends GetMethod {
217217
override string getCall(string arg) { result = "getElement(" + arg + ")" }
218218
}
219219

220+
private class StreamGetMethod extends GetMethod {
221+
StreamGetMethod() { this = "streamgetmethod" }
222+
223+
override predicate appliesTo(Type t, Content c) {
224+
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util.stream", "BaseStream") and
225+
c instanceof CollectionContent
226+
}
227+
228+
override string getDefinition() {
229+
result = "<T> T getElement(BaseStream<T,?> s) { return s.iterator().next(); }"
230+
}
231+
232+
bindingset[arg]
233+
override string getCall(string arg) { result = "getElement(" + arg + ")" }
234+
}
235+
220236
private class OptionalGetMethod extends GetMethod {
221237
OptionalGetMethod() { this = "optionalgetmethod" }
222238

@@ -392,6 +408,20 @@ private class IteratorGenMethod extends GenMethod {
392408
override string getCall(string arg) { result = "List.of(" + arg + ").iterator()" }
393409
}
394410

411+
private class StreamGenMethod extends GenMethod {
412+
StreamGenMethod() { this = "streamgenmethod" }
413+
414+
override predicate appliesTo(Type t, Content c) {
415+
exists(GenericType op | op.hasQualifiedName("java.util.stream", ["BaseStream", "Stream"]) |
416+
op.getAParameterizedType().getASupertype*() = t
417+
) and
418+
c instanceof CollectionContent
419+
}
420+
421+
bindingset[arg]
422+
override string getCall(string arg) { result = "Stream.of(" + arg + ")" }
423+
}
424+
395425
private class OptionalGenMethod extends GenMethod {
396426
OptionalGenMethod() { this = "optionalgenmethod" }
397427

java/ql/test/TestUtilities/InlineFlowTest.qll

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ import semmle.code.java.dataflow.ExternalFlow
4343
import semmle.code.java.dataflow.TaintTracking
4444
import TestUtilities.InlineExpectationsTest
4545

46+
private predicate defaultSource(DataFlow::Node src) {
47+
src.asExpr().(MethodAccess).getMethod().getName() = ["source", "taint"]
48+
}
49+
4650
class DefaultValueFlowConf extends DataFlow::Configuration {
4751
DefaultValueFlowConf() { this = "qltest:defaultValueFlowConf" }
4852

49-
override predicate isSource(DataFlow::Node n) {
50-
n.asExpr().(MethodAccess).getMethod().getName() = ["source", "taint"]
51-
}
53+
override predicate isSource(DataFlow::Node n) { defaultSource(n) }
5254

5355
override predicate isSink(DataFlow::Node n) {
5456
exists(MethodAccess ma | ma.getMethod().hasName("sink") | n.asExpr() = ma.getAnArgument())
@@ -60,9 +62,7 @@ class DefaultValueFlowConf extends DataFlow::Configuration {
6062
class DefaultTaintFlowConf extends TaintTracking::Configuration {
6163
DefaultTaintFlowConf() { this = "qltest:defaultTaintFlowConf" }
6264

63-
override predicate isSource(DataFlow::Node n) {
64-
n.asExpr().(MethodAccess).getMethod().getName() = ["source", "taint"]
65-
}
65+
override predicate isSource(DataFlow::Node n) { defaultSource(n) }
6666

6767
override predicate isSink(DataFlow::Node n) {
6868
exists(MethodAccess ma | ma.getMethod().hasName("sink") | n.asExpr() = ma.getAnArgument())
@@ -71,6 +71,11 @@ class DefaultTaintFlowConf extends TaintTracking::Configuration {
7171
override int fieldFlowBranchLimit() { result = 1000 }
7272
}
7373

74+
private string getSourceArgString(DataFlow::Node src) {
75+
defaultSource(src) and
76+
src.asExpr().(MethodAccess).getAnArgument().(StringLiteral).getValue() = result
77+
}
78+
7479
class InlineFlowTest extends InlineExpectationsTest {
7580
InlineFlowTest() { this = "HasFlowTest" }
7681

@@ -81,7 +86,7 @@ class InlineFlowTest extends InlineExpectationsTest {
8186
exists(DataFlow::Node src, DataFlow::Node sink | getValueFlowConfig().hasFlow(src, sink) |
8287
sink.getLocation() = location and
8388
element = sink.toString() and
84-
value = ""
89+
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
8590
)
8691
or
8792
tag = "hasTaintFlow" and
@@ -90,7 +95,7 @@ class InlineFlowTest extends InlineExpectationsTest {
9095
|
9196
sink.getLocation() = location and
9297
element = sink.toString() and
93-
value = ""
98+
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
9499
)
95100
}
96101

0 commit comments

Comments
 (0)