1 /********************************************************************************
2 *InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 package test.base.jdbs.cryptography.symmetric;
21
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25
26 import junit.framework.TestCase;
27
28 import org.apache.log4j.Logger;
29 import org.apache.log4j.xml.DOMConfigurator;
30
31 import test.base.jdbs.AllTests;
32 import base.jdbs.cryptography.symmetric.DESSymmetricCipher;
33 import base.jdbs.cryptography.symmetric.SymmetricKey;
34 import base.util.FileUtil;
35
36 public class DESSymmetricCipherTest extends TestCase {
37
38 private DESSymmetricCipher DESCipher;
39
40 private File plainFile = new File(AllTests.REPOSITORY_PATH+File.separatorChar+"strings.txt");
41
42 private File encFile = new File(AllTests.REPOSITORY_PATH+File.separatorChar+"strings_enc.txt");
43
44 private File decFile = new File(AllTests.REPOSITORY_PATH+File.separatorChar+"strings_dec.txt");
45
46 private static final boolean RUN_SILENTLY = true;
47
48 private static final transient Logger logger = Logger
49 .getLogger(DESSymmetricCipherTest.class.getName());
50
51 public void setUp() throws IOException {
52 DOMConfigurator.configure("log4jConfiguration.xml");
53 DESCipher = new DESSymmetricCipher(new SymmetricKey("pippo"));
54
55 if (RUN_SILENTLY) {
56 plainFile.getParentFile().mkdirs();
57 plainFile.createNewFile();
58 encFile.createNewFile();
59 decFile.createNewFile();
60
61
62 FileWriter fileWriter = new FileWriter(plainFile);
63 for(int k=0;k<100;k++)
64 fileWriter.append("Line "+k+" : THIS IS A SIMPLE TEST FILE CONTENT!");
65 fileWriter.close();
66
67
68 } else {
69 javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
70 chooser.setDialogTitle("Select the file to be encrypted...");
71 chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
72 chooser.setApproveButtonText("Select");
73 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION)
74 plainFile = new File(chooser.getSelectedFile()
75 .getAbsolutePath());
76
77 chooser = new javax.swing.JFileChooser();
78 chooser.setDialogTitle("Select the encryption output file...");
79 chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
80 chooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
81 chooser.setApproveButtonText("Select");
82 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION)
83 encFile = new File(chooser.getSelectedFile().getAbsolutePath());
84
85 chooser = new javax.swing.JFileChooser();
86 chooser.setDialogTitle("Select the decryption output file...");
87 chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
88 chooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
89 chooser.setApproveButtonText("Select");
90 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION)
91 decFile = new File(chooser.getSelectedFile().getAbsolutePath());
92 }
93
94 }
95
96
97
98
99
100
101
102
103 public void test() throws IOException {
104
105 if (plainFile != null && encFile != null) {
106 DESCipher.encrypt(plainFile, encFile);
107 }
108
109 if (encFile != null && decFile != null) {
110 DESCipher.decrypt(encFile, decFile);
111 }
112
113 if (RUN_SILENTLY && plainFile.exists() && encFile.exists()
114 && decFile.exists()) {
115 logger.info("PlainFile " + plainFile + " Size: "
116 + FileUtil.fileSizeInByte(plainFile) + " Length: "
117 + FileUtil.fileContent(plainFile).length());
118 logger.info("EncryptedFile " + encFile + " Size: "
119 + FileUtil.fileSizeInByte(encFile) + " Length: "
120 + FileUtil.fileContent(encFile).length());
121 logger.info("DecryptedFile " + decFile + " Size: "
122 + FileUtil.fileSizeInByte(decFile) + " Length: "
123 + FileUtil.fileContent(decFile).length());
124
125 assertTrue(FileUtil.fileContent(plainFile).length() == FileUtil
126 .fileContent(decFile).length());
127 assertEquals(FileUtil.fileContent(plainFile), FileUtil
128 .fileContent(decFile));
129 assertTrue(FileUtil.fileSizeInByte(plainFile) == FileUtil
130 .fileSizeInByte(decFile));
131 }
132
133 }
134 }