Skip to content

Commit fc01f38

Browse files
committed
Tests for taint analysis
1 parent f5a6b21 commit fc01f38

29 files changed

+982
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.utbot.examples.taint
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.testcheckers.eq
5+
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
6+
7+
internal class AliasExamplesTest : UtValueTestCaseChecker(
8+
testClass = AliasExamplesTest::class
9+
) {
10+
@Test
11+
fun testBad550() {
12+
check(
13+
AliasExamples::bad550,
14+
eq(-1),
15+
)
16+
}
17+
18+
@Test
19+
fun testParamDependentGood() {
20+
check(
21+
AliasExamples::paramDependentGood,
22+
eq(-1),
23+
)
24+
}
25+
26+
@Test
27+
fun testPassSecondParamBad() {
28+
check(
29+
AliasExamples::passSecondParamBad,
30+
eq(-1),
31+
)
32+
}
33+
34+
@Test
35+
fun testPassSecondParamGood() {
36+
check(
37+
AliasExamples::passSecondParamGood,
38+
eq(-1),
39+
)
40+
}
41+
42+
@Test
43+
fun testPassFirstParamGood() {
44+
check(
45+
AliasExamples::passFirstParamGood,
46+
eq(-1),
47+
)
48+
}
49+
50+
@Test
51+
fun testPassFirstParamBad() {
52+
check(
53+
AliasExamples::passFirstParamBad,
54+
eq(-1),
55+
)
56+
}
57+
58+
59+
@Test
60+
fun testParamDependentBad() {
61+
check(
62+
AliasExamples::paramDependentBad,
63+
eq(-1),
64+
)
65+
}
66+
67+
@Test
68+
fun testClearSecondParameter() {
69+
check(
70+
AliasExamples::clearSecondParameter,
71+
eq(-1),
72+
)
73+
}
74+
75+
76+
@Test
77+
fun testBad551() {
78+
check(
79+
AliasExamples::bad551,
80+
eq(-1),
81+
)
82+
}
83+
84+
@Test
85+
fun testBad552() {
86+
check(
87+
AliasExamples::bad552,
88+
eq(-1),
89+
)
90+
}
91+
92+
@Test
93+
fun testBad553() {
94+
check(
95+
AliasExamples::bad553,
96+
eq(-1),
97+
)
98+
}
99+
100+
@Test
101+
fun testBad554() {
102+
check(
103+
AliasExamples::bad554,
104+
eq(-1),
105+
)
106+
}
107+
108+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.utbot.examples.taint
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.testcheckers.eq
5+
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
6+
7+
internal class CollectionExamplesTest : UtValueTestCaseChecker(
8+
testClass = CollectionsExamples::class
9+
) {
10+
@Test
11+
fun testSinkWithList() {
12+
check(
13+
CollectionsExamples::sinkWithList,
14+
eq(-1),
15+
)
16+
}
17+
18+
@Test
19+
fun testPassThroughExample() {
20+
check(
21+
CollectionsExamples::passThroughExample,
22+
eq(-1),
23+
)
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.utbot.examples.taint.alias
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.engine.taint.TaintAnalysisError
5+
import org.utbot.framework.plugin.api.CodegenLanguage
6+
import org.utbot.tests.infrastructure.AtLeast
7+
import org.utbot.tests.infrastructure.Compilation
8+
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
9+
import org.utbot.tests.infrastructure.ignoreExecutionsNumber
10+
import org.utbot.tests.infrastructure.isException
11+
12+
class Injection550Test : UtValueTestCaseChecker(
13+
testClass = CWE_89_SQL_Injection_console__env_execute_550::class,
14+
testCodeGeneration = true,
15+
pipelines = listOf(
16+
TestLastStage(CodegenLanguage.JAVA, Compilation)
17+
)
18+
) {
19+
@Test
20+
fun testBad() {
21+
checkWithException(
22+
CWE_89_SQL_Injection_console__env_execute_550::bad,
23+
ignoreExecutionsNumber,
24+
{ r -> r.isException<TaintAnalysisError>() },
25+
coverage = AtLeast(95)
26+
)
27+
}
28+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package org.utbot.examples.taint;
2+
3+
import org.utbot.examples.taint.aliases.AliasA;
4+
import org.utbot.examples.taint.aliases.AliasB;
5+
6+
import static org.utbot.examples.taint.BadSink.writeIntoBd;
7+
8+
public class AliasExamples {
9+
10+
public void bad550() {
11+
AliasA a = new AliasA();
12+
AliasA b = new AliasA();
13+
b.getF().setData(BadSource.getEnvironment("data"));
14+
15+
a.f = b.f;
16+
17+
writeIntoBd(a.f.getData());
18+
}
19+
20+
public void paramDependentBad() {
21+
AliasA a = new AliasA();
22+
AliasA b = new AliasA();
23+
b.getF().setData(BadSource.getEnvironment("data"));
24+
25+
a.f = b.f;
26+
27+
BadSink.onlySecondParamIsImportant("safe", a.f.getData());
28+
}
29+
30+
public void clearSecondParameter() {
31+
AliasA a = new AliasA();
32+
AliasA b = new AliasA();
33+
b.getF().setData(BadSource.getEnvironment("data"));
34+
35+
a.f = b.f;
36+
37+
BadSink.onlySecondParamIsImportant("safe", TaintCleaner.removeTaintMark(a.f.getData()));
38+
}
39+
40+
public void paramDependentGood() {
41+
AliasA a = new AliasA();
42+
AliasA b = new AliasA();
43+
b.getF().setData(BadSource.getEnvironment("data"));
44+
45+
a.f = b.f;
46+
47+
BadSink.onlySecondParamIsImportant(a.f.getData(), "safe");
48+
}
49+
50+
public void passSecondParamGood() {
51+
AliasA a = new AliasA();
52+
AliasA b = new AliasA();
53+
b.getF().setData(BadSource.getEnvironment("data"));
54+
55+
a.f = b.f;
56+
57+
String param = a.f.getData();
58+
TaintPassThrough taintPassThrough = new TaintPassThrough();
59+
60+
BadSink.writeIntoBd(taintPassThrough.passSecondParameter(param, ""));
61+
}
62+
63+
public void passSecondParamBad() {
64+
AliasA a = new AliasA();
65+
AliasA b = new AliasA();
66+
b.getF().setData(BadSource.getEnvironment("data"));
67+
68+
a.f = b.f;
69+
70+
String param = a.f.getData();
71+
TaintPassThrough taintPassThrough = new TaintPassThrough();
72+
73+
BadSink.writeIntoBd(taintPassThrough.passSecondParameter("", param));
74+
}
75+
76+
public void passFirstParamGood() {
77+
AliasA a = new AliasA();
78+
AliasA b = new AliasA();
79+
b.getF().setData(BadSource.getEnvironment("data"));
80+
81+
a.f = b.f;
82+
83+
String param = a.f.getData();
84+
TaintPassThrough taintPassThrough = new TaintPassThrough();
85+
86+
BadSink.writeIntoBd(taintPassThrough.passFirstParameter("", param));
87+
}
88+
89+
public void passFirstParamBad() {
90+
AliasA a = new AliasA();
91+
AliasA b = new AliasA();
92+
b.getF().setData(BadSource.getEnvironment("data"));
93+
94+
a.f = b.f;
95+
96+
String param = a.f.getData();
97+
TaintPassThrough taintPassThrough = new TaintPassThrough();
98+
99+
BadSink.writeIntoBd(taintPassThrough.passFirstParameter(param, ""));
100+
}
101+
102+
public void passSecondParam() {
103+
AliasA a = new AliasA();
104+
AliasA b = new AliasA();
105+
b.getF().setData(BadSource.getEnvironment("data"));
106+
107+
a.f = b.f;
108+
109+
BadSink.onlySecondParamIsImportant(a.f.getData(), "safe");
110+
}
111+
112+
public void bad551() {
113+
AliasA a = new AliasA();
114+
a.getF().setData(BadSource.getEnvironment("data"));
115+
116+
AliasA b = a;
117+
118+
writeIntoBd(b.getF().getData());
119+
}
120+
121+
public void bad552() {
122+
AliasB b = new AliasB();
123+
b.setData(BadSource.getEnvironment("data"));
124+
AliasA a = new AliasA(b);
125+
AliasA c = new AliasA();
126+
assign(a, c);
127+
AliasB d = c.f;
128+
129+
writeIntoBd(d.getData());
130+
}
131+
132+
private static void assign(AliasA x, AliasA y) {
133+
y.f = x.f;
134+
}
135+
136+
public void bad553() {
137+
AliasB b = new AliasB();
138+
b.setData(BadSource.getEnvironment("data"));
139+
AliasA a = new AliasA(b);
140+
AliasA c = new AliasA();
141+
assign(a, c);
142+
AliasB d = c.f;
143+
144+
writeIntoBd(d.getData());
145+
}
146+
147+
public void bad554() {
148+
AliasA a = new AliasA();
149+
a.setData(BadSource.getEnvironment("data"));
150+
AliasA b = new AliasA();
151+
152+
b = a;
153+
154+
writeIntoBd(b.getData());
155+
}
156+
157+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.utbot.examples.taint;
2+
3+
public class BadSink {
4+
private static String value;
5+
6+
public static void writeIntoBd(String param) {
7+
value = param;
8+
}
9+
10+
public static void onlySecondParamIsImportant(String fst, String snd) {
11+
System.out.println(fst);
12+
value = snd;
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.utbot.examples.taint;
2+
3+
public class BadSource {
4+
public static String getEnvironment(String param) {
5+
return "unsafe " + param;
6+
}
7+
}

0 commit comments

Comments
 (0)