Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add miscellaneous tests #471

Merged
merged 28 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
42fe377
Move code to legacy in preparation for redesign
fwendland Mar 29, 2022
d02efe3
Add notice regarding redesign
fwendland Apr 7, 2022
2490afb
Merge branch 'main' into redesign
fwendland Apr 7, 2022
69aa026
Merge branch 'main' into redesign
maximiliankaul Apr 27, 2022
24a89d7
Merge branch 'main' into redesign
fwendland Apr 29, 2022
6c6a12e
Use inplace sorting of suggestion list for console completion
fwendland Apr 29, 2022
3616bcf
Fix minor code smells
fwendland Apr 29, 2022
7414f56
Fix some major code smells
fwendland Apr 29, 2022
1e561bc
fix toComparableString(String) (#473)
seelchen Apr 29, 2022
16727d5
add test for evaluateMultiplicationExpr in ExpressionEvaluator
seelchen Apr 25, 2022
3f69303
add test for division in evaluateMultiplicationExpr
seelchen Apr 25, 2022
477f501
add test for modulo, shifts, bitwise "&" and bitwise "|" in evaluateM…
seelchen Apr 25, 2022
fb46009
make numbers in expression smaller
seelchen Apr 25, 2022
efe448d
separate multiplication operations into own rules
seelchen Apr 28, 2022
c2e9617
add tests for rest of ExpressionHelper.toComparableString functions
seelchen Apr 28, 2022
c4573ab
add test for null
seelchen Apr 28, 2022
d769017
add test for collectVars
seelchen Apr 28, 2022
ca4b010
add tests for evaluating unary expressions
seelchen Apr 28, 2022
c5af462
add test for ExpressionHelper asString, asBoolean, asNumber
seelchen Apr 28, 2022
6e8b1d7
add test for Node.followNextDFG
seelchen Apr 28, 2022
16bfad6
add test that uses analyze(String) function for analysis
seelchen Apr 29, 2022
5b484f9
add internal to class
seelchen Apr 29, 2022
b93191a
add test for Utils functions
seelchen Apr 29, 2022
fb5c4ab
formatting
seelchen Apr 29, 2022
0f77554
Merge remote-tracking branch 'origin/main' into sl/test
seelchen May 3, 2022
40eb42a
readd tests with Strings with quote marks at end for toComparableString
seelchen May 3, 2022
4f9fae2
add assert that logicalNotRule is not null and adjust comment
seelchen May 3, 2022
ff133d6
Merge branch 'main' into sl/test
fwendland May 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,62 @@ class ExpressionEvaluatorTest : AbstractTest() {
// directly use the xtext classes
val convertedMark = MarkModelLoader().load(mapOf("" to model))
assertNotNull(convertedMark)

val convertedRule = convertedMark.rules.first()
assertNotNull(convertedRule)
assertNotNull(convertedMark.rules)

val convertedEntity = convertedMark.getEntity("MyClass")
assertNotNull(convertedEntity)

for (convertedRule in convertedMark.rules) {
assertNotNull(convertedRule)

val eval =
ExpressionEvaluator(
graph,
convertedMark,
convertedRule,
resultCtx,
ServerConfiguration.builder().build(),
markContextHolder
)

val result = eval.evaluateExpression(convertedRule.statement.ensure.exp)
assertEquals(1, result.size)

val ctx = result[0]
assertNotNull(ctx)

assertEquals(
ConstantValue.of(true),
ctx,
"${convertedRule.name} was not evaluated to true"
)
}
}

@Test
fun nullExpressionTest() {
val evaluator = Evaluator(mark, ServerConfiguration.builder().build())

// Let's provide the list of entities and nodes, this is normally done by
// findInstancesForEntities. In this case, we simply point it towards our variable
// declaration
val entities = listOf(listOf(Pair("a", a)))

val markContextHolder = evaluator.createMarkContext(entities)
assertEquals(1, markContextHolder.allContexts.size)

val eval =
ExpressionEvaluator(
graph,
convertedMark,
convertedRule,
resultCtx,
null,
null,
null,
ServerConfiguration.builder().build(),
markContextHolder
)

val result = eval.evaluateExpression(convertedRule.statement.ensure.exp)
assertEquals(1, result.size)

val ctx = result[0]
assertNotNull(ctx)

assertEquals(ConstantValue.of(true), ctx)
val result = eval.evaluateExpression(null)
assertEquals(markContextHolder.generateNullResult(), result)
}

companion object {
Expand Down Expand Up @@ -181,26 +213,193 @@ class ExpressionEvaluatorTest : AbstractTest() {
graph = result.graph

var myEntity: EntityDeclaration? = null
var myRule: RuleDeclaration? = null
var eqRule: RuleDeclaration? = null
var multRule: RuleDeclaration? = null
var divRule: RuleDeclaration? = null
var modRule: RuleDeclaration? = null
var leftShiftRule: RuleDeclaration? = null
var rightShiftRule: RuleDeclaration? = null
var bitwiseAndRule: RuleDeclaration? = null
var bitwiseOrRule: RuleDeclaration? = null
var plusSignRule: RuleDeclaration? = null
var minusSignRule: RuleDeclaration? = null
var logicalNotRule: RuleDeclaration? = null
var bitwiseComplementRule: RuleDeclaration? = null

model = mark {
myEntity =
entity("MyClass") {
variable("field")
op("init") { stmt { call("MyClass") { param("field") } } }
}
myRule =
rule("myRule") {
eqRule =
rule("eqRule") {
statement {
using(myEntity!!, "a")
ensure {
// comparison is: a == 1
exp =
comparison(left = operand("a.field"), op = "==", right = lit(1))
}
}
}
multRule =
rule("multRule") {
statement {
using(myEntity!!, "a")
ensure {
// comparison is: a == 1 * 1
exp =
comparison(
left = operand("a.field"),
op = "==",
right = mul(left = lit(1), op = "*", right = lit(1))
)
}
}
}
divRule =
rule("divRule") {
statement {
using(myEntity!!, "a")
ensure {
// comparison is: a == 2 / 2
exp =
comparison(
left = operand("a.field"),
op = "==",
right = mul(left = lit(2), op = "/", right = lit(2))
)
}
}
}
modRule =
rule("modRule") {
statement {
using(myEntity!!, "a")
ensure {
comparison(left = operand("a.field"), op = "==", right = lit(1))
// comparison is: a == 19 % 6
exp =
comparison(
left = operand("a.field"),
op = "==",
right = mul(left = lit(19), op = "%", right = lit(6))
)
}
}
}
leftShiftRule =
rule("leftShiftRule") {
statement {
ensure {
// comparison is: 32 == 2 << 4
exp =
comparison(
left = lit(32),
op = "==",
right = mul(left = lit(2), op = "<<", right = lit(4))
)
}
}
}
rightShiftRule =
rule("rightShiftRule") {
statement {
ensure {
// comparison is: 4 == 32 >> 3
exp =
comparison(
left = lit(4),
op = "==",
right = mul(left = lit(32), op = ">>", right = lit(3))
)
}
}
}
bitwiseAndRule =
rule("bitwiseAndRule") {
statement {
ensure {
// comparison is: 2 == 11 & 6 (0b10 == 0b1011 & 0b110)
exp =
comparison(
left = lit(2),
op = "==",
right = mul(left = lit(11), op = "&", right = lit(6))
)
}
}
}

bitwiseOrRule =
rule("bitwiseOrRule") {
statement {
ensure {
// comparison is: 5 == 7 & 2 (0b101 == 0b111 & 0b10)
exp =
comparison(
left = lit(5),
op = "==",
right = mul(left = lit(7), op = "&^", right = lit(2))
)
}
}
}
plusSignRule =
rule("plusSignRule") {
statement {
ensure {
// comparison is: 5 == +5
exp =
comparison(
left = lit(5),
op = "==",
right = unary(exp = lit(5), op = "+")
)
}
}
}
minusSignRule =
rule("minusSignRule") {
statement {
ensure {
// comparison is: 5 != -(5)
exp =
comparison(
left = lit(5),
op = "!=",
right = unary(exp = lit(5), op = "-")
)
}
}
}
logicalNotRule =
rule("logicalNotRule") {
statement {
ensure {
// comparison is: true == !false
exp =
comparison(
left = lit(true),
op = "==",
right = unary(exp = lit(false), op = "!")
)
}
}
}
}

assertNotNull(myRule)
assertNotNull(eqRule)
assertNotNull(multRule)
assertNotNull(divRule)
assertNotNull(modRule)
assertNotNull(leftShiftRule)
assertNotNull(rightShiftRule)
assertNotNull(bitwiseAndRule)
assertNotNull(bitwiseOrRule)
assertNotNull(plusSignRule)
assertNotNull(minusSignRule)
assertNotNull(logicalNotRule)
assertNotNull(myEntity)

mark = MarkModelLoader().load(mapOf("" to model))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.fraunhofer.aisec.codyze.legacy.crymlin

import de.fraunhofer.aisec.codyze.legacy.config.CodyzeConfiguration
import de.fraunhofer.aisec.codyze.legacy.config.Configuration
import de.fraunhofer.aisec.codyze.legacy.config.CpgConfiguration
import de.fraunhofer.aisec.cpg.TranslationConfiguration
import de.fraunhofer.aisec.cpg.TranslationManager
import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
Expand All @@ -12,7 +15,7 @@ abstract class AbstractTest {
* Helper method for initializing an Analysis Run.
*
* @param sourceLocations
* @return
* @return TranslationManager
*/
@JvmStatic
protected fun newAnalysisRun(vararg sourceLocations: File): TranslationManager {
Expand All @@ -30,5 +33,23 @@ abstract class AbstractTest {
)
.build()
}

/**
* Helper method for initializing an Analysis Run. Returned Configuration can be used to
* initialize AnalysisServer and start the analysis with {@link
* de.fraunhofer.aisec.codyze.legacy.analysis.AnalysisServer#analyze(String url)}.
*
* @param codyzeConfig
* @return Configuration
*/
@JvmStatic
protected fun newAnalysisRun(codyzeConfig: CodyzeConfiguration): Configuration {
val cpgConfig = CpgConfiguration()
cpgConfig.debugParser = true
cpgConfig.failOnError = false
cpgConfig.defaultPasses = true
cpgConfig.passes = listOf(IdentifierPass(), EdgeCachePass())
return Configuration(codyzeConfig, cpgConfig)
}
}
}
Loading