1   package org.kit.furia;
2   
3   import static org.junit.Assert.*;
4   
5   import java.io.File;
6   import java.io.IOException;
7   import java.util.Properties;
8   
9   import org.ajmm.obsearch.index.utils.Directory;
10  import org.apache.log4j.Logger;
11  import org.apache.log4j.PropertyConfigurator;
12  import org.junit.Before;
13  
14  import org.junit.After;
15  import org.junit.BeforeClass;
16  import org.junit.Test;
17  import org.kit.furia.fragment.FragmentBuilderClient;
18  import org.kit.furia.misc.FuriaProperties;
19  
20  /*
21   Furia-chan: An Open Source software license violation detector.    
22   Copyright (C) 2007 Kyushu Institute of Technology
23  
24   This program is free software: you can redistribute it and/or modify
25   it under the terms of the GNU General Public License as published by
26   the Free Software Foundation, either version 3 of the License, or
27   (at your option) any later version.
28  
29   This program is distributed in the hope that it will be useful,
30   but WITHOUT ANY WARRANTY; without even the implied warranty of
31   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32   GNU General Public License for more details.
33  
34   You should have received a copy of the GNU General Public License
35   along with this program.  If not, see <http://www.gnu.org/licenses/>.
36   */
37  
38  /**
39   * OverallTest tests that Furia can run. It fails if Furia cannot detect the
40   * given programs within a certain threshold
41   * @author Arnoldo Jose Muller Molina
42   * @since 0
43   */
44  
45  public class OverallTest {
46      private static final Logger logger = Logger.getLogger("OverallTest");
47      /**
48       * Properties for the test.
49       */    
50          
51      @Before
52      public void setUp() throws Exception {
53      }
54  
55  
56      FuriaChanEngine engine = null;
57      @Test
58      public void testAll() throws IOException, Exception{
59          
60          try {
61              PropertyConfigurator.configure(FuriaProperties.getProperty("log4j.file"));
62          } catch (Exception e) {
63              System.err.print("Make sure log4j is configured properly"
64                      + e.getMessage()); 
65              e.printStackTrace();
66              assertTrue(false);
67          }
68  
69          // delete the output directory before starting a new test.
70          String output = FuriaProperties.getProperty("test.db.output");
71          File outputDir = new File(output);
72                       
73         Directory.deleteDirectory(outputDir); 
74         assertTrue(outputDir.mkdirs());
75         fragmentDataSet("JPackageClass");
76         fragmentDataSet("JPackageClassObfuscatedSandMarkNoClassEnc");
77         fragmentDataSet("JPackageClassObfuscatedZelix");
78         
79         // now we perform matches with IRIndex and the results
80         // should be within the top 10.*/
81         
82         // first we create the database:
83         File furiaChanDBDir = new File(outputDir, "FuriaChanDB");
84         Directory.deleteDirectory(furiaChanDBDir);
85          engine = new FuriaChanEngine(furiaChanDBDir);      
86         
87         engine.insert(new File(outputDir, "JPackageClass"));
88         engine.freeze();
89         engine.close();
90         // got to reopen everything or lucene won't have anything refreshed.
91         engine = new FuriaChanEngine(furiaChanDBDir);   
92         engine.setValidate(true);
93         engine.setN((short)10);
94         engine.setR((short) 3);
95         logger.info("*** Matching base ***");
96         // performs the search. Makes sure all the items were found in the specified range
97         assertEquals(engine.search(new File(outputDir, "JPackageClass")), 1f);       
98         logger.info("*** Matching zelix ***");
99         assertEquals(engine.search(new File(outputDir, "JPackageClassObfuscatedZelix")), 1f);
100       // logger.info("*** Matching SandMark ***");
101        assertEquals(engine.search(new File(outputDir, "JPackageClassObfuscatedSandMarkNoClassEnc")), 1f);
102     }
103     
104     @After
105     public void closeEngine() throws Exception{
106         if(engine != null){
107             engine.close(); // close the engine so that everything will be refreshed.
108         }
109     }
110     
111     /**
112      * Fragments the given data set. The dataset name is prepended to the 
113      * test variable: test.db.input
114      * @param dataset
115      */
116     private void fragmentDataSet(String dataset) throws Exception{
117         String input = FuriaProperties.getProperty("test.db.input") + File.separator + dataset;
118         String output = FuriaProperties.getProperty("test.db.output")+ File.separator + dataset ;
119         // create the fragments!
120         File outputFile = new File(output);
121         Directory.deleteDirectory(outputFile);
122         FragmentBuilderClient c = new FragmentBuilderClient(true,
123                 new File(input), Runtime.getRuntime().availableProcessors(), outputFile, true,"asm");
124         // fragments were created.         
125     }
126 
127 }