From c597985175d465318b6036b1a0094da31f1d015b Mon Sep 17 00:00:00 2001
From: "U-BASIS\\dgrove" <dgrove@WIN-DGROV-4999.basistech.net>
Date: Wed, 16 May 2018 16:18:15 -0400
Subject: [PATCH] Partial implementation.

---
 .../autopsy/ingest/EmbeddedFileTest.java      | 11 +---
 .../autopsy/ingest/IngestFileFiltersTest.java | 23 +++------
 .../EncryptionDetectionTest.java              | 24 ++++-----
 .../autopsy/testutils/CaseUtils.java          | 51 +++++++++++++------
 4 files changed, 57 insertions(+), 52 deletions(-)

diff --git a/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/EmbeddedFileTest.java b/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/EmbeddedFileTest.java
index 6af0ec81f9..1321d80213 100755
--- a/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/EmbeddedFileTest.java
+++ b/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/EmbeddedFileTest.java
@@ -62,17 +62,10 @@ public EmbeddedFileTest(String name) {
 
     @Override
     public void setUp() {
-        CaseUtils.createCase(CASE_NAME);
+        openCase = CaseUtils.createAsCurrentCase(CASE_NAME);
         ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, IMAGE_PATH);
         
-        try {
-            openCase = Case.getCurrentCaseThrows();
-        } catch (NoCurrentCaseException ex) {
-            Exceptions.printStackTrace(ex);
-            Assert.fail(ex);
-        } 
-        
         IngestModuleTemplate embeddedTemplate = IngestUtils.getIngestModuleTemplate(new EmbeddedFileExtractorModuleFactory());
         IngestModuleTemplate hashLookupTemplate = IngestUtils.getIngestModuleTemplate(new HashLookupModuleFactory());
 
@@ -91,7 +84,7 @@ public void setUp() {
 
     @Override
     public void tearDown() {
-        CaseUtils.closeCase();
+        CaseUtils.closeCurrentCase();
     }
     
     public void testEncryption() {
diff --git a/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/IngestFileFiltersTest.java b/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/IngestFileFiltersTest.java
index 7be3e4f794..8b99052295 100644
--- a/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/IngestFileFiltersTest.java
+++ b/Core/test/qa-functional/src/org/sleuthkit/autopsy/ingest/IngestFileFiltersTest.java
@@ -68,11 +68,11 @@ public IngestFileFiltersTest(String name) {
 
     @Override
     public void tearDown() {
-        CaseUtils.closeCase();
+        CaseUtils.closeCurrentCase();
     }
     
     public void testBasicDir() {
-        CaseUtils.createCase("testBasicDir");
+        Case currentCase = CaseUtils.createAsCurrentCase("testBasicDir");
         ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, IMAGE_PATH);
 
@@ -82,7 +82,6 @@ public void testBasicDir() {
         FilesSet dirFilter = new FilesSet("Filter", "Filter to find all files in dir1.", false, true, rule);        
 
         try {
-            Case currentCase = Case.getCurrentCase();
             ArrayList<IngestModuleTemplate> templates =  new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new FileTypeIdModuleFactory()));
             IngestJobSettings ingestJobSettings = new IngestJobSettings(IngestFileFiltersTest.class.getCanonicalName(), IngestJobSettings.IngestType.FILES_ONLY, templates, dirFilter);
@@ -114,7 +113,7 @@ public void testBasicDir() {
     }
     
     public void testExtAndDirWithOneRule() {
-        CaseUtils.createCase("testExtAndDirWithOneRule");
+        Case currentCase = CaseUtils.createAsCurrentCase("testExtAndDirWithOneRule");
         ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, IMAGE_PATH);
 
@@ -124,7 +123,6 @@ public void testExtAndDirWithOneRule() {
         FilesSet filesExtDirsFilter = new FilesSet("Filter", "Filter to find all jpg files in dir1.", false, true, rules);
         
         try {
-            Case currentCase = Case.getCurrentCase();
             ArrayList<IngestModuleTemplate> templates =  new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new FileTypeIdModuleFactory()));
             IngestJobSettings ingestJobSettings = new IngestJobSettings(IngestFileFiltersTest.class.getCanonicalName(), IngestJobSettings.IngestType.FILES_ONLY, templates, filesExtDirsFilter);
@@ -149,7 +147,7 @@ public void testExtAndDirWithOneRule() {
     }
 
     public void testExtAndDirWithTwoRules() {
-        CaseUtils.createCase("testExtAndDirWithTwoRules");
+        Case currentCase = CaseUtils.createAsCurrentCase("testExtAndDirWithTwoRules");
         ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, IMAGE_PATH);
 
@@ -160,7 +158,6 @@ public void testExtAndDirWithTwoRules() {
         FilesSet filesExtDirsFilter = new FilesSet("Filter", "Filter to find all files in dir1 and all files with jpg extention.", false, true, rules);        
           
         try {
-            Case currentCase = Case.getCurrentCase();
             ArrayList<IngestModuleTemplate> templates =  new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new FileTypeIdModuleFactory()));
             IngestJobSettings ingestJobSettings = new IngestJobSettings(IngestFileFiltersTest.class.getCanonicalName(), IngestJobSettings.IngestType.FILES_ONLY, templates, filesExtDirsFilter);
@@ -193,7 +190,7 @@ public void testExtAndDirWithTwoRules() {
    }
    
     public void testFullFileNameRule() {
-        CaseUtils.createCase("testFullFileNameRule");
+        Case currentCase = CaseUtils.createAsCurrentCase("testFullFileNameRule");
         ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, IMAGE_PATH);
 
@@ -203,7 +200,6 @@ public void testFullFileNameRule() {
         FilesSet fullNameFilter = new FilesSet("Filter", "Filter to find file.docx.", false, true, rules);
                  
         try {
-            Case currentCase = Case.getCurrentCase();
             ArrayList<IngestModuleTemplate> templates =  new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new FileTypeIdModuleFactory()));
             IngestJobSettings ingestJobSettings = new IngestJobSettings(IngestFileFiltersTest.class.getCanonicalName(), IngestJobSettings.IngestType.FILES_ONLY, templates, fullNameFilter);
@@ -228,7 +224,7 @@ public void testFullFileNameRule() {
     }
 
     public void testCarvingWithExtRuleAndUnallocSpace() {
-        CaseUtils.createCase("testCarvingWithExtRuleAndUnallocSpace");
+        Case currentCase = CaseUtils.createAsCurrentCase("testCarvingWithExtRuleAndUnallocSpace");
         ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, IMAGE_PATH);
 
@@ -240,7 +236,6 @@ public void testCarvingWithExtRuleAndUnallocSpace() {
         FilesSet extensionFilter = new FilesSet("Filter", "Filter to files with .jpg and .gif extension.", false, false, rules);
                   
         try {
-            Case currentCase = Case.getCurrentCase();
             ArrayList<IngestModuleTemplate> templates =  new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new FileTypeIdModuleFactory()));
             templates.add(IngestUtils.getIngestModuleTemplate(new PhotoRecCarverIngestModuleFactory()));
@@ -276,7 +271,7 @@ public void testCarvingWithExtRuleAndUnallocSpace() {
     }
   
     public void testCarvingNoUnallocatedSpace() {
-        CaseUtils.createCase("testCarvingNoUnallocatedSpace");
+        Case currentCase = CaseUtils.createAsCurrentCase("testCarvingNoUnallocatedSpace");
         ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, IMAGE_PATH);
 
@@ -288,7 +283,6 @@ public void testCarvingNoUnallocatedSpace() {
         FilesSet extensionFilter = new FilesSet("Filter", "Filter to files with .jpg and .gif extension.", false, true, rules);        
           
         try {
-            Case currentCase = Case.getCurrentCase();
             ArrayList<IngestModuleTemplate> templates =  new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new FileTypeIdModuleFactory()));
             templates.add(IngestUtils.getIngestModuleTemplate(new PhotoRecCarverIngestModuleFactory()));
@@ -309,7 +303,7 @@ public void testCarvingNoUnallocatedSpace() {
     }
 
     public void testEmbeddedModule() {
-        CaseUtils.createCase("testEmbeddedModule");
+        Case currentCase = CaseUtils.createAsCurrentCase("testEmbeddedModule");
         LocalFilesDSProcessor dataSourceProcessor = new LocalFilesDSProcessor();
         IngestUtils.addDataSource(dataSourceProcessor, ZIPFILE_PATH);
         
@@ -322,7 +316,6 @@ public void testEmbeddedModule() {
         FilesSet embeddedFilter = new FilesSet("Filter", "Filter to files with .jpg extension.", false, false, rules);
                   
         try {
-            Case currentCase = Case.getCurrentCase();
             ArrayList<IngestModuleTemplate> templates =  new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new FileTypeIdModuleFactory()));
             templates.add(IngestUtils.getIngestModuleTemplate(new EmbeddedFileExtractorModuleFactory()));
diff --git a/Core/test/qa-functional/src/org/sleuthkit/autopsy/modules/encryptiondetection/EncryptionDetectionTest.java b/Core/test/qa-functional/src/org/sleuthkit/autopsy/modules/encryptiondetection/EncryptionDetectionTest.java
index cd55b16f82..726a0e5439 100755
--- a/Core/test/qa-functional/src/org/sleuthkit/autopsy/modules/encryptiondetection/EncryptionDetectionTest.java
+++ b/Core/test/qa-functional/src/org/sleuthkit/autopsy/modules/encryptiondetection/EncryptionDetectionTest.java
@@ -18,6 +18,7 @@
  */
 package org.sleuthkit.autopsy.modules.encryptiondetection;
 
+import java.io.File;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
@@ -78,7 +79,11 @@ public EncryptionDetectionTest(String name) {
 
     @Override
     public void tearDown() {
-        CaseUtils.closeCase();
+        File keepCaseData = new File(".keepCaseData");
+        if (keepCaseData.exists()) {
+            System.out.println("!");
+        }
+        CaseUtils.closeCurrentCase();
     }
 
     /**
@@ -86,10 +91,9 @@ public void tearDown() {
      */
     public void testBitlockerEncryption() {
         try {
-            CaseUtils.createCase(BITLOCKER_CASE_NAME);
+            Case openCase = CaseUtils.createAsCurrentCase(BITLOCKER_CASE_NAME);
             ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
             IngestUtils.addDataSource(dataSourceProcessor, BITLOCKER_IMAGE_PATH);
-            Case openCase = Case.getCurrentCaseThrows();
 
             /*
              * Create ingest job settings.
@@ -146,7 +150,7 @@ public void testBitlockerEncryption() {
 
             errorMessage = "Expected to find 'vol2', but no such volume exists.";
             assertEquals(errorMessage, true, vol2Found);
-        } catch (NoCurrentCaseException | TskCoreException ex) {
+        } catch (TskCoreException ex) {
             Exceptions.printStackTrace(ex);
             Assert.fail(ex);
         }
@@ -157,13 +161,10 @@ public void testBitlockerEncryption() {
      */
     public void testPasswordProtection() {
         try {
-            CaseUtils.createCase(PASSWORD_DETECTION_CASE_NAME);
-
+            Case openCase = CaseUtils.createAsCurrentCase(PASSWORD_DETECTION_CASE_NAME);
             ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
             IngestUtils.addDataSource(dataSourceProcessor, PASSWORD_DETECTION_IMAGE_PATH);
 
-            Case openCase = Case.getCurrentCaseThrows();    
-
             /*
              * Create ingest job settings.
              */
@@ -235,7 +236,7 @@ public void testPasswordProtection() {
                     }
                 }
             }
-        } catch (NoCurrentCaseException | TskCoreException ex) {
+        } catch (TskCoreException ex) {
             Exceptions.printStackTrace(ex);
             Assert.fail(ex);
         }
@@ -256,11 +257,10 @@ public void testPasswordProtection() {
      */
     public void testVeraCryptSupport() {
         try {
-            CaseUtils.createCase(VERACRYPT_DETECTION_CASE_NAME);
+            Case openCase = CaseUtils.createAsCurrentCase(VERACRYPT_DETECTION_CASE_NAME);
             ImageDSProcessor dataSourceProcessor = new ImageDSProcessor();
             IngestUtils.addDataSource(dataSourceProcessor, VERACRYPT_DETECTION_IMAGE_PATH);
 
-            Case openCase = Case.getCurrentCaseThrows();
             ArrayList<IngestModuleTemplate> templates = new ArrayList<>();
             templates.add(IngestUtils.getIngestModuleTemplate(new EncryptionDetectionModuleFactory()));
             //image includes an encrypted container file with size greater than 5 mb so default settings detect it
@@ -289,7 +289,7 @@ public void testVeraCryptSupport() {
                 numberOfEncryptedContainers += file.getArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_ENCRYPTION_SUSPECTED).size();
             }
             assertEquals("Encrypted Container file should have one encyption suspected artifact", 1, numberOfEncryptedContainers);
-        } catch (NoCurrentCaseException | TskCoreException ex) {
+        } catch (TskCoreException ex) {
             Exceptions.printStackTrace(ex);
             Assert.fail(ex);
         }
diff --git a/Core/test/qa-functional/src/org/sleuthkit/autopsy/testutils/CaseUtils.java b/Core/test/qa-functional/src/org/sleuthkit/autopsy/testutils/CaseUtils.java
index 17b0c2168b..7e0a9f9799 100755
--- a/Core/test/qa-functional/src/org/sleuthkit/autopsy/testutils/CaseUtils.java
+++ b/Core/test/qa-functional/src/org/sleuthkit/autopsy/testutils/CaseUtils.java
@@ -30,6 +30,7 @@
 import org.sleuthkit.autopsy.casemodule.Case;
 import org.sleuthkit.autopsy.casemodule.CaseActionException;
 import org.sleuthkit.autopsy.casemodule.CaseDetails;
+import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException;
 
 /**
  * Class with common methods for testing related to the creation and elimination
@@ -40,9 +41,12 @@ public final class CaseUtils {
     /**
      * Create a case case directory and case for the given case name.
      *
-     * @param caseName the name for the case and case directory to have
+     * @param caseName The name for the case and case directory to have
+     *
+     * @return The new case
      */
-    public static void createCase(String caseName) {
+    public static Case createAsCurrentCase(String caseName) {
+        Case currentCase = null;
         //Make sure the case is starting with a clean state. So delete the case directory, if it exists.
         Path caseDirectoryPath = Paths.get(System.getProperty("java.io.tmpdir"), caseName);
         File caseDir = new File(caseDirectoryPath.toString());
@@ -59,27 +63,42 @@ public static void createCase(String caseName) {
 
         try {
             Case.createAsCurrentCase(Case.CaseType.SINGLE_USER_CASE, caseDirectoryPath.toString(), new CaseDetails(caseName));
-        } catch (CaseActionException ex) {
+            currentCase = Case.getCurrentCaseThrows();
+        } catch (CaseActionException | NoCurrentCaseException ex) {
             Exceptions.printStackTrace(ex);
             Assert.fail(ex);
         }
 
         assertTrue(caseDir.exists());
+
+        return currentCase;
     }
 
     /**
-     * Close the current case, fails test if case was unable to be closed.
+     * Close and delete the current case. This will fail the test if the case
+     * was unable to be closed.
+     *
+     * Note: This method will skip case deletion if '//DLG:' exists in the class
+     * folder.
      */
-    public static void closeCase() {
+    public static void closeCurrentCase() {
         try {
-            Case.closeCurrentCase();
-            //Seems like we need some time to close the case, so file handler later can delete the case directory.
-            try {
-                Thread.sleep(20000);
-            } catch (Exception ex) {
-
+            if (Case.isCaseOpen()) {
+                String currentCaseDirectory = Case.getCurrentCase().getCaseDirectory();
+                Case.closeCurrentCase();
+                File keepCaseData = new File(".keepCaseData");
+                if (keepCaseData.exists()) {
+                    deleteCaseDir(new File(currentCaseDirectory));
+                }
             }
-        } catch (CaseActionException ex) {
+            System.gc();
+            //DLG: //Seems like we need some time to close the case, so file handler later can delete the case directory.
+            //DLG: try {
+            //DLG:     Thread.sleep(20000);
+            //DLG: } catch (Exception ex) {
+            //DLG: 
+            //DLG: }
+        } catch (CaseActionException | IOException ex) {
             Exceptions.printStackTrace(ex);
             Assert.fail(ex);
         }
@@ -89,9 +108,9 @@ public static void closeCase() {
      * Delete the case directory if it exists, thows exception if unable to
      * delete case dir to allow the user to determine failure with.
      *
-     * @param caseDirectory the case directory to delete
+     * @param caseDirectory The case directory to delete
      *
-     * @throws IOException thrown if there was an problem deleting the case
+     * @throws IOException Thrown if there was an problem deleting the case
      *                     directory
      */
     public static void deleteCaseDir(File caseDirectory) throws IOException {
@@ -105,6 +124,6 @@ public static void deleteCaseDir(File caseDirectory) throws IOException {
      * Private constructor to prevent utility class instantiation.
      */
     private CaseUtils() {
-    }    
-    
+    }
+
 }
-- 
GitLab