From fca31dfc395811ed61e79570a811352cb3868b1c Mon Sep 17 00:00:00 2001 From: "Achim D. Brucker" Date: Mon, 29 Aug 2016 22:47:33 +0100 Subject: [PATCH] Refactured initial scalatest setup. --- .../crosslanguage/test/apps/AppTest.scala | 31 +++++---- .../test/apps/FeatherweightInfoTest.scala | 63 +++++++++++-------- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/AppTest.scala b/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/AppTest.scala index 350e0ae..e4ceb08 100644 --- a/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/AppTest.scala +++ b/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/AppTest.scala @@ -25,11 +25,16 @@ import eu.aniketos.dasca.crosslanguage.util.SourceLocation import eu.aniketos.dasca.crosslanguage.builder.FilterJSFrameworks import scala.collection.mutable.LinkedHashSet import com.ibm.wala.classLoader.IMethod +import eu.aniketos.dasca.crosslanguage.builder.CrossBuilderOption +import eu.aniketos.dasca.crosslanguage.builder.FilterJavaCallSites +import eu.aniketos.dasca.crosslanguage.builder.MockCordovaExec +import eu.aniketos.dasca.crosslanguage.builder.ReplacePluginDefinesAndRequires +import eu.aniketos.dasca.crosslanguage.builder.FilterJSFrameworks +import eu.aniketos.dasca.crosslanguage.builder.PreciseJS +import eu.aniketos.dasca.crosslanguage.builder.RunBuildersInParallel - -class AppTest(apk:String, expectedConnections:Set[(SourceLocation, SourceLocation)]){ +class AppTest { def apkDir = "src/main/resources/"; - private var js2JavaHits = -1; private var js2JavaMisses = -1; private var js2JavaTotal = -1; @@ -57,7 +62,6 @@ class AppTest(apk:String, expectedConnections:Set[(SourceLocation, SourceLocatio 100.0 * js2JavaHits / js2JavaTotal } - def getJava2JSHits() = { java2JSHits } @@ -92,13 +96,12 @@ class AppTest(apk:String, expectedConnections:Set[(SourceLocation, SourceLocatio def getFalsePositives() = { falsePositives } - - def analyze() = { - val builder = CordovaCGBuilder(new File(apkDir,apk)) - val mcg = builder.createCallGraph - val crossTargets = mcg.getAllCrossTargets + + def analyze(apk:String, options:List[CrossBuilderOption], expectedConnections:Set[(SourceLocation, SourceLocation)]):Boolean = { + val builder = CordovaCGBuilder(new File(apkDir, apk)) + builder.setOptions(options:_*) + val crossTargets = builder.createCallGraph.getAllCrossTargets val convertedCrossTargets = convertToSourceLocationPairs(crossTargets) - val (javaPairs, jsPairs) = convertedCrossTargets.partition({case (origin, target) => origin.isInstanceOf[JavaSourceLocation]}) java2JSTotal = javaPairs.size js2JavaTotal = jsPairs.size @@ -111,9 +114,10 @@ class AppTest(apk:String, expectedConnections:Set[(SourceLocation, SourceLocatio truePositives = found falseNegatives = notFound falsePositives = convertedCrossTargets -- expectedConnections + true } - - def convertToSourceLocationPairs(crossTargets: Map[(CGNode, CallSiteReference), LinkedHashSet[CGNode]]): Set[(SourceLocation, SourceLocation)] = { + + def convertToSourceLocationPairs(crossTargets: Map[(CGNode, CallSiteReference), LinkedHashSet[CGNode]]): Set[(SourceLocation, SourceLocation)] = { for ( origin <- crossTargets.keys; target <- crossTargets.get(origin).get @@ -131,4 +135,7 @@ class AppTest(apk:String, expectedConnections:Set[(SourceLocation, SourceLocatio } } }.toSet + + + } diff --git a/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/FeatherweightInfoTest.scala b/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/FeatherweightInfoTest.scala index 24a1278..221c06a 100644 --- a/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/FeatherweightInfoTest.scala +++ b/src/eu.aniketos.dasca.crosslanguage.test/src/main/scala/eu/aniketos/dasca/crosslanguage/test/apps/FeatherweightInfoTest.scala @@ -18,9 +18,17 @@ import org.scalatest.junit.JUnitRunner import eu.aniketos.dasca.crosslanguage.util.JavaScriptSourceLocation import eu.aniketos.dasca.crosslanguage.util.JavaSourceLocation import eu.aniketos.dasca.crosslanguage.util.SourceLocation +import eu.aniketos.dasca.crosslanguage.builder.CrossBuilderOption +import eu.aniketos.dasca.crosslanguage.builder.FilterJavaCallSites +import eu.aniketos.dasca.crosslanguage.builder.MockCordovaExec +import eu.aniketos.dasca.crosslanguage.builder.ReplacePluginDefinesAndRequires +import eu.aniketos.dasca.crosslanguage.builder.FilterJSFrameworks +import eu.aniketos.dasca.crosslanguage.builder.PreciseJS +import eu.aniketos.dasca.crosslanguage.builder.RunBuildersInParallel + @RunWith(classOf[JUnitRunner]) -class FeatherweightInfoTest extends FlatSpec with Matchers { +class FeatherweightInfoTest extends FlatSpec with Matchers with BeforeAndAfterAll { def ApkName: String = "de.zertapps.dvhma.featherweight.apk" def connections = Set[(SourceLocation, SourceLocation)]( (new JavaSourceLocation(43, "com/borismus/webintent/WebIntent"), new JavaScriptSourceLocation(31, 12, "www/js/index.js")) @@ -39,69 +47,70 @@ class FeatherweightInfoTest extends FlatSpec with Matchers { ,(new JavaScriptSourceLocation(16, 12, "www/plugins/de.zertapps.dvhma.plugins.storage/www/DVHMA-Storage.js"), new JavaSourceLocation(43, "de/zertapps/dvhma/plugins/storage/DVHMAStorage")) ,(new JavaScriptSourceLocation(20, 12, "www/plugins/de.zertapps.dvhma.plugins.storage/www/DVHMA-Storage.js"), new JavaSourceLocation(43, "de/zertapps/dvhma/plugins/storage/DVHMAStorage")) ) - - def fixture = - new { - val app = new AppTest(ApkName, connections); - app.analyze(); - } - - + def options = List( + FilterJavaCallSites + ,MockCordovaExec + ,ReplacePluginDefinesAndRequires + ,FilterJSFrameworks + ) + + + val app = new AppTest(); + override def beforeAll(){ + app.analyze(ApkName, options, connections) + } "Java -> JavaScript" should "report ten hits" in { - fixture.app.getJS2JavaHits() should be (10) + app.getJava2JSHits() should be (10) } it should "report no misses" in { - fixture.app.getJS2JavaMisses() should be (0) + app.getJS2JavaMisses() should be (0) } it should "report no errors" in { - fixture.app.getJS2JavaErrors() should be (0) + app.getJS2JavaErrors() should be (0) } it should "have a precision of 100%" in { - fixture.app.getJS2JavaPrecision() should be (100.0 +- 0.1) + app.getJS2JavaPrecision() should be (100.0 +- 0.1) } it should "have a recall of 100%" in { - fixture.app.getJS2JavaRecall() should be (100.0 +- 0.1) - } - + app.getJS2JavaRecall() should be (100.0 +- 0.1) + } - - "JavaScript -> Java" should "report ten hits" in { - fixture.app.getJS2JavaHits() should be (5) + "JavaScript -> Java" should "report five hits" in { + app.getJS2JavaHits() should be (5) } it should "report no misses" in { - fixture.app.getJS2JavaMisses() should be (0) + app.getJS2JavaMisses() should be (0) } it should "report no errors" in { - fixture.app.getJS2JavaErrors() should be (0) + app.getJS2JavaErrors() should be (0) } it should "have a precision of 100%" in { - fixture.app.getJS2JavaPrecision() should be (100.0 +- 0.1) + app.getJS2JavaPrecision() should be (100.0 +- 0.1) } it should "have a recall of 100%" in { - fixture.app.getJS2JavaRecall() should be (100.0 +- 0.1) + app.getJS2JavaRecall() should be (100.0 +- 0.1) } - "Reported connections" should "contain all expected connections" in { - fixture.app.getTruePositives() should contain theSameElementsAs connections + app.getTruePositives() should contain theSameElementsAs connections } it should "not contain false negatives" in { - fixture.app.getFalseNegatives() shouldBe empty + app.getFalseNegatives() shouldBe empty } it should "not contain false positives" in { - fixture.app.getFalsePositives() shouldBe empty + app.getFalsePositives() shouldBe empty } }