Skip to content

Commit 39f4ac2

Browse files
committed
Java: Add a copy of all testutilities.
1 parent cbf58f3 commit 39f4ac2

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Inline expectation tests for Java.
3+
* See `shared/util/codeql/util/test/InlineExpectationsTest.qll`
4+
*/
5+
6+
private import codeql.util.test.InlineExpectationsTest
7+
private import internal.InlineExpectationsTestImpl
8+
import Make<Impl>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @kind test-postprocess
3+
*/
4+
5+
private import java
6+
private import codeql.util.test.InlineExpectationsTest as T
7+
private import internal.InlineExpectationsTestImpl
8+
import T::TestPostProcessing
9+
import T::TestPostProcessing::Make<Impl, Input>
10+
11+
private module Input implements T::TestPostProcessing::InputSig<Impl> {
12+
string getRelativeUrl(Location location) {
13+
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
14+
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
15+
f = location.getFile()
16+
|
17+
result =
18+
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
19+
)
20+
}
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Inline flow tests for Java.
3+
* See `shared/util/codeql/dataflow/test/InlineFlowTest.qll`
4+
*/
5+
6+
import java
7+
import semmle.code.java.dataflow.DataFlow
8+
private import codeql.dataflow.test.InlineFlowTest
9+
private import semmle.code.java.dataflow.internal.DataFlowImplSpecific
10+
private import semmle.code.java.dataflow.internal.TaintTrackingImplSpecific
11+
private import semmle.code.java.dataflow.ExternalFlow as ExternalFlow
12+
private import internal.InlineExpectationsTestImpl
13+
14+
private module FlowTestImpl implements InputSig<Location, JavaDataFlow> {
15+
predicate defaultSource(DataFlow::Node source) {
16+
source.asExpr().(MethodCall).getMethod().getName() = ["source", "taint"]
17+
}
18+
19+
predicate defaultSink(DataFlow::Node sink) {
20+
exists(MethodCall ma | ma.getMethod().hasName("sink") | sink.asExpr() = ma.getAnArgument())
21+
}
22+
23+
private string getSourceArgString(DataFlow::Node src) {
24+
defaultSource(src) and
25+
src.asExpr().(MethodCall).getAnArgument().(StringLiteral).getValue() = result
26+
}
27+
28+
string getArgString(DataFlow::Node src, DataFlow::Node sink) {
29+
(if exists(getSourceArgString(src)) then result = getSourceArgString(src) else result = "") and
30+
exists(sink)
31+
}
32+
33+
predicate interpretModelForTest = ExternalFlow::interpretModelForTest/2;
34+
}
35+
36+
import InlineFlowTestMake<Location, JavaDataFlow, JavaTaintTracking, Impl, FlowTestImpl>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
private import java as J
2+
private import codeql.mad.test.InlineMadTest
3+
4+
private module InlineMadTestLang implements InlineMadTestLangSig {
5+
class Callable = J::Callable;
6+
7+
string getComment(Callable c) {
8+
exists(J::Javadoc doc |
9+
hasJavadoc(c, doc) and
10+
isNormalComment(doc) and
11+
result = doc.getChild(0).toString()
12+
)
13+
}
14+
}
15+
16+
import InlineMadTestImpl<InlineMadTestLang>

java/test/TestUtilities/PrettyPrintModels.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
*/
44

55
import semmle.code.java.dataflow.ExternalFlow
6-
import codeql.dataflow.test.ProvenancePathGraph
76
import codeql.dataflow.test.ProvenancePathGraph::TestPostProcessing::TranslateProvenanceResults<interpretModelForTest/2>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
private import java as J
2+
private import codeql.util.test.InlineExpectationsTest
3+
4+
module Impl implements InlineExpectationsTestSig {
5+
/**
6+
* A class representing line comments in Java, which is simply Javadoc restricted
7+
* to EOL comments, with an extra accessor used by the InlineExpectations core code
8+
*/
9+
abstract class ExpectationComment extends J::Top {
10+
/** Gets the contents of the given comment, _without_ the preceding comment marker (`//`). */
11+
abstract string getContents();
12+
}
13+
14+
private class JavadocExpectationComment extends J::Javadoc, ExpectationComment {
15+
JavadocExpectationComment() { isEolComment(this) }
16+
17+
override string getContents() { result = this.getChild(0).toString() }
18+
}
19+
20+
private class KtExpectationComment extends J::KtComment, ExpectationComment {
21+
KtExpectationComment() { this.isEolComment() }
22+
23+
override string getContents() { result = this.getText().suffix(2).trim() }
24+
}
25+
26+
private class XmlExpectationComment extends ExpectationComment instanceof J::XmlComment {
27+
override string getContents() { result = super.getText().trim() }
28+
29+
override Location getLocation() { result = J::XmlComment.super.getLocation() }
30+
31+
override string toString() { result = J::XmlComment.super.toString() }
32+
}
33+
34+
class Location = J::Location;
35+
}

0 commit comments

Comments
 (0)