Skip to content

Commit

Permalink
Refactor tests which extends Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
egregius313 committed Apr 18, 2023
1 parent 66f971e commit 49521f6
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 64 deletions.
15 changes: 7 additions & 8 deletions java/ql/test/library-tests/dataflow/capture/test.ql
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import java
import semmle.code.java.dataflow.DataFlow
import DataFlow

StringLiteral src() { result.getCompilationUnit().fromSource() }

class Conf extends Configuration {
Conf() { this = "qq capture" }
module Config implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node n) { n.asExpr() = src() }

override predicate isSource(Node n) { n.asExpr() = src() }

override predicate isSink(Node n) { any() }
predicate isSink(DataFlow::Node n) { any() }
}

from Node src, Node sink, Conf conf
where conf.hasFlow(src, sink)
module Flow = DataFlow::Global<Config>;

from DataFlow::Node src, DataFlow::Node sink
where Flow::flow(src, sink)
select src, sink
15 changes: 7 additions & 8 deletions java/ql/test/library-tests/dataflow/records/test.ql
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import java
import semmle.code.java.dataflow.DataFlow
import DataFlow

class Conf extends Configuration {
Conf() { this = "qqconf" }
module Config implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node n) { n.asExpr().(MethodAccess).getMethod().hasName("source") }

override predicate isSource(Node n) { n.asExpr().(MethodAccess).getMethod().hasName("source") }

override predicate isSink(Node n) { n.asExpr().(Argument).getCall().getCallee().hasName("sink") }
predicate isSink(DataFlow::Node n) { n.asExpr().(Argument).getCall().getCallee().hasName("sink") }
}

from Conf conf, Node src, Node sink
where conf.hasFlow(src, sink)
module Flow = DataFlow::Global<Config>;

from DataFlow::Node src, DataFlow::Node sink
where Flow::flow(src, sink)
select src, sink
15 changes: 7 additions & 8 deletions java/ql/test/library-tests/dataflow/switchexpr/switchexprflow.ql
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import java
import semmle.code.java.dataflow.DataFlow
import DataFlow

class Conf extends Configuration {
Conf() { this = "qqconf" }
module Config implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node n) { n.asExpr().(MethodAccess).getMethod().hasName("source") }

override predicate isSource(Node n) { n.asExpr().(MethodAccess).getMethod().hasName("source") }

override predicate isSink(Node n) { any() }
predicate isSink(DataFlow::Node n) { any() }
}

from Conf c, Node sink
where c.hasFlow(_, sink)
module Flow = DataFlow::Global<Config>;

from DataFlow::Node sink
where Flow::flowTo(sink)
select sink
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import java
import semmle.code.java.dataflow.DataFlow
import DataFlow

class Conf extends Configuration {
Conf() { this = "taintgettersetter" }
module Config implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node n) { n.asExpr().(MethodAccess).getMethod().hasName("taint") }

override predicate isSource(Node n) { n.asExpr().(MethodAccess).getMethod().hasName("taint") }

override predicate isSink(Node n) {
predicate isSink(DataFlow::Node n) {
exists(MethodAccess sink |
sink.getAnArgument() = n.asExpr() and sink.getMethod().hasName("sink")
)
}

override predicate isAdditionalFlowStep(Node n1, Node n2) {
predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
exists(AddExpr add |
add.getType() instanceof TypeString and add.getAnOperand() = n1.asExpr() and n2.asExpr() = add
)
}
}

from Node src, Node sink, Conf conf
where conf.hasFlow(src, sink)
module Flow = DataFlow::Global<Config>;

from DataFlow::Node src, DataFlow::Node sink
where Flow::flow(src, sink)
select src, sink
34 changes: 20 additions & 14 deletions java/ql/test/library-tests/dataflow/taintreturn/taintreturn.ql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import java
import semmle.code.java.dataflow.TaintTracking
import DataFlow

predicate step(Expr e1, Expr e2) {
exists(MethodAccess ma |
Expand All @@ -17,28 +16,35 @@ predicate isSink0(Expr sink) {
)
}

class Conf1 extends Configuration {
Conf1() { this = "testconf1" }
module FirstConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node n) { n.asExpr().(MethodAccess).getMethod().hasName("src") }

override predicate isSource(Node n) { n.asExpr().(MethodAccess).getMethod().hasName("src") }
predicate isSink(DataFlow::Node n) { any() }

override predicate isSink(Node n) { any() }

override predicate isAdditionalFlowStep(Node n1, Node n2) { step(n1.asExpr(), n2.asExpr()) }
predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
step(n1.asExpr(), n2.asExpr())
}
}

class Conf2 extends Configuration {
Conf2() { this = "testconf2" }
module FirstFlow = DataFlow::Global<FirstConfig>;

override predicate isSource(Node n) { n.asExpr().(MethodAccess).getMethod().hasName("src") }
module SecondConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node n) { n.asExpr().(MethodAccess).getMethod().hasName("src") }

override predicate isSink(Node n) { isSink0(n.asExpr()) }
predicate isSink(DataFlow::Node n) { isSink0(n.asExpr()) }

override predicate isAdditionalFlowStep(Node n1, Node n2) { step(n1.asExpr(), n2.asExpr()) }
predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
step(n1.asExpr(), n2.asExpr())
}
}

module SecondFlow = DataFlow::Global<SecondConfig>;

from int i1, int i2
where
i1 = count(Node src, Node sink, Conf1 c | c.hasFlow(src, sink) and isSink0(sink.asExpr())) and
i2 = count(Node src, Node sink, Conf2 c | c.hasFlow(src, sink))
i1 =
count(DataFlow::Node src, DataFlow::Node sink |
FirstFlow::flow(src, sink) and isSink0(sink.asExpr())
) and
i2 = count(DataFlow::Node src, DataFlow::Node sink | SecondFlow::flow(src, sink))
select i1, i2
17 changes: 8 additions & 9 deletions java/ql/test/library-tests/dataflow/this-flow/this-flow.ql
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import java
import semmle.code.java.dataflow.DataFlow
import DataFlow

class ThisFlowConfig extends Configuration {
ThisFlowConfig() { this = "ThisFlowConfig" }

override predicate isSource(Node src) {
exists(PostUpdateNode cie | cie.asExpr() instanceof ClassInstanceExpr |
module ThisFlowConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node src) {
exists(DataFlow::PostUpdateNode cie | cie.asExpr() instanceof ClassInstanceExpr |
cie.getPreUpdateNode() = src or cie = src
)
}

override predicate isSink(Node sink) { any() }
predicate isSink(DataFlow::Node sink) { any() }
}

from Node n, ThisFlowConfig conf
where conf.hasFlow(_, n)
module ThisFlow = DataFlow::Global<ThisFlowConfig>;

from DataFlow::Node n
where ThisFlow::flowTo(n)
select n
16 changes: 8 additions & 8 deletions java/ql/test/library-tests/dataflow/typepruning/test.ql
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import java
import semmle.code.java.dataflow.DataFlow
import DataFlow

class Conf extends Configuration {
Conf() { this = "test types" }
module Config implements DataFlow::ConfigSig {
predicate isSource(Node n) { n.asExpr().(MethodAccess).getMethod().hasName("source") }

override predicate isSource(Node n) { n.asExpr().(MethodAccess).getMethod().hasName("source") }

override predicate isSink(Node n) {
predicate isSink(Node n) {
exists(MethodAccess sink |
sink.getAnArgument() = n.asExpr() and sink.getMethod().hasName("sink")
)
}

override predicate isAdditionalFlowStep(Node n1, Node n2) {
predicate isAdditionalFlowStep(Node n1, Node n2) {
exists(MethodAccess ma |
ma.getMethod().hasName("customStep") and
ma.getAnArgument() = n1.asExpr() and
Expand All @@ -22,6 +20,8 @@ class Conf extends Configuration {
}
}

from Node src, Node sink, Conf conf
where conf.hasFlow(src, sink)
module Flow = DataFlow::Global<Config>;

from Node src, Node sink
where Flow::flow(src, sink)
select src, sink, sink.getEnclosingCallable()

0 comments on commit 49521f6

Please sign in to comment.