-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disabled NPE checks for non-public library fields by default (#353)
- Loading branch information
Showing
9 changed files
with
219 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
utbot-framework-api/src/main/kotlin/org/utbot/framework/TrustedLibraries.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.utbot.framework | ||
|
||
import mu.KotlinLogging | ||
import org.utbot.common.PathUtil.toPath | ||
import java.io.IOException | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
private val defaultUserTrustedLibrariesPath: String = "${utbotHomePath}/trustedLibraries.txt" | ||
private const val userTrustedLibrariesKey: String = "utbot.settings.trusted.libraries.path" | ||
|
||
object TrustedLibraries { | ||
/** | ||
* Always "trust" JDK. | ||
*/ | ||
private val defaultTrustedLibraries: List<String> = listOf( | ||
"java", | ||
"sun", | ||
"javax", | ||
"com.sun", | ||
"org.omg", | ||
"org.xml", | ||
"org.w3c.dom", | ||
) | ||
|
||
private val userTrustedLibraries: List<String> | ||
get() { | ||
val userTrustedLibrariesPath = System.getProperty(userTrustedLibrariesKey) ?: defaultUserTrustedLibrariesPath | ||
val userTrustedLibrariesFile = userTrustedLibrariesPath.toPath().toFile() | ||
|
||
if (!userTrustedLibrariesFile.exists()) { | ||
return emptyList() | ||
} | ||
|
||
return try { | ||
userTrustedLibrariesFile.readLines() | ||
} catch (e: IOException) { | ||
logger.info { e.message } | ||
|
||
emptyList() | ||
} | ||
} | ||
|
||
/** | ||
* Represents prefixes of packages for trusted libraries - | ||
* as the union of [defaultTrustedLibraries] and [userTrustedLibraries]. | ||
*/ | ||
val trustedLibraries: Set<String> by lazy { (defaultTrustedLibraries + userTrustedLibraries).toSet() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
utbot-framework/src/main/kotlin/org/utbot/engine/util/trusted/TrustedPackages.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.utbot.engine.util.trusted | ||
|
||
import org.utbot.framework.TrustedLibraries | ||
import soot.SootClass | ||
|
||
/** | ||
* Cache for already discovered trusted/untrusted packages. | ||
*/ | ||
private val isPackageTrusted: MutableMap<String, Boolean> = mutableMapOf() | ||
|
||
/** | ||
* Determines whether [this] class is from trusted libraries as defined in [TrustedLibraries]. | ||
*/ | ||
fun SootClass.isFromTrustedLibrary(): Boolean { | ||
isPackageTrusted[packageName]?.let { | ||
return it | ||
} | ||
|
||
val isTrusted = TrustedLibraries.trustedLibraries.any { packageName.startsWith(it, ignoreCase = false) } | ||
|
||
return isTrusted.also { isPackageTrusted[packageName] = it } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
utbot-framework/src/test/kotlin/org/utbot/examples/stdlib/DateExampleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.utbot.examples.stdlib | ||
|
||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
import org.utbot.examples.AbstractTestCaseGeneratorTest | ||
import org.utbot.examples.eq | ||
import org.utbot.examples.isException | ||
import org.utbot.examples.withUsingReflectionForMaximizingCoverage | ||
import java.util.Date | ||
|
||
class DateExampleTest : AbstractTestCaseGeneratorTest(testClass = DateExample::class) { | ||
@Suppress("SpellCheckingInspection") | ||
@Tag("slow") | ||
@Test | ||
fun testGetTimeWithNpeChecksForNonPublicFields() { | ||
withUsingReflectionForMaximizingCoverage(maximizeCoverage = true) { | ||
checkWithException( | ||
DateExample::getTime, | ||
eq(5), | ||
*commonMatchers, | ||
{ date: Date?, r: Result<Boolean> -> | ||
val cdate = date!!.getDeclaredFieldValue("cdate") | ||
val calendarDate = cdate!!.getDeclaredFieldValue("date") | ||
|
||
calendarDate == null && r.isException<NullPointerException>() | ||
}, | ||
{ date: Date?, r: Result<Boolean> -> | ||
val cdate = date!!.getDeclaredFieldValue("cdate") | ||
val calendarDate = cdate!!.getDeclaredFieldValue("date") | ||
|
||
val gcal = date.getDeclaredFieldValue("gcal") | ||
|
||
val normalized = calendarDate!!.getDeclaredFieldValue("normalized") as Boolean | ||
val gregorianYear = calendarDate.getDeclaredFieldValue("gregorianYear") as Int | ||
|
||
gcal == null && !normalized && gregorianYear >= 1582 && r.isException<NullPointerException>() | ||
} | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetTimeWithoutReflection() { | ||
withUsingReflectionForMaximizingCoverage(maximizeCoverage = false) { | ||
checkWithException( | ||
DateExample::getTime, | ||
eq(3), | ||
*commonMatchers | ||
) | ||
} | ||
} | ||
|
||
private val commonMatchers = arrayOf( | ||
{ date: Date?, r: Result<Boolean> -> date == null && r.isException<NullPointerException>() }, | ||
{ date: Date?, r: Result<Boolean> -> date != null && date.time == 100L && r.getOrThrow() }, | ||
{ date: Date?, r: Result<Boolean> -> date != null && date.time != 100L && !r.getOrThrow() } | ||
) | ||
|
||
private fun Any.getDeclaredFieldValue(fieldName: String): Any? { | ||
val declaredField = javaClass.getDeclaredField(fieldName) | ||
declaredField.isAccessible = true | ||
|
||
return declaredField.get(this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
utbot-sample/src/main/java/org/utbot/examples/stdlib/DateExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.utbot.examples.stdlib; | ||
|
||
import java.util.Date; | ||
|
||
public class DateExample { | ||
public boolean getTime(Date date) { | ||
return date.getTime() == 100; | ||
} | ||
} |