Java: Move test utilities to the java query pack.

This commit is contained in:
Michael Nebel
2024-11-12 16:34:59 +01:00
parent 066db766ef
commit 91cfb30513
6 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
/**
* Inline expectation tests for Java.
* See `shared/util/codeql/util/test/InlineExpectationsTest.qll`
*/
private import codeql.util.test.InlineExpectationsTest
private import internal.InlineExpectationsTestImpl
import Make<Impl>

View File

@@ -0,0 +1,21 @@
/**
* @kind test-postprocess
*/
private import java
private import codeql.util.test.InlineExpectationsTest as T
private import internal.InlineExpectationsTestImpl
import T::TestPostProcessing
import T::TestPostProcessing::Make<Impl, Input>
private module Input implements T::TestPostProcessing::InputSig<Impl> {
string getRelativeUrl(Location location) {
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
f = location.getFile()
|
result =
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
)
}
}

View File

@@ -0,0 +1,36 @@
/**
* Inline flow tests for Java.
* See `shared/util/codeql/dataflow/test/InlineFlowTest.qll`
*/
import java
import semmle.code.java.dataflow.DataFlow
private import codeql.dataflow.test.InlineFlowTest
private import semmle.code.java.dataflow.internal.DataFlowImplSpecific
private import semmle.code.java.dataflow.internal.TaintTrackingImplSpecific
private import semmle.code.java.dataflow.ExternalFlow as ExternalFlow
private import internal.InlineExpectationsTestImpl
private module FlowTestImpl implements InputSig<Location, JavaDataFlow> {
predicate defaultSource(DataFlow::Node source) {
source.asExpr().(MethodCall).getMethod().getName() = ["source", "taint"]
}
predicate defaultSink(DataFlow::Node sink) {
exists(MethodCall ma | ma.getMethod().hasName("sink") | sink.asExpr() = ma.getAnArgument())
}
private string getSourceArgString(DataFlow::Node src) {
defaultSource(src) and
src.asExpr().(MethodCall).getAnArgument().(StringLiteral).getValue() = result
}
string getArgString(DataFlow::Node src, DataFlow::Node sink) {
(if exists(getSourceArgString(src)) then result = getSourceArgString(src) else result = "") and
exists(sink)
}
predicate interpretModelForTest = ExternalFlow::interpretModelForTest/2;
}
import InlineFlowTestMake<Location, JavaDataFlow, JavaTaintTracking, Impl, FlowTestImpl>

View File

@@ -0,0 +1,16 @@
private import java as J
private import codeql.mad.test.InlineMadTest
private module InlineMadTestLang implements InlineMadTestLangSig {
class Callable = J::Callable;
string getComment(Callable c) {
exists(J::Javadoc doc |
hasJavadoc(c, doc) and
isNormalComment(doc) and
result = doc.getChild(0).toString()
)
}
}
import InlineMadTestImpl<InlineMadTestLang>

View File

@@ -0,0 +1,6 @@
/**
* @kind test-postprocess
*/
import semmle.code.java.dataflow.ExternalFlow
import codeql.dataflow.test.ProvenancePathGraph::TestPostProcessing::TranslateProvenanceResults<interpretModelForTest/2>

View File

@@ -0,0 +1,35 @@
private import java as J
private import codeql.util.test.InlineExpectationsTest
module Impl implements InlineExpectationsTestSig {
/**
* A class representing line comments in Java, which is simply Javadoc restricted
* to EOL comments, with an extra accessor used by the InlineExpectations core code
*/
abstract class ExpectationComment extends J::Top {
/** Gets the contents of the given comment, _without_ the preceding comment marker (`//`). */
abstract string getContents();
}
private class JavadocExpectationComment extends J::Javadoc, ExpectationComment {
JavadocExpectationComment() { isEolComment(this) }
override string getContents() { result = this.getChild(0).toString() }
}
private class KtExpectationComment extends J::KtComment, ExpectationComment {
KtExpectationComment() { this.isEolComment() }
override string getContents() { result = this.getText().suffix(2).trim() }
}
private class XmlExpectationComment extends ExpectationComment instanceof J::XmlComment {
override string getContents() { result = super.getText().trim() }
override Location getLocation() { result = J::XmlComment.super.getLocation() }
override string toString() { result = J::XmlComment.super.toString() }
}
class Location = J::Location;
}