View Javadoc
1   package org.jastacry.test;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.net.MalformedURLException;
9   import java.nio.file.Files;
10  import java.nio.file.Path;
11  import java.nio.file.Paths;
12  import java.util.Arrays;
13  
14  import org.apache.logging.log4j.LogManager;
15  import org.apache.logging.log4j.Logger;
16  import org.jastacry.GlobalData;
17  import org.jastacry.JaStaCry;
18  import org.jastacry.test.utils.ShannonEntropy;
19  import org.jastacry.test.utils.Tooling;
20  
21  import org.junit.jupiter.api.AfterEach;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.BeforeAll;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test of entropy.
28   *
29   * @author Kai Kretschmann
30   *
31   */
32  public class TestEntropy
33  {
34      /**
35       * Maven temp resources path.
36       */
37      private static final String TMPRESOURCES = "target/";
38  
39      /**
40       * Maven test resources path.
41       */
42      private static final String RESOURCES = "src/test/resources/";
43  
44      /**
45       * log4j2 object.
46       */
47      private static Logger oLogger = null;
48  
49      /**
50       * Tooling functions object.
51       */
52      private static Tooling tooling = null;
53  
54      /**
55       * Tooling functions object.
56       */
57      private static ShannonEntropy shannon = null;
58  
59      /**
60       * Test configuration file, contains broad range of running layers. used for "OK" tests.
61       */
62      public static final String CONF1 = "conf1.cfg";
63  
64      /**
65       * Test input text file.
66       */
67      public static final String INPUTFILE = "plaintext.txt";
68  
69      /**
70       * Test input binary file with all values.
71       */
72      public static final String INPUTBYTEFILE = "allbytes.dat";
73  
74      /**
75       * Test input binary file with one value.
76       */
77      public static final String INPUTREPEATFILE = "onebyte.dat";
78  
79      /**
80       * Test input encoded file.
81       */
82      public static final String INPUTENCODED = "encoded.dat";
83  
84      /**
85       * temporary file.
86       */
87      private File tmpFile;
88  
89      /**
90       * temporary binary file.
91       */
92      private static File allbinFile;
93  
94      /**
95       * temporary binary file.
96       */
97      private static File onebinFile;
98  
99      /**
100      * Encrypted file.
101      */
102     private File encFile;
103 
104     /**
105      * The BeforeClass method.
106      *
107      * @throws MalformedURLException
108      *             in case of error.
109      */
110     @BeforeAll
111     public static void setupData() throws MalformedURLException
112     {
113         oLogger = LogManager.getLogger();
114         tooling = new Tooling();
115         shannon = new ShannonEntropy();
116 
117         allbinFile = new File(TMPRESOURCES + INPUTBYTEFILE);
118         tooling.createBinaryTestfile(allbinFile, 1024, (byte) 0x20);
119 
120         onebinFile = new File(TMPRESOURCES + INPUTREPEATFILE);
121         tooling.createBinaryTestfile(onebinFile);
122     }
123 
124     /**
125      * Test Before method.
126      *
127      * @throws Exception
128      *             in case of error
129      */
130     @BeforeEach
131     public void setUp() throws Exception
132     {
133 
134         try
135         {
136             tmpFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.TMPEXT);
137             encFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.ENCEXT);
138         }
139         catch (final IOException e1)
140         {
141             oLogger.catching(e1);
142         }
143     }
144 
145     /**
146      * Test After method.
147      *
148      * @throws Exception
149      *             in case of error
150      */
151     @AfterEach
152     public void tearDown() throws Exception
153     {
154         encFile.delete();
155         tmpFile.delete();
156     }
157 
158     /**
159      * Test method plain string.
160      */
161     @Test
162     public void testEntropyZero()
163     {
164         String sSimple = "aaaaaaaaaa";
165         shannon.calculate(sSimple);
166         double entropy = shannon.getEntropy();
167         oLogger.info("testEntropyZero Entropy: {}", entropy);
168         assertTrue("Entropy", entropy == 0);
169     }
170 
171     /**
172      * Test method mixed string.
173      *
174      */
175     @Test
176     public void testEntropyNonzero()
177     {
178         String sSimple = "abcdefgh";
179         shannon.calculate(sSimple);
180         double entropy = shannon.getEntropy();
181         oLogger.info("testEntropyNonzero Entropy: {}", entropy);
182         assertTrue("Entropy", entropy > 0);
183     }
184 
185     /**
186      * Test method mixed byte array.
187      *
188      */
189     @Test
190     public void testEntropyBytes()
191     {
192         String sSimple = "abcdefgh";
193         byte[] byteArr = sSimple.getBytes();
194         shannon.calculate(byteArr);
195         double entropy = shannon.getEntropy();
196         oLogger.info("testEntropyBytes Entropy: {}", entropy);
197         assertTrue("Entropy", entropy > 0);
198     }
199 
200     /**
201      * Test method mixed byte array.
202      *
203      */
204     @Test
205     public void testEntropyStringEqualsBytes()
206     {
207         String sSimple = "abcdefgh";
208 
209         shannon.calculate(sSimple);
210         double entropyString = shannon.getEntropy();
211 
212         byte[] byteArr = sSimple.getBytes();
213         shannon.calculate(byteArr);
214         double entropyBytes = shannon.getEntropy();
215         oLogger.info("testEntropyStringEqualsBytes Entropy: {} & {}", entropyString, entropyBytes);
216         assertTrue("Entropy equals", entropyString == entropyBytes);
217     }
218 
219     /**
220      * Test method encode call for Main function.
221      *
222      */
223     @Test
224     public void testEntropyMainEncode()
225     {
226         final String sInputFile = RESOURCES + INPUTFILE;
227         final String sOutputFile = encFile.getAbsolutePath();
228         final String sConfigFile = RESOURCES + CONF1;
229 
230         final String[] sArguments = {
231             "-v", "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
232         };
233         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
234         final int iRc = JaStaCry.mainMethod(sArguments);
235         assertEquals("Main encode returncode", 0, iRc);
236 
237         // Test entropy values
238         try
239         {
240             Path pathInput = Paths.get(sInputFile);
241             byte[] dataInput = Files.readAllBytes(pathInput);
242             shannon.calculate(dataInput);
243             double entropyInput = shannon.getEntropy();
244 
245             Path pathOutput = Paths.get(sOutputFile);
246             byte[] dataOutput = Files.readAllBytes(pathOutput);
247             shannon.calculate(dataOutput);
248             double entropyOutput = shannon.getEntropy();
249 
250             oLogger.info("testMainEncode Entropy: {} to {}", entropyInput, entropyOutput);
251 
252             assertTrue("Entropy input above zero", entropyInput > 0);
253             assertTrue("Entropy output above zero", entropyOutput > 0);
254             assertTrue("Entropy getting better", entropyInput < entropyOutput);
255         }
256         catch (IOException e)
257         {
258             oLogger.catching(e);
259         }
260     }
261 }