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.util.Arrays;
10  
11  import org.apache.logging.log4j.LogManager;
12  import org.apache.logging.log4j.Logger;
13  import org.jastacry.GlobalData;
14  import org.jastacry.GlobalData.Returncode;
15  import org.jastacry.JaStaCry;
16  import org.jastacry.test.utils.Tooling;
17  import org.junit.jupiter.api.AfterEach;
18  import org.junit.jupiter.api.BeforeAll;
19  import org.junit.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.Test;
21  
22  import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
23  
24  
25  /**
26   * Test of Main function.
27   *
28   * @author Kai Kretschmann
29   *
30   */
31  public class TestMain
32  {
33      /**
34       * Maven test resources path.
35       */
36      private static final String RESOURCES = "src/test/resources/";
37  
38      /**
39       * log4j2 object.
40       */
41      private static Logger oLogger = null;
42  
43      /**
44       * Tooling functions object.
45       */
46      private Tooling tooling;
47  
48      /**
49       * Test configuration file, contains broad range of running layers. used for "OK" tests.
50       */
51      public static final String CONF1 = "conf1.cfg";
52  
53      /**
54       * Test configuration file, contains unknown tag for tests.
55       */
56      public static final String CONF2 = "conf2.cfg";
57  
58      /**
59       * Test configuration file, contains no layer at all.
60       */
61      public static final String CONF3 = "conf3.cfg";
62  
63      /**
64       * Test configuration file, contains two layers.
65       */
66      public static final String CONF4 = "conf4.cfg";
67  
68      /**
69       * Test configuration file, contains only one layers.
70       */
71      public static final String CONF5 = "conf5.cfg";
72  
73      /**
74       * Test configuration file, contains interactive password macro.
75       */
76      public static final String CONF6 = "conf6.cfg";
77  
78      /**
79       * Test input text file.
80       */
81      public static final String INPUTFILE = "plaintext.txt";
82  
83      /**
84       * Test input binary file.
85       */
86      public static final String INPUTBYTEFILE = "allbytes.dat";
87  
88      /**
89       * Test input encoded file.
90       */
91      public static final String INPUTENCODED = "encoded.dat";
92  
93      /**
94       * temporary file.
95       */
96      private File tmpFile; // NOPMD by kkretsch on 29.03.18 14:53
97  
98      /**
99       * temporary binary file.
100      */
101     private File binFile; // NOPMD by kkretsch on 29.03.18 14:53
102 
103     /**
104      * Encrypted file.
105      */
106     private File encFile; // NOPMD by kkretsch on 29.03.18 14:53
107 
108 
109     /**
110      * The BeforeClass method.
111      *
112      * @throws MalformedURLException
113      *             in case of error.
114      */
115     @BeforeAll
116     public static void setLogger() throws MalformedURLException
117     {
118         oLogger = LogManager.getLogger();
119     }
120 
121     /**
122      * Test Before method.
123      *
124      * @throws Exception
125      *             in case of error
126      */
127     @BeforeEach
128     public void setUp() throws Exception
129     {
130         tooling = new Tooling();
131         try
132         {
133             tmpFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.TMPEXT);
134             binFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.TMPEXT);
135             encFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.ENCEXT);
136         }
137         catch (final IOException e1)
138         {
139             oLogger.catching(e1);
140         }
141     }
142 
143     /**
144      * Test After method.
145      *
146      * @throws Exception
147      *             in case of error
148      */
149     @AfterEach
150     public void tearDown() throws Exception
151     {
152         encFile.delete();
153         binFile.delete();
154         tmpFile.delete();
155     }
156 
157     /**
158      * Test method help for Main function via static main.
159      *
160      */
161     @Test
162     @ExpectSystemExitWithStatus(2) // Returncode.RC_HELP
163     public void testMainStaticHelp()
164     {
165         final String[] sArguments = {
166             "-h"
167         };
168         JaStaCry.main(sArguments);
169     }
170 
171     /**
172      * Test method no parameters error for Main function via static main.
173      *
174      */
175     @Test
176     @ExpectSystemExitWithStatus(3) // Returncode.RC_ERROR
177     public void testMainStaticNoargs()
178     {
179         final String[] sArguments = {};
180         JaStaCry.main(sArguments);
181     }
182 
183     /**
184      * Test method two layer for Main function.
185      *
186      */
187     @Test
188     @ExpectSystemExitWithStatus(0) // Returncode.RC_OK
189     public void testMainStaticTwolayer()
190     {
191         final String sInputFile = RESOURCES + INPUTFILE;
192         final String sOutputFile = tmpFile.getAbsolutePath();
193         final String sConfigFile = RESOURCES + CONF4;
194 
195         final String[] sArguments = {
196             "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
197         };
198         JaStaCry.main(sArguments);
199     }
200 
201     /**
202      * Test method help for Main function.
203      *
204      */
205     @Test
206     public void testMainHelp()
207     {
208         final String[] sArguments = {
209             "-h"
210         };
211         final int returncode = JaStaCry.mainMethod(sArguments);
212         assertEquals("Main help returncode", Returncode.RC_HELP.getNumVal(), returncode);
213     }
214 
215     /**
216      * Test method no parameters error for Main function.
217      *
218      */
219     @Test
220     public void testMainNoargs()
221     {
222         final String[] sArguments = {};
223         final int returncode = JaStaCry.mainMethod(sArguments);
224         assertEquals("Main noargs returncode", Returncode.RC_ERROR.getNumVal(), returncode);
225     }
226 
227     /**
228      * Test method unknown parameters error for Main function.
229      *
230      */
231     @Test
232     public void testMainUnknownargs()
233     {
234         final String[] sArguments = {
235             "--unknown", "--arguments"
236         };
237         final int returncode = JaStaCry.mainMethod(sArguments);
238         assertEquals("Main unknown args returncode", Returncode.RC_ERROR.getNumVal(), returncode);
239     }
240 
241     /**
242      * Test method missing parameters for Main function.
243      *
244      */
245     @Test
246     public void testMainMissingArgs()
247     {
248         final String sInputFile = RESOURCES + INPUTFILE;
249         final String sOutputFile = tmpFile.getAbsolutePath();
250         final String sConfigFile = RESOURCES + CONF1;
251 
252         final String[] sArguments = {
253             "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
254         };
255         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
256         final int returncode = JaStaCry.mainMethod(sArguments);
257         assertEquals("Main missing args returncode", Returncode.RC_ERROR.getNumVal(), returncode);
258     }
259 
260     /**
261      * Test method missing parameters for Main function.
262      *
263      */
264     @Test
265     public void testMainMissingParams()
266     {
267         final String sInputFile = RESOURCES + INPUTFILE;
268         final String sOutputFile = tmpFile.getAbsolutePath();
269 
270         final String[] sArguments = {
271             "--infile", sInputFile, "--outfile", sOutputFile, "--conffile"
272         };
273         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
274         final int returncode = JaStaCry.mainMethod(sArguments);
275         assertEquals("Main missing args returncode", Returncode.RC_ERROR.getNumVal(), returncode);
276     }
277 
278     /**
279      * Test method encode call for Main function.
280      *
281      */
282     @Test
283     public void testMainEncode()
284     {
285         final String sInputFile = RESOURCES + INPUTFILE;
286         final String sOutputFile = encFile.getAbsolutePath();
287         final String sConfigFile = RESOURCES + CONF1;
288 
289         final String[] sArguments = {
290             "-v", "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
291         };
292         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
293         final int returncode = JaStaCry.mainMethod(sArguments);
294         assertEquals("Main encode returncode", 0, returncode);
295     }
296 
297     /**
298      * Test method decode call for Main function.
299      *
300      */
301     @Test
302     public void testMainDecode()
303     {
304         final String sInputFile = RESOURCES + INPUTENCODED;
305         final String sOutputFile = tmpFile.getAbsolutePath();
306         final String sConfigFile = RESOURCES + CONF1;
307 
308         final String[] sArguments = {
309             "--decode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
310         };
311         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
312         final int returncode = JaStaCry.mainMethod(sArguments);
313         assertEquals("Main decode returncode", 0, returncode);
314     }
315 
316     /**
317      * Test method short encode call for Main function.
318      *
319      */
320     @Test
321     public void testMainShortEncode()
322     {
323         final String sInputFile = RESOURCES + INPUTFILE;
324         final String sOutputFile = encFile.getAbsolutePath();
325         final String sConfigFile = RESOURCES + CONF1;
326 
327         final String[] sArguments = {
328             "-v", "-e", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
329         };
330         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
331         final int returncode = JaStaCry.mainMethod(sArguments);
332         assertEquals("Main encode returncode", 0, returncode);
333     }
334 
335     /**
336      * Test method decode call for Main function.
337      *
338      */
339     @Test
340     public void testMainShortDecode()
341     {
342         final String sInputFile = RESOURCES + INPUTENCODED;
343         final String sOutputFile = tmpFile.getAbsolutePath();
344         final String sConfigFile = RESOURCES + CONF1;
345 
346         final String[] sArguments = {
347             "-d", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
348         };
349         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
350         final int returncode = JaStaCry.mainMethod(sArguments);
351         assertEquals("Main decode returncode", 0, returncode);
352     }
353 
354     /**
355      * Test method unknown layer for Main function.
356      *
357      */
358     @Test
359     public void testMainUnknownLayer()
360     {
361         final String sInputFile = RESOURCES + INPUTFILE;
362         final String sOutputFile = tmpFile.getAbsolutePath();
363         final String sConfigFile = RESOURCES + CONF2;
364 
365         final String[] sArguments = {
366             "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
367         };
368         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
369         final int returncode = JaStaCry.mainMethod(sArguments);
370         assertEquals("Main one layer returncode", Returncode.RC_ERROR.getNumVal(), returncode);
371     }
372 
373     /**
374      * Test method one layer for Main function.
375      *
376      */
377     @Test
378     public void testMainOnelayer()
379     {
380         final String sInputFile = RESOURCES + INPUTFILE;
381         final String sOutputFile = tmpFile.getAbsolutePath();
382         final String sConfigFile = RESOURCES + CONF5;
383 
384         final String[] sArguments = {
385             "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
386         };
387         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
388         final int returncode = JaStaCry.mainMethod(sArguments);
389         assertEquals("Main one layer returncode", Returncode.RC_ERROR.getNumVal(), returncode);
390     }
391 
392     /**
393      * Test method two layer for Main function.
394      *
395      */
396     @Test
397     public void testMainTwolayer()
398     {
399         final String sInputFile = RESOURCES + INPUTFILE;
400         final String sOutputFile = tmpFile.getAbsolutePath();
401         final String sConfigFile = RESOURCES + CONF4;
402 
403         final String[] sArguments = {
404             "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
405         };
406         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
407         final int returncode = JaStaCry.mainMethod(sArguments);
408         assertEquals("Main two layer returncode", Returncode.RC_OK.getNumVal(), returncode);
409     }
410 
411     /**
412      * Test method two layer for Main function.
413      *
414      */
415     @Test
416     public void testMainPassword()
417     {
418         final String sInputFile = RESOURCES + INPUTFILE;
419         final String sOutputFile = tmpFile.getAbsolutePath();
420         final String sConfigFile = RESOURCES + CONF6;
421 
422         final String[] sArguments = {
423             "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
424         };
425         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
426         final int returncode = JaStaCry.mainMethod(sArguments);
427         assertEquals("Main two layer returncode", Returncode.RC_ERROR.getNumVal(), returncode);
428     }
429 
430     /**
431      * Test method no layer for Main function.
432      *
433      */
434     @Test
435     public void testMainNolayer()
436     {
437         final String sInputFile = RESOURCES + INPUTFILE;
438         final String sOutputFile = tmpFile.getAbsolutePath();
439         final String sConfigFile = RESOURCES + CONF3;
440 
441         final String[] sArguments = {
442             "-v", "--encode", "--infile", sInputFile, "--outfile", sOutputFile, "--conffile", sConfigFile
443         };
444         oLogger.info("Main test with args: {}", Arrays.toString(sArguments));
445         final int returncode = JaStaCry.mainMethod(sArguments);
446         assertEquals("Main no layer returncode", Returncode.RC_ERROR.getNumVal(), returncode);
447     }
448 
449     /**
450      * Test method missing input file for Main function.
451      *
452      */
453     @Test
454     public void testMainMissingInputFile()
455     {
456         final String sInputFile = RESOURCES + "NotExistingFile.txt";
457         final String sEncryptedFile = encFile.getAbsolutePath();
458         final String sConfigFile = RESOURCES + CONF1;
459 
460         final String[] sArgumentsEncrypt = {
461             "--encode", "--infile", sInputFile, "--outfile", sEncryptedFile, "--conffile", sConfigFile
462         };
463         oLogger.info("Main test encrypt with args: {}", Arrays.toString(sArgumentsEncrypt));
464         final int returncode = JaStaCry.mainMethod(sArgumentsEncrypt);
465         assertEquals("Main testMainMissingInputFile returncode", Returncode.RC_ERROR.getNumVal(), returncode);
466     }
467 
468     /**
469      * Test method missing config file for Main function.
470      *
471      */
472     @Test
473     public void testMainMissingConfigFile()
474     {
475         final String sInputFile = RESOURCES + INPUTFILE;
476         final String sEncryptedFile = encFile.getAbsolutePath();
477         final String sConfigFile = RESOURCES + "NotExistingConfig.txt";
478 
479         final String[] sArgumentsEncrypt = {
480             "--encode", "--infile", sInputFile, "--outfile", sEncryptedFile, "--conffile", sConfigFile
481         };
482         oLogger.info("Main test encrypt with args: {}", Arrays.toString(sArgumentsEncrypt));
483         final int returncode = JaStaCry.mainMethod(sArgumentsEncrypt);
484         assertEquals("Main testMainMissingConfigFile returncode", Returncode.RC_ERROR.getNumVal(), returncode);
485     }
486 
487     /**
488      * Test method normal for Main function.
489      *
490      */
491     @Test
492     public void testMainEncDec()
493     {
494         final String sInputFile = RESOURCES + INPUTFILE;
495         final String sEncryptedFile = encFile.getAbsolutePath();
496         final String sDecryptedFile = tmpFile.getAbsolutePath();
497         final String sConfigFile = RESOURCES + CONF1;
498         final File fInputfile = new File(sInputFile);
499         final File fEncryptedfile = new File(sEncryptedFile);
500         final File fDecryptedfile = new File(sDecryptedFile);
501 
502         final String[] sArgumentsEncrypt = {
503             "--encode", "--infile", sInputFile, "--outfile", sEncryptedFile, "--conffile", sConfigFile
504         };
505         oLogger.info("Main test encrypt with args: {}", Arrays.toString(sArgumentsEncrypt));
506         int returncode = JaStaCry.mainMethod(sArgumentsEncrypt);
507         assertEquals("Main ascencdec returncode", Returncode.RC_OK.getNumVal(), returncode);
508 
509         assertTrue("Encrypted data content", fEncryptedfile.length() > 0);
510 
511         final String[] sArgumentsDecrypt = {
512             "-v", "--decode", "--infile", sEncryptedFile, "--outfile", sDecryptedFile, "--conffile", sConfigFile
513         };
514         oLogger.info("Main test decrypt with args: {}", Arrays.toString(sArgumentsDecrypt));
515         returncode = JaStaCry.mainMethod(sArgumentsDecrypt);
516         assertEquals("Main ascdecend returncode", Returncode.RC_OK.getNumVal(), returncode);
517 
518         assertTrue("File results in equal content", tooling.compareFiles(fInputfile, fDecryptedfile));
519     }
520 
521     /**
522      * Test method normal for Main function including base64 encoding.
523      */
524     @Test
525     public void testMainBase64EncDec()
526     {
527         final String sInputFile = RESOURCES + INPUTFILE;
528         final String sEncryptedFile = encFile.getAbsolutePath();
529         final String sDecryptedFile = tmpFile.getAbsolutePath();
530         final String sConfigFile = RESOURCES + CONF4;
531         final File fInputfile = new File(sInputFile);
532         final File fEncryptedfile = new File(sEncryptedFile);
533         final File fDecryptedfile = new File(sDecryptedFile);
534 
535         final String[] sArgumentsEncrypt = {
536             "-e", "-t", "--infile", sInputFile, "--outfile", sEncryptedFile, "--conffile", sConfigFile
537         };
538         oLogger.info("Main test encrypt with args: {}", Arrays.toString(sArgumentsEncrypt));
539         int returncode = JaStaCry.mainMethod(sArgumentsEncrypt);
540         assertEquals("Main ascencdec returncode", Returncode.RC_OK.getNumVal(), returncode);
541 
542         assertTrue("Encrypted data content", fEncryptedfile.length() > 0);
543 
544         final String[] sArgumentsDecrypt = {
545             "-v", "-d", "--text", "--infile", sEncryptedFile, "--outfile", sDecryptedFile, "--conffile", sConfigFile
546         };
547         oLogger.info("Main test decrypt with args: {}", Arrays.toString(sArgumentsDecrypt));
548         returncode = JaStaCry.mainMethod(sArgumentsDecrypt);
549         assertEquals("Main ascdecend returncode", Returncode.RC_OK.getNumVal(), returncode);
550 
551         assertTrue("File results in equal content", tooling.compareFiles(fInputfile, fDecryptedfile));
552     }
553 
554     /**
555      * Test method normal for Main function including base64 encoding.
556      */
557     @Test
558     public void testMainBase64EncDecTexttwice()
559     {
560         final String sInputFile = RESOURCES + INPUTFILE;
561         final String sEncryptedFile = encFile.getAbsolutePath();
562         final String sDecryptedFile = tmpFile.getAbsolutePath();
563         final String sConfigFile = RESOURCES + CONF4;
564         final File fInputfile = new File(sInputFile);
565         final File fEncryptedfile = new File(sEncryptedFile);
566         final File fDecryptedfile = new File(sDecryptedFile);
567 
568         final String[] sArgumentsEncrypt = {
569             "-e", "-t", "--text", "--infile", sInputFile, "--outfile", sEncryptedFile, "--conffile", sConfigFile
570         };
571         oLogger.info("Main test encrypt with args: {}", Arrays.toString(sArgumentsEncrypt));
572         int returncode = JaStaCry.mainMethod(sArgumentsEncrypt);
573         assertEquals("Main ascencdec returncode", Returncode.RC_OK.getNumVal(), returncode);
574 
575         assertTrue("Encrypted data content", fEncryptedfile.length() > 0);
576 
577         final String[] sArgumentsDecrypt = {
578             "-v", "-d", "-t", "--text", "--infile", sEncryptedFile, "--outfile", sDecryptedFile, "--conffile", sConfigFile
579         };
580         oLogger.info("Main test decrypt with args: {}", Arrays.toString(sArgumentsDecrypt));
581         returncode = JaStaCry.mainMethod(sArgumentsDecrypt);
582         assertEquals("Main ascdecend returncode", Returncode.RC_OK.getNumVal(), returncode);
583 
584         assertTrue("File results in equal content", tooling.compareFiles(fInputfile, fDecryptedfile));
585     }
586 
587     /**
588      * Test method binary data for Main function.
589      *
590      */
591     @Test
592     public void testMainBinaryEncDec()
593     {
594         tooling.createBinaryTestfile(binFile);
595 
596         final String sInputFile = binFile.getAbsolutePath();
597         final String sEncryptedFile = encFile.getAbsolutePath();
598         final String sDecryptedFile = tmpFile.getAbsolutePath();
599         final String sConfigFile = RESOURCES + CONF1;
600         final File fInputfile = new File(sInputFile);
601         final File fEncryptedfile = new File(sEncryptedFile);
602         final File fDecryptedfile = new File(sDecryptedFile);
603 
604         final String[] sArgumentsEncrypt = {
605             "-v", "--encode", "--infile", sInputFile, "--outfile", sEncryptedFile, "--conffile", sConfigFile
606         };
607         oLogger.info("Main test encrypt with args: {}", Arrays.toString(sArgumentsEncrypt));
608         int returncode = JaStaCry.mainMethod(sArgumentsEncrypt);
609         assertEquals("Main binencdec returncode", Returncode.RC_OK.getNumVal(), returncode);
610 
611         assertTrue("Encrypted data content", fEncryptedfile.length() > 0);
612 
613         final String[] sArgumentsDecrypt = {
614             "-v", "--decode", "--infile", sEncryptedFile, "--outfile", sDecryptedFile, "--conffile", sConfigFile
615         };
616         oLogger.info("Main test decrypt with args: {}", Arrays.toString(sArgumentsDecrypt));
617         returncode = JaStaCry.mainMethod(sArgumentsDecrypt);
618         assertEquals("Main bindecenc returncode", Returncode.RC_OK.getNumVal(), returncode);
619 
620         assertTrue("File results in equal content", tooling.compareFiles(fInputfile, fDecryptedfile));
621     }
622 
623 }