Skip to content

Commit 6844b78

Browse files
committed
Java: Expanded test suite of java/visible-for-testing-abuse
1 parent 9670941 commit 6844b78

File tree

5 files changed

+157
-8
lines changed

5 files changed

+157
-8
lines changed
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
| packageone/SourcePackage.java:8:21:8:32 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
2-
| packagetwo/Source.java:7:17:7:29 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:12:16:12:16 | f | element |
1+
| packageone/SourcePackage.java:9:21:9:32 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
2+
| packageone/SourcePackage.java:10:21:10:32 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
3+
| packageone/SourcePackage.java:16:18:16:36 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
4+
| packageone/SourcePackage.java:17:18:17:39 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
5+
| packageone/SourcePackage.java:25:31:25:42 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
6+
| packageone/SourcePackage.java:26:31:26:42 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
7+
| packageone/SourcePackage.java:29:28:29:46 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
8+
| packageone/SourcePackage.java:30:28:30:49 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
9+
| packagetwo/Annotated.java:49:31:49:31 | m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
10+
| packagetwo/Annotated.java:50:32:50:33 | m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
11+
| packagetwo/Annotated.java:51:32:51:33 | m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
12+
| packagetwo/Annotated.java:54:26:54:28 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
13+
| packagetwo/Annotated.java:56:32:56:40 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
14+
| packagetwo/Annotated.java:57:35:57:46 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
15+
| packagetwo/Annotated.java:64:28:64:28 | m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
16+
| packagetwo/Annotated.java:69:26:69:28 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
317
| packagetwo/Source.java:8:20:8:30 | Annotated.m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
4-
| packagetwo/Source.java:9:28:9:47 | new AnnotatedClass(...) | Access of $@ annotated with VisibleForTesting found in production code. | packageone/AnnotatedClass.java:4:14:4:27 | AnnotatedClass | element |
18+
| packagetwo/Source.java:14:17:14:29 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
19+
| packagetwo/Source.java:20:28:20:47 | new AnnotatedClass(...) | Access of $@ annotated with VisibleForTesting found in production code. | packageone/AnnotatedClass.java:4:14:4:27 | AnnotatedClass | element |
20+
| packagetwo/Source.java:24:30:24:40 | Annotated.m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
21+
| packagetwo/Source.java:25:31:25:42 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
22+
| packagetwo/Source.java:26:31:26:42 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
23+
| packagetwo/Source.java:28:27:28:39 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
24+
| packagetwo/Source.java:29:28:29:46 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
25+
| packagetwo/Source.java:30:28:30:49 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |

java/ql/test/query-tests/VisibleForTestingAbuse/packageone/SourcePackage.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,31 @@
44

55
public class SourcePackage extends Annotated {
66
void f() {
7-
AnnotatedClass a = new AnnotatedClass(); // COMPLIANT - same package
7+
// Fields - cross-package access (only accessible ones)
8+
// String s = Annotated.m; // Cannot access package-private from different package
89
String s1 = Annotated.m1; // $ Alert
10+
String s2 = Annotated.m2; // $ Alert
11+
// String s3 = Annotated.m3; // Cannot access private field
12+
13+
// Methods - cross-package access (only accessible ones)
14+
// int i = Annotated.f(); // Cannot access package-private from different package
15+
// int i1 = Annotated.fPrivate(); // Cannot access private method
16+
int i2 = Annotated.fPublic(); // $ Alert
17+
int i3 = Annotated.fProtected(); // $ Alert
18+
19+
// Same package class
20+
AnnotatedClass a = new AnnotatedClass(); // COMPLIANT - same package
21+
22+
// Lambda usage - cross-package (only accessible members)
23+
Runnable lambda = () -> {
24+
// String lambdaS = Annotated.m; // Cannot access package-private
25+
String lambdaS1 = Annotated.m1; // $ Alert
26+
String lambdaS2 = Annotated.m2; // $ Alert
27+
28+
// int lambdaI = Annotated.f(); // Cannot access package-private
29+
int lambdaI2 = Annotated.fPublic(); // $ Alert
30+
int lambdaI3 = Annotated.fProtected(); // $ Alert
31+
};
32+
lambda.run();
933
}
1034
}

java/ql/test/query-tests/VisibleForTestingAbuse/packagetwo/Annotated.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,69 @@ public class Annotated {
77
static String m;
88
@VisibleForTesting
99
static protected String m1;
10+
@VisibleForTesting
11+
static public String m2;
12+
@VisibleForTesting
13+
static private String m3;
1014

1115
@VisibleForTesting
1216
static int f() {
1317
return 1;
1418
}
19+
20+
@VisibleForTesting
21+
static private int fPrivate() {
22+
return 1;
23+
}
24+
25+
@VisibleForTesting
26+
static public int fPublic() {
27+
return 1;
28+
}
29+
30+
@VisibleForTesting
31+
static protected int fProtected() {
32+
return 1;
33+
}
34+
35+
private static void resetPriorities() {
36+
String priority = m;
37+
String priority1 = m1;
38+
String priority2 = m2;
39+
String priority3 = m3;
40+
41+
int result = f();
42+
int resultPrivate = fPrivate();
43+
int resultPublic = fPublic();
44+
int resultProtected = fProtected();
45+
}
46+
47+
private static void resetPriorities2() {
48+
Runnable task = () -> {
49+
String priority = m; // $ SPURIOUS: Alert
50+
String priority1 = m1; // $ SPURIOUS: Alert
51+
String priority2 = m2; // $ SPURIOUS: Alert
52+
String priority3 = m3;
53+
54+
int result = f(); // $ SPURIOUS: Alert
55+
int resultPrivate = fPrivate();
56+
int resultPublic = fPublic(); // $ SPURIOUS: Alert
57+
int resultProtected = fProtected(); // $ SPURIOUS: Alert
58+
};
59+
task.run();
60+
}
61+
62+
private static class InnerClass {
63+
void useVisibleForMembers() {
64+
String field = m; // $ SPURIOUS: Alert
65+
String field1 = m1;
66+
String field2 = m2;
67+
String field3 = m3;
68+
69+
int method = f(); // $ SPURIOUS: Alert
70+
int methodPrivate = fPrivate();
71+
int methodPublic = fPublic();
72+
int methodProtected = fProtected();
73+
}
74+
}
1575
}

java/ql/test/query-tests/VisibleForTestingAbuse/packagetwo/Source.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,31 @@
44

55
public class Source {
66
void f() {
7-
int i = Annotated.f(); // $ Alert
7+
// Fields
88
String s = Annotated.m; // $ Alert
9-
AnnotatedClass a = new AnnotatedClass(); // $ Alert
109
String s1 = Annotated.m1; // COMPLIANT - same package
10+
String s2 = Annotated.m2;
11+
// String s3 = Annotated.m3; // Cannot access private field
12+
13+
// Methods
14+
int i = Annotated.f(); // $ Alert
15+
// int i1 = Annotated.fPrivate(); // Cannot access private method
16+
int i2 = Annotated.fPublic();
17+
int i3 = Annotated.fProtected();
18+
19+
// Other class
20+
AnnotatedClass a = new AnnotatedClass(); // $ Alert
21+
22+
// Lambda usage
23+
Runnable lambda = () -> {
24+
String lambdaS = Annotated.m; // $ Alert
25+
String lambdaS1 = Annotated.m1; // $ SPURIOUS: Alert
26+
String lambdaS2 = Annotated.m2; // $ SPURIOUS: Alert
27+
28+
int lambdaI = Annotated.f(); // $ Alert
29+
int lambdaI2 = Annotated.fPublic(); // $ SPURIOUS: Alert
30+
int lambdaI3 = Annotated.fProtected(); // $ SPURIOUS: Alert
31+
};
32+
lambda.run();
1133
}
1234
}

java/ql/test/query-tests/VisibleForTestingAbuse/packagetwo/Test.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,31 @@
44

55
public class Test {
66
void f() {
7-
int i = Annotated.f(); // COMPLIANT
7+
// Fields
88
String s = Annotated.m; // COMPLIANT
9-
AnnotatedClass a = new AnnotatedClass(); // COMPLIANT
109
String s1 = Annotated.m1; // COMPLIANT
10+
String s2 = Annotated.m2; // COMPLIANT
11+
// String s3 = Annotated.m3; // Cannot access private field
12+
13+
// Methods
14+
int i = Annotated.f(); // COMPLIANT
15+
// int i1 = Annotated.fPrivate(); // Cannot access private method
16+
int i2 = Annotated.fPublic(); // COMPLIANT
17+
int i3 = Annotated.fProtected(); // COMPLIANT
18+
19+
// Other class
20+
AnnotatedClass a = new AnnotatedClass(); // COMPLIANT
21+
22+
// Lambda usage
23+
Runnable lambda = () -> {
24+
String lambdaS = Annotated.m; // COMPLIANT
25+
String lambdaS1 = Annotated.m1; // COMPLIANT
26+
String lambdaS2 = Annotated.m2; // COMPLIANT
27+
28+
int lambdaI = Annotated.f(); // COMPLIANT
29+
int lambdaI2 = Annotated.fPublic(); // COMPLIANT
30+
int lambdaI3 = Annotated.fProtected(); // COMPLIANT
31+
};
32+
lambda.run();
1133
}
1234
}

0 commit comments

Comments
 (0)