Skip to content

Commit 18e44b6

Browse files
author
Max Schaefer
committed
Turn AutomodelApplicationModeExtractCandidates into an inline-expectations test.
1 parent 587d69e commit 18e44b6

File tree

5 files changed

+54
-51
lines changed

5 files changed

+54
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,2 @@
1-
| PluginImpl.java:5:27:5:37 | name | hudson | Plugin | configure | (String,String) | | Parameter[0] | sourceModel |
2-
| PluginImpl.java:5:40:5:51 | value | hudson | Plugin | configure | (String,String) | | Parameter[1] | sourceModel |
3-
| Test.java:19:3:19:11 | reference | java.util.concurrent.atomic | AtomicReference | set | (Object) | Argument[this] | | sinkModel |
4-
| Test.java:19:3:19:24 | set(...) | java.util.concurrent.atomic | AtomicReference | set | (Object) | | ReturnValue | sourceModel |
5-
| Test.java:24:3:24:10 | supplier | java.util.function | Supplier | get | () | Argument[this] | | sinkModel |
6-
| Test.java:24:3:24:16 | get(...) | java.util.function | Supplier | get | () | | ReturnValue | sourceModel |
7-
| Test.java:28:3:32:3 | copy(...) | java.nio.file | Files | copy | (Path,Path,CopyOption[]) | | ReturnValue | sourceModel |
8-
| Test.java:36:10:38:3 | newInputStream(...) | java.nio.file | Files | newInputStream | (Path,OpenOption[]) | | ReturnValue | sourceModel |
9-
| Test.java:37:4:37:11 | openPath | java.nio.file | Files | newInputStream | (Path,OpenOption[]) | Argument[0] | | sinkModel |
10-
| Test.java:43:4:43:22 | get(...) | java.nio.file | Paths | get | (String,String[]) | | ReturnValue | sourceModel |
11-
| Test.java:54:3:59:3 | walk(...) | java.nio.file | Files | walk | (Path,FileVisitOption[]) | | ReturnValue | sourceModel |
12-
| Test.java:54:3:59:3 | walk(...) | java.nio.file | Files | walk | (Path,FileVisitOption[]) | Argument[1] | | sinkModel |
13-
| Test.java:63:3:63:3 | c | java.net | URLConnection | getInputStream | () | Argument[this] | | sinkModel |
14-
| Test.java:68:30:68:47 | writer | java.lang | Throwable | printStackTrace | (PrintWriter) | | Parameter[0] | sourceModel |
15-
| Test.java:86:3:88:3 | list(...) | java.nio.file | Files | list | (Path) | | ReturnValue | sourceModel |
16-
| Test.java:87:4:87:29 | createDirectories(...) | java.nio.file | Files | createDirectories | (Path,FileAttribute[]) | | ReturnValue | sourceModel |
17-
| Test.java:90:3:92:3 | delete(...) | java.nio.file | Files | delete | (Path) | | ReturnValue | sourceModel |
18-
| Test.java:91:4:91:4 | p | java.nio.file | Files | delete | (Path) | Argument[0] | | sinkModel |
19-
| Test.java:95:4:95:4 | p | java.nio.file | Files | deleteIfExists | (Path) | Argument[0] | | sinkModel |
1+
testFailures
2+
failures
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import java
22
import AutomodelApplicationModeCharacteristics
3+
import TestUtilities.InlineExpectationsTest
34

4-
from
5-
Endpoint endpoint, Top t, string package, string type, string name, string signature,
6-
string input, string output, string extensibleType
7-
where
8-
isCandidate(endpoint, package, type, _, name, signature, input, output, _, extensibleType, _) and
9-
t = endpoint.asTop()
10-
select t, package, type, name, signature, input, output, extensibleType
5+
module CandidateTest implements TestSig {
6+
string getARelevantTag() { result in ["sourceModel", "sinkModel"] }
7+
8+
predicate hasActualResult(Location location, string element, string tag, string value) {
9+
exists(
10+
Endpoint endpoint, string name, string signature, string input, string output,
11+
string extensibleType
12+
|
13+
isCandidate(endpoint, _, _, _, name, signature, input, output, _, extensibleType, _)
14+
|
15+
endpoint.asTop().getLocation() = location and
16+
endpoint.toString() = element and
17+
tag = extensibleType and
18+
// for source models only the output is relevant, and vice versa for sink models
19+
if extensibleType = "sourceModel"
20+
then value = name + signature + ":" + output
21+
else value = name + signature + ":" + input
22+
)
23+
}
24+
}
25+
26+
import MakeTest<CandidateTest>

java/ql/automodel/test/AutomodelApplicationModeExtraction/PluginImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class PluginImpl extends Plugin {
44
@Override
5-
public void configure(String name, String value) {
5+
public void configure(String name, String value) { // $ sourceModel=configure(String,String):Parameter[0] sourceModel=configure(String,String):Parameter[1]
66
// ...
77
}
88
}

java/ql/automodel/test/AutomodelApplicationModeExtraction/Test.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,59 @@
1616
class Test {
1717
public static void main(String[] args) throws Exception {
1818
AtomicReference<String> reference = new AtomicReference<>(); // uninteresting (parameterless constructor)
19-
reference.set(args[0]); // arg[0] is not a candidate (modeled as value flow step)
20-
// ^^^^^^ Argument[this] is a candidate
19+
reference.set( // $ sinkModel=set(Object):Argument[this]
20+
args[0] // not a sink candidate (modeled as a flow step)
21+
); // $ sourceModel=set(Object):ReturnValue
2122
}
2223

2324
public static void callSupplier(Supplier<String> supplier) {
24-
supplier.get(); // Argument[this] is a sink candidate; the call is a source candidate
25+
supplier.get(); // $ sourceModel=get():ReturnValue sinkModel=get():Argument[this]
2526
}
2627

2728
public static void copyFiles(Path source, Path target, CopyOption option) throws Exception {
28-
Files.copy( // the call is a source candidate
29+
Files.copy(
2930
source, // positive example (known sink)
3031
target, // positive example (known sink)
3132
option // no candidate (not modeled, but source and target are modeled)
32-
);
33+
); // $ sourceModel=copy(Path,Path,CopyOption[]):ReturnValue
3334
}
3435

3536
public static InputStream getInputStream(Path openPath) throws Exception {
36-
return Files.newInputStream( // the call is a source candidate
37-
openPath // positive example (known sink), candidate ("only" ai-modeled, and useful as a candidate in regression testing)
38-
);
37+
return Files.newInputStream(
38+
openPath // $ sinkModel=newInputStream(Path,OpenOption[]):Argument[0] // positive example (known sink), candidate ("only" ai-modeled, and useful as a candidate in regression testing)
39+
); // $ sourceModel=newInputStream(Path,OpenOption[]):ReturnValue
3940
}
4041

4142
public static InputStream getInputStream(String openPath) throws Exception {
4243
return Test.getInputStream( // the call is not a source candidate (argument to local call)
43-
Paths.get(openPath) // no sink candidate (argument to local call); the call is a source candidate
44+
Paths.get(
45+
openPath // not a sink candidate (argument to local call)
46+
) // $ sourceModel=get(String,String[]):ReturnValue
4447
);
4548
}
4649

4750
public static int compareFiles(File f1, File f2) {
48-
return f1.compareTo( // compareTo call is a known sanitizer
51+
return f1.compareTo(
4952
f2 // negative sink example (modeled as not a sink)
5053
); // the call is a negative source candidate (sanitizer)
5154
}
5255

5356
public static void FilesWalkExample(Path p, FileVisitOption o) throws Exception {
54-
Files.walk( // the call is a source candidate
57+
Files.walk(
5558
p, // negative sink example (modeled as a taint step)
56-
o, // the implicit varargs array is a candidate
59+
o, // the implicit varargs array is a candidate, annotated on the last line of the call
5760
o // not a candidate (only the first arg corresponding to a varargs array
5861
// is extracted)
59-
);
62+
); // $ sourceModel=walk(Path,FileVisitOption[]):ReturnValue sinkModel=walk(Path,FileVisitOption[]):Argument[1]
6063
}
6164

6265
public static void WebSocketExample(URLConnection c) throws Exception {
63-
c.getInputStream(); // the call is a source example, c is a sink candidate
66+
c.getInputStream(); // $ sinkModel=getInputStream():Argument[this] // not a source candidate (manual modeling)
6467
}
6568
}
6669

6770
class OverrideTest extends Exception {
68-
public void printStackTrace(PrintWriter writer) { // writer is a source candidate because it overrides an existing method
71+
public void printStackTrace(PrintWriter writer) { // $ sourceModel=printStackTrace(PrintWriter):Parameter[0]
6972
return;
7073
}
7174

@@ -83,16 +86,16 @@ public FutureTask getTask() {
8386

8487
class MoreTests {
8588
public static void FilesListExample(Path p) throws Exception {
86-
Files.list( // the call is a source candidate
87-
Files.createDirectories(p) // the call is a source candidate, but not a sink candidate (modeled as a taint step)
88-
);
89+
Files.list(
90+
Files.createDirectories(p) // $ sourceModel=createDirectories(Path,FileAttribute[]):ReturnValue // not a sink candidate (modeled as a taint step)
91+
); // $ sourceModel=list(Path):ReturnValue
8992

90-
Files.delete( // not a source candidate (return type is void)
91-
p // sink candidate
92-
);
93+
Files.delete(
94+
p // $ sinkModel=delete(Path):Argument[0]
95+
); // $ SPURIOUS: sourceModel=delete(Path):ReturnValue
9396

94-
Files.deleteIfExists( // not a source candidate (return type is boolean)
95-
p // sink candidate
96-
);
97+
Files.deleteIfExists(
98+
p // $ sinkModel=deleteIfExists(Path):Argument[0]
99+
); // not a source candidate (return type is boolean)
97100
}
98101
}

java/ql/automodel/test/qlpack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ groups:
77
dependencies:
88
codeql/java-all: ${workspace}
99
codeql/java-automodel-queries: ${workspace}
10+
codeql/java-tests: ${workspace}
1011
extractor: java
1112
tests: .
1213
warnOnImplicitThis: true

0 commit comments

Comments
 (0)