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  
18  import org.junit.jupiter.api.AfterEach;
19  import org.junit.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.BeforeAll;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Test of Main function.
25   *
26   * @author Kai Kretschmann
27   *
28   */
29  public class TestConfig
30  {
31      /**
32       * Maven test resources path.
33       */
34      private static final String RESOURCES = "src/test/resources/";
35  
36      /**
37       * log4j2 object.
38       */
39      private static Logger oLogger;
40  
41      /**
42       * Tooling functions object.
43       */
44      private Tooling tooling;
45  
46      /**
47       * Test configuration file, UTF8 encoded with BOM.
48       */
49      public static final String CONF_UTFBOM = "conf_withbom.cfg";
50  
51      /**
52       * Test configuration file, UTF8 encoded without BOM.
53       */
54      public static final String CONF_UTFNOBOM = "conf_withoutbom.cfg";
55  
56      /**
57       * Test configuration file, ISO encoded and for sure no BOM.
58       */
59      public static final String CONF_ISO = "conf_withiso.cfg";
60  
61      /**
62       * Test input text file.
63       */
64      public static final String INPUTFILE = "plaintext.txt";
65  
66      /**
67       * Test input binary file.
68       */
69      public static final String INPUTBYTEFILE = "allbytes.dat";
70  
71      /**
72       * Test input encoded file.
73       */
74      public static final String INPUTENCODED = "encoded.dat";
75  
76      /**
77       * temporary file.
78       */
79      private File tmpFile;
80  
81      /**
82       * temporary binary file.
83       */
84      private File binFile;
85  
86      /**
87       * Encrypted file.
88       */
89      private File encFile;
90  
91      /**
92       * The BeforeClass method.
93       *
94       * @throws MalformedURLException
95       *             in case of error.
96       */
97      @BeforeAll
98      public static void setLogger() throws MalformedURLException
99      {
100         oLogger = LogManager.getLogger();
101     }
102 
103     /**
104      * Test Before method.
105      *
106      * @throws Exception
107      *             in case of error
108      */
109     @BeforeEach
110     public void setUp() throws Exception
111     {
112         tooling = new Tooling();
113         try
114         {
115             tmpFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.TMPEXT);
116             binFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.TMPEXT);
117             encFile = File.createTempFile(org.jastacry.GlobalData.TMPBASE, GlobalData.ENCEXT);
118         }
119         catch (final IOException e1)
120         {
121             oLogger.catching(e1);
122         }
123     }
124 
125     /**
126      * Test After method.
127      *
128      * @throws Exception
129      *             in case of error
130      */
131     @AfterEach
132     public void tearDown() throws Exception
133     {
134         encFile.delete();
135         binFile.delete();
136         tmpFile.delete();
137     }
138 
139     private void testGivenConfig(final String sConfig)
140     {
141         final String sInputFile = RESOURCES + INPUTFILE;
142         final String sEncryptedFile = encFile.getAbsolutePath();
143         final String sDecryptedFile = tmpFile.getAbsolutePath();
144         final String sConfigFile = RESOURCES + sConfig;
145         final File fInputfile = new File(sInputFile);
146         final File fEncryptedfile = new File(sEncryptedFile);
147         final File fDecryptedfile = new File(sDecryptedFile);
148 
149         final String[] sArgumentsEncrypt = {
150             "--encode", "--infile", sInputFile, "--outfile", sEncryptedFile, "--conffile", sConfigFile
151         };
152         oLogger.info("Main test encrypt with args: {}", Arrays.toString(sArgumentsEncrypt));
153         int returncode = JaStaCry.mainMethod(sArgumentsEncrypt);
154         assertEquals("Main ascencdec returncode", Returncode.RC_OK.getNumVal(), returncode);
155 
156         assertTrue("Encrypted data content", fEncryptedfile.length() > 0);
157 
158         final String[] sArgumentsDecrypt = {
159             "-v", "--decode", "--infile", sEncryptedFile, "--outfile", sDecryptedFile, "--conffile", sConfigFile
160         };
161         oLogger.info("Main test decrypt with args: {}", Arrays.toString(sArgumentsDecrypt));
162         returncode = JaStaCry.mainMethod(sArgumentsDecrypt);
163         assertEquals("Main ascdecend returncode", Returncode.RC_OK.getNumVal(), returncode);
164 
165         assertTrue("File results in equal content", tooling.compareFiles(fInputfile, fDecryptedfile));
166     }
167 
168     /**
169      * Test method normal for Main function.
170      *
171      */
172     @Test
173     public void testConfWithBom()
174     {
175         testGivenConfig(CONF_UTFBOM);
176     }
177 
178     /**
179      * Test method normal for Main function.
180      *
181      */
182     @Test
183     public void testConfWithoutBom()
184     {
185         testGivenConfig(CONF_UTFNOBOM);
186     }
187 
188     /**
189      * Test method normal for Main function.
190      *
191      */
192     @Test
193     public void testConfIso()
194     {
195         testGivenConfig(CONF_ISO);
196     }
197 
198 }