// Custom Imports List addedJars = %jars *.jar //*/ %maven junit:junit:4.12 %maven org.apache.commons:commons-io:1.3.2 %maven commons-io:commons-io:2.7 // CryptoGuard imports import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotNull; import static util.Utils.makeArg; import frontEnd.Interface.outputRouting.ExceptionHandler; import frontEnd.Interface.ArgumentsCheck; import frontEnd.MessagingSystem.routing.Listing; import frontEnd.MessagingSystem.routing.EnvironmentInformation; import frontEnd.MessagingSystem.routing.structure.Default.Report; import frontEnd.MessagingSystem.routing.structure.Scarf.AnalyzerReport; import frontEnd.MessagingSystem.routing.structure.Scarf.BugInstance; import frontEnd.Interface.EntryPoint; import rule.engine.EngineType; import frontEnd.argsIdentifier; import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import soot.G; import util.Utils; import org.apache.commons.io.FileUtils; import java.util.Scanner; import org.apache.commons.io.filefilter.WildcardFileFilter; // region String Statics public static final Boolean isLinux = !System.getProperty("os.name").contains("Windows"); public static final String basePath = System.getProperty("user.dir").replace("/Notebook", ""); public static final String testRec = Utils.osPathJoin(basePath, "samples"); public static final String srcOneGrv = Utils.osPathJoin(testRec, "testable-jar"); public static final String scarfArgs = Utils.osPathJoin(basePath, "src", "main", "resources", "Scarf", "sample.properties"); public static final String testPath = Utils.osPathJoin(System.getProperty("user.dir"), "tmp"); public static final String classSource = Utils.osPathJoin(srcOneGrv, "build", "classes", "java", "main", "tester"); FileUtils.deleteDirectory(new File (testPath)); new File(testPath).mkdirs(); public static final String verySimple_Path = Utils.osPathJoin(testRec, "VerySimple"); public static final String verySimple_Klass = Utils.osPathJoin(verySimple_Path, "very.class"); public static final String verySimple_Klass_xml_1 = Utils.osPathJoin(testPath, "verySimple_klass_1.xml"); public static final String verySimple_Klass_xml_2 = Utils.osPathJoin(testPath, "verySimple_klass_2.xml"); public static final String verySimple_Klass_yaml_1 = Utils.osPathJoin(testPath, "verySimple_klass_2.yaml"); public static final String testablejar_Crypto_class = Utils.osPathJoin(classSource, "Crypto.class"); public static final String testablejar_Crypto_class_xml = Utils.osPathJoin(testPath, "java-file_One.xml"); public static final String testablejar_Crypto_class_xml_0 = Utils.osPathJoin(testPath, "java-file_One_0.xml"); public static final String newTestCaseTwo_Class = Utils.osPathJoin(classSource, "NewTestCase2.class"); public static final String newTestCaseTwo_xml = Utils.osPathJoin(testPath, "NewTestCase2-class.xml"); public static final String newTestCaseTwo_xml_0 = Utils.osPathJoin(testPath, "NewTestCase2-class_0.xml"); public static final String newTestCaseTwo_Class = Utils.osPathJoin(classSource, "NewTestCase2.class"); public static final String newTestCaseTwo_xml = Utils.osPathJoin(testPath, "NewTestCase2-class.xml"); public static final String newTestCaseTwo_xml_0 = Utils.osPathJoin(testPath, "NewTestCase2-class_0.xml"); public static final String tempFileOutTxt_two = Utils.osPathJoin(testPath, "testable-jar_classFiles.txt"); public static final String[] classFiles = { Utils.osPathJoin(classSource, "PBEUsage.class"), Utils.osPathJoin(classSource, "UrlFrameWorks.class"), Utils.osPathJoin(classSource, "NewTestCase1.class"), Utils.osPathJoin(classSource, "NewTestCase2.class") }; public static final String srcOneGrvDep = Utils.osPathJoin(testRec, "testable-jar", "build", "dependencies"); public static final String tempFileOutXML_Class_0 = Utils.osPathJoin(testPath, "java-class-file_0.xml"); public static final String tempFileOutTxt_two_0 = Utils.osPathJoin(testPath, "testable-jar_classFiles_0.txt"); public static final String tempFileOutXML_Class_1 = Utils.osPathJoin(testPath, "java-class-file_1.xml"); public static final String tempFileOutXML_Class_Stream_0 = Utils.osPathJoin(testPath, "java-class-file-stream_0.xml"); // Utililties String cur_user = System.getProperty("user.name"); String java_home = "/home/" + cur_user + "/.sdkman/candidates/java/8*.0.25*"; String java7_home = "/home/" + cur_user + "/.sdkman/candidates/java/7*"; String android_home = "/home/" + cur_user + "/.sdkman/candidates/android/current"; //Retrieving a specific path from a wild card based list public static String getFileFromWildCard(String path) { String[] split = path.split("/"); StringBuilder build = new StringBuilder(); for (int itr = 0;itr < split.length - 1;itr ++) build.append(split[itr]).append("/"); String[] files = new File(build.toString()).list(new WildcardFileFilter(split[split.length-1])); if (files.length > 0) return build.toString() + files[files.length - 1]; else return null; } java_home = getFileFromWildCard(java_home); java7_home = getFileFromWildCard(java7_home); public static void print(String file) { try { Scanner reader = new Scanner(new File(file)); while (reader.hasNextLine()) System.out.println(reader.nextLine()); } catch (FileNotFoundException e) { } } public void main_TestableFile_VerySimple() { G.reset(); soot.G.v().reset(); String source = verySimple_Klass; String fileOut = verySimple_Klass_xml_1; new File(fileOut).delete(); if (isLinux) { String args = makeArg(argsIdentifier.FORMAT, EngineType.CLASSFILES) + makeArg(argsIdentifier.FORMATOUT, Listing.ScarfXML) + makeArg(argsIdentifier.SOURCE, source) + makeArg(argsIdentifier.NOEXIT) + makeArg(argsIdentifier.ANDROID, "/InvalidPath/") + makeArg(argsIdentifier.VERYVERBOSE) + makeArg(argsIdentifier.STREAM) + makeArg(argsIdentifier.PRETTY) + makeArg(argsIdentifier.JAVA, java_home) + makeArg(argsIdentifier.OUT, fileOut); try { EntryPoint.main(args.split(" ")); AnalyzerReport report = AnalyzerReport.deserialize(new File(fileOut)); assertFalse(report.getBugInstance().isEmpty()); List style = new ArrayList<>(); for (BugInstance instance : report.getBugInstance()) style.add(instance.getClassName()); assertTrue( report.getBugInstance().stream() .anyMatch( bugInstance -> { try { return bugInstance .getClassName() .contains(Utils.retrieveFullyQualifiedName(source)); // bugInstance.getlocation().get(0).getSourceFile().startsWith(Utils.retrieveFullyQualifiedName(source)); } catch (ExceptionHandler exceptionHandler) { exceptionHandler.printStackTrace(); return false; } })); print(fileOut); } catch (Exception e) { e.printStackTrace(); assertNull(e); } } } main_TestableFile_VerySimple() public void main_TestableFile_VerySimple_XML() { G.reset(); soot.G.v().reset(); String source = verySimple_Klass; String fileOut = verySimple_Klass_xml_2; Listing type = Listing.XMLGeneric; new File(fileOut).delete(); if (isLinux) { String args = makeArg(argsIdentifier.FORMAT, EngineType.CLASSFILES) + makeArg(argsIdentifier.FORMATOUT, type) + makeArg(argsIdentifier.SOURCE, source) + makeArg(argsIdentifier.NOEXIT) + makeArg(argsIdentifier.ANDROID, "/InvalidPath/") + makeArg(argsIdentifier.VERYVERBOSE) + // makeArg(argsIdentifier.STREAM) makeArg(argsIdentifier.PRETTY) + makeArg(argsIdentifier.JAVA, java_home) + makeArg(argsIdentifier.OUT, fileOut); try { EntryPoint.main(args.split(" ")); Report report = Report.deserialize(new File(fileOut), type); assertFalse(report.getIssues().isEmpty()); assertTrue( report.getIssues().stream() .anyMatch( bugInstance -> { try { return bugInstance .getFullPath() .contains(Utils.retrieveFullyQualifiedName(source)); } catch (ExceptionHandler e) { assertNull(e); e.printStackTrace(); } return false; })); print(fileOut); } catch (Exception e) { e.printStackTrace(); assertNull(e); } } } main_TestableFile_VerySimple_XML() public void main_TestableFile_VerySimple_YAML() { G.reset(); soot.G.v().reset(); String source = verySimple_Klass; String fileOut = verySimple_Klass_yaml_1; Listing type = Listing.YAMLGeneric; new File(fileOut).delete(); if (isLinux) { String args = makeArg(argsIdentifier.FORMAT, EngineType.CLASSFILES) + makeArg(argsIdentifier.FORMATOUT, type) + makeArg(argsIdentifier.SOURCE, source) + makeArg(argsIdentifier.NOEXIT) + makeArg(argsIdentifier.ANDROID, "/InvalidPath/") + makeArg(argsIdentifier.VERYVERBOSE) + // makeArg(argsIdentifier.STREAM) makeArg(argsIdentifier.PRETTY) + makeArg(argsIdentifier.JAVA, java_home) + makeArg(argsIdentifier.OUT, fileOut); try { EntryPoint.main(args.split(" ")); Report report = Report.deserialize(new File(fileOut), type); assertFalse(report.getIssues().isEmpty()); assertTrue( report.getIssues().stream() .anyMatch( bugInstance -> { try { return bugInstance .getFullPath() .contains(Utils.retrieveFullyQualifiedName(source)); } catch (ExceptionHandler e) { assertNull(e); e.printStackTrace(); } return false; })); print(fileOut); } catch (Exception e) { e.printStackTrace(); assertNull(e); } } } main_TestableFile_VerySimple_YAML()