Skip to content

Commit f6e06b3

Browse files
merge
1 parent d7800c4 commit f6e06b3

File tree

227 files changed

+20619
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+20619
-18
lines changed

utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import java.nio.file.Path
3939
import java.nio.file.Paths
4040
import java.time.LocalDateTime
4141
import java.time.temporal.ChronoUnit
42+
import org.utbot.engine.greyboxfuzzer.util.CustomClassLoader
4243

4344
private const val LONG_GENERATION_TIMEOUT = 1_200_000L
4445

@@ -143,8 +144,16 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
143144
protected fun getWorkingDirectory(classFqn: String): Path? {
144145
val classRelativePath = classFqnToPath(classFqn) + ".class"
145146
val classAbsoluteURL = classLoader.getResource(classRelativePath) ?: return null
146-
val classAbsolutePath = replaceSeparator(classAbsoluteURL.toPath().toString())
147-
.removeSuffix(classRelativePath)
147+
val classAbsolutePath =
148+
if (classAbsoluteURL.toURI().scheme == "jar") {
149+
replaceSeparator(classAbsoluteURL.file.removePrefix("file:"))
150+
.removeSuffix(classRelativePath)
151+
.removeSuffix("/")
152+
.removeSuffix("!")
153+
} else {
154+
replaceSeparator(classAbsoluteURL.toPath().toString())
155+
.removeSuffix(classRelativePath)
156+
}
148157
return Paths.get(classAbsolutePath)
149158
}
150159

@@ -154,15 +163,17 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
154163
sourceCodeFile: Path? = null,
155164
searchDirectory: Path,
156165
chosenClassesToMockAlways: Set<ClassId>
157-
): List<UtMethodTestSet> =
158-
testCaseGenerator.generate(
166+
): List<UtMethodTestSet> {
167+
CustomClassLoader.classLoader = classLoader
168+
return testCaseGenerator.generate(
159169
targetMethods,
160170
mockStrategy,
161171
chosenClassesToMockAlways,
162172
generationTimeout
163173
).map {
164174
if (sourceCodeFile != null) it.summarize(sourceCodeFile.toFile(), searchDirectory) else it
165175
}
176+
}
166177

167178

168179
protected fun withLogger(targetClassFqn: String, block: Runnable) {

utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ object FileUtil {
9090

9191
for (clazz in classes) {
9292
val path = clazz.toClassFilePath()
93-
val resource = clazz.classLoader.getResource(path) ?: error("No such file: $path")
93+
val resource =
94+
clazz.classLoader.getResource(path)
95+
?: ClassLoader.getSystemClassLoader().getResource(path)
96+
?: error("No such file: $path")
9497

9598
if (resource.toURI().scheme == "jar") {
9699
val jarLocation = resource.toURI().extractJarName()

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,12 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
239239
/**
240240
* Set to true to start fuzzing if symbolic execution haven't return anything
241241
*/
242-
var useFuzzing: Boolean by getBooleanProperty(true)
242+
var useFuzzing: Boolean by getBooleanProperty(false)
243+
244+
/**
245+
* Set to true to start grey-box fuzzing
246+
*/
247+
var useGreyBoxFuzzing: Boolean by getBooleanProperty(true)
243248

244249
/**
245250
* Set the total attempts to improve coverage by fuzzer.

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,13 @@ data class UtArrayModel(
480480
* @param instantiationCall is an [UtExecutableCallModel] to instantiate represented object. It **must** not return `null`.
481481
* @param modificationsChain is a chain of [UtStatementModel] to construct object state.
482482
*/
483-
data class UtAssembleModel private constructor(
483+
data class UtAssembleModel constructor(
484484
override val id: Int?,
485485
override val classId: ClassId,
486486
override val modelName: String,
487487
val instantiationCall: UtExecutableCallModel,
488-
val modificationsChain: List<UtStatementModel>,
488+
//TODO: get rid of var
489+
var modificationsChain: List<UtStatementModel>,
489490
val origin: UtCompositeModel?
490491
) : UtReferenceModel(id, classId, modelName) {
491492

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/UtExecutionResult.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.framework.plugin.api
22

3+
import org.utbot.framework.plugin.api.util.objectClassId
34
import org.utbot.framework.plugin.api.visible.UtStreamConsumingException
45
import java.io.File
56
import java.util.LinkedList
@@ -21,6 +22,10 @@ sealed class UtExecutionFailure : UtExecutionResult() {
2122
get() = exception
2223
}
2324

25+
data class UtExecutionSuccessConcrete(val result: Any?) : UtExecutionResult() {
26+
override fun toString() = "$result"
27+
}
28+
2429
data class UtOverflowFailure(
2530
override val exception: Throwable,
2631
) : UtExecutionFailure()
@@ -102,7 +107,14 @@ inline fun UtExecutionResult.onFailure(action: (exception: Throwable) -> Unit):
102107
return this
103108
}
104109

110+
fun UtExecutionResult.getOrThrow(): UtModel = when (this) {
111+
is UtExecutionSuccess -> model
112+
is UtExecutionFailure -> throw exception
113+
is UtExecutionSuccessConcrete -> UtNullModel(objectClassId)
114+
}
115+
105116
fun UtExecutionResult.exceptionOrNull(): Throwable? = when (this) {
106117
is UtExecutionFailure -> rootCauseException
107118
is UtExecutionSuccess -> null
119+
is UtExecutionSuccessConcrete -> null
108120
}

utbot-framework/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ dependencies {
3434
implementation group: 'io.github.microutils', name: 'kotlin-logging', version: kotlinLoggingVersion
3535
implementation group: 'org.jacoco', name: 'org.jacoco.report', version: jacocoVersion
3636
implementation group: 'org.apache.commons', name: 'commons-text', version: apacheCommonsTextVersion
37+
implementation "org.javaruntype:javaruntype:1.3"
38+
implementation "ru.vyarus:generics-resolver:3.0.3"
39+
implementation "ognl:ognl:3.3.2"
40+
3741
// we need this for construction mocks from composite models
3842
implementation group: 'org.mockito', name: 'mockito-core', version: '4.2.0'
3943

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2010-2021 Paul R. Holser, Jr.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.utbot.quickcheck;
27+
28+
import org.utbot.quickcheck.Produced;
29+
import org.utbot.quickcheck.Property;
30+
import org.utbot.quickcheck.generator.Generator;
31+
32+
import java.lang.annotation.Repeatable;
33+
import java.lang.annotation.Retention;
34+
import java.lang.annotation.Target;
35+
36+
import static java.lang.annotation.ElementType.*;
37+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
38+
39+
/**
40+
* <p>Mark a parameter of a {@link Property} method with this annotation to
41+
* have random values supplied to it via the specified
42+
* {@link Generator}.</p>
43+
*
44+
* <p>You may specify as many of these annotations as as you wish on a given
45+
* parameter. On a given generation, one of the specified generators will be
46+
* chosen at random with probability in proportion to {@link #frequency()}.</p>
47+
*
48+
* <p>If any such generator produces values of a type incompatible with the
49+
* type of the marked parameter, {@link IllegalArgumentException} is
50+
* raised.</p>
51+
*/
52+
@Target({ PARAMETER, FIELD, ANNOTATION_TYPE, TYPE_USE })
53+
@Retention(RUNTIME)
54+
@Repeatable(Produced.class)
55+
public @interface From {
56+
/**
57+
* @return the generator to be used for the annotated property parameter
58+
*/
59+
Class<? extends Generator> value();
60+
61+
/**
62+
* @return a weight to influence how often the generator is chosen
63+
*/
64+
int frequency() default 1;
65+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2010-2021 Paul R. Holser, Jr.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.utbot.quickcheck;
27+
28+
import org.utbot.quickcheck.Property;
29+
30+
/**
31+
* <p>Allows access to an actual failing example.</p>
32+
*
33+
* <p>This may be useful if any of the objects' {@link Object#toString()}
34+
* representations is difficult to understand.</p>
35+
*
36+
* @see Property#onMinimalCounterexample()
37+
*/
38+
public interface MinimalCounterexampleHook {
39+
/**
40+
* @param counterexample the minimal counterexample (after shrinking)
41+
* @param action work to perform with the minimal counterexample;
42+
* for example, this could repeat the test using the same inputs.
43+
* This action should be safely callable multiple times.
44+
*/
45+
void handle(Object[] counterexample, Runnable action);
46+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2010-2021 Paul R. Holser, Jr.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.utbot.quickcheck;
27+
28+
import org.utbot.quickcheck.Property;
29+
import org.utbot.quickcheck.internal.ParameterSampler;
30+
import org.utbot.quickcheck.internal.sampling.ExhaustiveParameterSampler;
31+
import org.utbot.quickcheck.internal.sampling.TupleParameterSampler;
32+
33+
/**
34+
* Represents different modes of execution of property-based tests.
35+
*
36+
* @see org.utbot.quickcheck.generator.Only
37+
* @see org.utbot.quickcheck.generator.Also
38+
*/
39+
public enum Mode {
40+
/**
41+
* Verify {@link org.utbot.quickcheck.Property#trials()} tuples of arguments for a property's
42+
* parameters.
43+
*/
44+
SAMPLING {
45+
@Override ParameterSampler sampler(int defaultSampleSize) {
46+
return new TupleParameterSampler(defaultSampleSize);
47+
}
48+
},
49+
50+
/**
51+
* Generate {@link Property#trials()} arguments for each parameter
52+
* property, and verify the cross-products of those sets of arguments.
53+
* This behavior mirrors that of the JUnit
54+
* {@link org.junit.experimental.theories.Theories} runner.
55+
*/
56+
EXHAUSTIVE {
57+
@Override ParameterSampler sampler(int defaultSampleSize) {
58+
return new ExhaustiveParameterSampler(defaultSampleSize);
59+
}
60+
};
61+
62+
abstract ParameterSampler sampler(int defaultSampleSize);
63+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2017-2018 The Regents of the University of California
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are
8+
* met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*/
29+
package org.utbot.quickcheck;
30+
31+
import org.utbot.quickcheck.generator.GenerationStatus;
32+
import org.utbot.quickcheck.internal.GeometricDistribution;
33+
import org.utbot.quickcheck.random.SourceOfRandomness;
34+
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
import java.util.Optional;
38+
39+
40+
/**
41+
* Provides a generation status that does not track the number of trials
42+
* generated so far. This is useful for guided fuzzing where the burden
43+
* of making choices is on the guidance system rather than on quickcheck.
44+
*
45+
* @author Rohan Padhye
46+
*/
47+
public class NonTrackingGenerationStatus implements GenerationStatus {
48+
49+
public static final int MEAN_SIZE = 10;
50+
51+
private final SourceOfRandomness random;
52+
private final Map<Key<?>, Object> contextValues = new HashMap<>();
53+
private final GeometricDistribution geometric = new GeometricDistribution();
54+
55+
56+
public NonTrackingGenerationStatus(SourceOfRandomness random) {
57+
this.random = random;
58+
}
59+
60+
@Override
61+
public int size() {
62+
return geometric.sampleWithMean(MEAN_SIZE, random);
63+
}
64+
65+
@Override
66+
public int attempts() {
67+
throw new UnsupportedOperationException("attempts() and @ValueOf" +
68+
" is not supported in guided mode.");
69+
}
70+
71+
@Override
72+
public <T> GenerationStatus setValue(Key<T> key, T value) {
73+
contextValues.put(key, value);
74+
return this;
75+
}
76+
77+
@Override
78+
public <T> Optional<T> valueOf(Key<T> key) {
79+
return Optional.ofNullable(key.cast(contextValues.get(key)));
80+
}
81+
}

0 commit comments

Comments
 (0)