| 26 | |
| 27 | // TODO Use LL-zip |
| 28 | public class Scanner { |
| 29 | |
| 30 | public static boolean detectSus(File file, boolean verbose, TestResult.TestResultLevel level, boolean color, boolean json) throws Exception { |
| 31 | final PrintWriter pw = new PrintWriter(System.out); |
| 32 | |
| 33 | if (file.isDirectory()) { |
| 34 | return detectSusFromDirectory(file, verbose, level, color, pw, json); |
| 35 | } |
| 36 | |
| 37 | if (file.toString().toLowerCase().endsWith(".class")) { |
| 38 | return detectSusFromClassfile(file, verbose, level, color, pw, json); |
| 39 | } |
| 40 | |
| 41 | try |
| 42 | (final JarFile jarFile = new JarFile(file)) { |
| 43 | return detectSusFromJar(jarFile, verbose, level, color, pw, json); |
| 44 | } catch (final Exception e) { |
| 45 | System.err.println("Invalid directory or jar file " + file.getAbsolutePath()); |
| 46 | throw e; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public static boolean detectSusFromDirectory(File dir, boolean verbose, TestResult.TestResultLevel level, boolean color, PrintWriter pw, boolean json) { |
| 51 | if (!dir.isDirectory()) { |
| 52 | System.err.println("Invalid directory " + dir.getAbsolutePath()); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | boolean foundSus = false; |
| 57 | final File[] files = dir.listFiles(); |
| 58 | |
| 59 | if (files != null) { |
| 60 | for (final File file : files) { |
| 61 | if (file.isDirectory() && detectSusFromDirectory(file, verbose, level, color, pw, json)) { |
| 62 | foundSus = true; |
| 63 | } |
| 64 | |
| 65 | final String fileName = file.toString().toLowerCase(); |
| 66 | |
| 67 | if (fileName.endsWith(".class")) { |
| 68 | try { |
| 69 | if (detectSusFromClassfile(file, verbose, level, color, pw, json)) { |
| 70 | foundSus = true; |
| 71 | } |
| 72 | } catch (final Exception e) { |
| 73 | System.err.println("Invalid class file " + file.getAbsolutePath()); |
| 74 | e.printStackTrace(); |
| 75 | } |
| 76 | |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | if (!fileName.endsWith(".jar")) { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | try |
| 85 | (final JarFile jarFile = new JarFile(file)) { |
nothing calls this directly
no outgoing calls
no test coverage detected