Skip to content

Commit 04ffe80

Browse files
Add unit tests
1 parent 153e0c4 commit 04ffe80

File tree

9 files changed

+473
-7
lines changed

9 files changed

+473
-7
lines changed

java/ql/src/semmle/code/java/frameworks/guava/Base.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private class GuavaBaseCsv extends SummaryModelCsv {
5050
"com.google.common.base;Ascii;false;toUpperCase;(String);;Argument[0];ReturnValue;taint",
5151
"com.google.common.base;Ascii;false;truncate;(CharSequence,int,String);;Argument[0];ReturnValue;taint",
5252
"com.google.common.base;Ascii;false;truncate;(CharSequence,int,String);;Argument[2];ReturnValue;taint",
53-
"com.google.common.base;CaseFormat;true;to;(CharFormat,String);;Argument[1];ReturnValue;taint",
53+
"com.google.common.base;CaseFormat;true;to;(CaseFormat,String);;Argument[1];ReturnValue;taint",
5454
"com.google.common.base;Converter;true;apply;;;Argument[0];ReturnValue;taint",
5555
"com.google.common.base;Converter;true;convert;;;Argument[0];ReturnValue;taint",
5656
"com.google.common.base;Converter;true;convertAll;;;Argument[0];ReturnValue;taint",
@@ -65,6 +65,8 @@ private class GuavaBaseCsv extends SummaryModelCsv {
6565
"com.google.common.base;Optional;true;asSet;();;Argument[-1];ReturnValue;taint",
6666
"com.google.common.base;Optional;true;of;(T);;Argument[0];ReturnValue;taint",
6767
"com.google.common.base;Optional;true;or;;;Argument[-1];ReturnValue;taint",
68+
"com.google.common.base;Optional;true;or;;;Argument[0];ReturnValue;taint",
69+
"com.google.common.base;Optional;true;orNull;();;Argument[-1];ReturnValue;taint",
6870
"com.google.common.base;Optional;true;presentInstances;;;Argument[0];ReturnValue;taint",
6971
"com.google.common.base;Optional;true;toJavaUtil;();;Argument[-1];ReturnValue;taint",
7072
"com.google.common.base;Optional;true;toJavaUtil;(Optional<T>);;Argument[0];ReturnValue;taint",

java/ql/test/library-tests/frameworks/guava/TestBase.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44
import java.util.HashMap;
5+
import java.util.concurrent.TimeUnit;
56

67
class TestBase {
78
String taint() { return "tainted"; }
@@ -59,6 +60,50 @@ void test3() {
5960

6061
void test4() {
6162
sink(Preconditions.checkNotNull(taint())); // $numTaintFlow=1
63+
sink(Verify.verifyNotNull(taint())); // $numTaintFlow=1
64+
}
65+
66+
void test5() {
67+
sink(Ascii.toLowerCase(taint())); // $numTaintFlow=1
68+
sink(Ascii.toUpperCase(taint())); // $numTaintFlow=1
69+
sink(Ascii.truncate(taint(), 3, "...")); // $numTaintFlow=1
70+
sink(Ascii.truncate("abcabcabc", 3, taint())); // $numTaintFlow=1
71+
sink(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, taint())); // $numTaintFlow=1
72+
sink(CaseFormat.LOWER_HYPHEN.converterTo(CaseFormat.UPPER_CAMEL).convert(taint())); // $numTaintFlow=1
73+
sink(CaseFormat.LOWER_UNDERSCORE.converterTo(CaseFormat.LOWER_HYPHEN).reverse().convert(taint())); // $numTaintFlow=1
74+
}
75+
76+
void test6() {
77+
sink(Suppliers.memoize(Suppliers.memoizeWithExpiration(Suppliers.synchronizedSupplier(Suppliers.ofInstance(taint())), 3, TimeUnit.HOURS))); // $numTaintFlow=1
78+
}
79+
80+
void test7() {
81+
sink(MoreObjects.firstNonNull(taint(), "abc")); // $numTaintFlow=1
82+
sink(MoreObjects.firstNonNull(null, taint())); // $numTaintFlow=1
83+
sink(MoreObjects.toStringHelper(taint()).add("x", 3).omitNullValues().toString()); // $numTaintFlow=1
84+
sink(MoreObjects.toStringHelper((Object) taint()).toString());
85+
sink(MoreObjects.toStringHelper("a").add("x", 3).add(taint(), 4).toString()); // $numTaintFlow=1
86+
sink(MoreObjects.toStringHelper("a").add("x", taint()).toString()); // $numTaintFlow=1
87+
sink(MoreObjects.toStringHelper("a").addValue(taint()).toString()); // $numTaintFlow=1
88+
MoreObjects.ToStringHelper h = MoreObjects.toStringHelper("a");
89+
h.add("x", 3).add(taint(), 4);
90+
sink(h.add("z",5).toString()); // $numTaintFlow=1
91+
}
92+
93+
void test8() {
94+
Optional<String> x = Optional.of(taint());
95+
sink(x); // $numTaintFlow=1
96+
sink(x.get()); // $numTaintFlow=1
97+
sink(x.or("hi")); // $numTaintFlow=1
98+
sink(x.orNull()); // $numTaintFlow=1
99+
sink(x.asSet()); // $numTaintFlow=1
100+
sink(Optional.fromJavaUtil(x.toJavaUtil())); // $numTaintFlow=1
101+
sink(Optional.fromJavaUtil(Optional.toJavaUtil(x))); // $numTaintFlow=1
102+
sink(x.asSet()); // $numTaintFlow=1
103+
sink(Optional.fromNullable(taint())); // $numTaintFlow=1
104+
sink(Optional.absent().or(x)); // $numTaintFlow=1
105+
sink(Optional.absent().or(taint())); // $numTaintFlow=1
106+
sink(Optional.presentInstances(Optional.of(x).asSet())); // $numTaintFlow=1
62107
}
63108

64109
void test5() {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2010 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.base;
16+
17+
public final class Ascii {
18+
public static String toLowerCase(String string) {
19+
return null;
20+
}
21+
22+
public static String toLowerCase(CharSequence chars) {
23+
return null;
24+
}
25+
26+
public static char toLowerCase(char c) {
27+
return '0';
28+
}
29+
30+
public static String toUpperCase(String string) {
31+
return null;
32+
}
33+
34+
public static String toUpperCase(CharSequence chars) {
35+
return null;
36+
}
37+
38+
public static char toUpperCase(char c) {
39+
return '0';
40+
}
41+
42+
public static boolean isLowerCase(char c) {
43+
return false;
44+
}
45+
46+
public static boolean isUpperCase(char c) {
47+
return false;
48+
}
49+
50+
public static String truncate(CharSequence seq, int maxLength, String truncationIndicator) {
51+
return null;
52+
}
53+
54+
public static boolean equalsIgnoreCase(CharSequence s1, CharSequence s2) {
55+
return false;
56+
}
57+
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2006 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.base;
16+
17+
public enum CaseFormat {
18+
LOWER_HYPHEN,
19+
LOWER_UNDERSCORE,
20+
LOWER_CAMEL,
21+
UPPER_CAMEL,
22+
UPPER_UNDERSCORE;
23+
24+
public final String to(CaseFormat format, String str) {
25+
return null;
26+
}
27+
28+
public Converter<String, String> converterTo(CaseFormat targetFormat) {
29+
return null;
30+
}
31+
32+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2008 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.base;
16+
import org.checkerframework.checker.nullness.qual.Nullable;
17+
18+
public abstract class Converter<A, B> implements Function<A, B> {
19+
public final @Nullable B convert(@Nullable A a) {
20+
return null;
21+
}
22+
23+
public Iterable<B> convertAll(final Iterable<? extends A> fromIterable) {
24+
return null;
25+
}
26+
27+
public Converter<B, A> reverse() {
28+
return null;
29+
}
30+
31+
public final <C> Converter<A, C> andThen(Converter<B, C> secondConverter) {
32+
return null;
33+
}
34+
35+
@Override
36+
public final @Nullable B apply(@Nullable A a) {
37+
return null;
38+
}
39+
40+
@Override
41+
public boolean equals(@Nullable Object object) {
42+
return false;
43+
}
44+
45+
public static <A, B> Converter<A, B> from(
46+
Function<? super A, ? extends B> forwardFunction,
47+
Function<? super B, ? extends A> backwardFunction) {
48+
return null;
49+
}
50+
51+
public static <T> Converter<T, T> identity() {
52+
return null;
53+
}
54+
55+
}
Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,102 @@
1-
package com.google.common.base;
1+
/*
2+
* Copyright (C) 2014 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
214

15+
package com.google.common.base;
316
import org.checkerframework.checker.nullness.qual.Nullable;
417

518
public final class MoreObjects {
6-
public static <T> T firstNonNull(@Nullable T first, @Nullable T second) {
7-
return null;
19+
public static <T> T firstNonNull(@Nullable T first, @Nullable T second) {
20+
return null;
21+
}
22+
23+
public static ToStringHelper toStringHelper(Object self) {
24+
return null;
25+
}
26+
27+
public static ToStringHelper toStringHelper(Class<?> clazz) {
28+
return null;
29+
}
30+
31+
public static ToStringHelper toStringHelper(String className) {
32+
return null;
33+
}
34+
35+
public static final class ToStringHelper {
36+
public ToStringHelper omitNullValues() {
37+
return null;
38+
}
39+
40+
public ToStringHelper add(String name, @Nullable Object value) {
41+
return null;
42+
}
43+
44+
public ToStringHelper add(String name, boolean value) {
45+
return null;
846
}
9-
}
47+
48+
public ToStringHelper add(String name, char value) {
49+
return null;
50+
}
51+
52+
public ToStringHelper add(String name, double value) {
53+
return null;
54+
}
55+
56+
public ToStringHelper add(String name, float value) {
57+
return null;
58+
}
59+
60+
public ToStringHelper add(String name, int value) {
61+
return null;
62+
}
63+
64+
public ToStringHelper add(String name, long value) {
65+
return null;
66+
}
67+
68+
public ToStringHelper addValue(@Nullable Object value) {
69+
return null;
70+
}
71+
72+
public ToStringHelper addValue(boolean value) {
73+
return null;
74+
}
75+
76+
public ToStringHelper addValue(char value) {
77+
return null;
78+
}
79+
80+
public ToStringHelper addValue(double value) {
81+
return null;
82+
}
83+
84+
public ToStringHelper addValue(float value) {
85+
return null;
86+
}
87+
88+
public ToStringHelper addValue(int value) {
89+
return null;
90+
}
91+
92+
public ToStringHelper addValue(long value) {
93+
return null;
94+
}
95+
96+
@Override
97+
public String toString() {
98+
return null;
99+
}
100+
101+
}
102+
}

java/ql/test/stubs/guava-30.0/com/google/common/base/Optional.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public static <T> Optional<T> fromNullable(@Nullable T nullableReference) {
3131
}
3232

3333
public static <T> @Nullable Optional<T> fromJavaUtil(
34-
java.util.Optional<T> javaUtilOptional) {
34+
java.util.@Nullable Optional<T> javaUtilOptional) {
3535
return null;
3636
}
3737

38-
public static <T> java.util.Optional<T> toJavaUtil(
38+
public static <T> java.util.@Nullable Optional<T> toJavaUtil(
3939
@Nullable Optional<T> googleOptional) {
4040
return null;
4141
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2007 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.base;
16+
import java.util.concurrent.TimeUnit;
17+
import org.checkerframework.checker.nullness.qual.Nullable;
18+
19+
public final class Suppliers {
20+
public static <F, T> Supplier<T> compose(Function<? super F, T> function, Supplier<F> supplier) {
21+
return null;
22+
}
23+
24+
public static <T> Supplier<T> memoize(Supplier<T> delegate) {
25+
return null;
26+
}
27+
28+
public static <T> Supplier<T> memoizeWithExpiration(
29+
Supplier<T> delegate, long duration, TimeUnit unit) {
30+
return null;
31+
}
32+
33+
public static <T> Supplier<T> ofInstance(@Nullable T instance) {
34+
return null;
35+
}
36+
37+
public static <T> Supplier<T> synchronizedSupplier(Supplier<T> delegate) {
38+
return null;
39+
}
40+
41+
public static <T> Function<Supplier<T>, T> supplierFunction() {
42+
return null;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)