KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > lightcrypto > test > HsqldbTest


1 /*
2   Name: net.sourceforge.lightcrypto.test.HsqldbTest
3   Licensing: LGPL (lesser GNU Public License)
4   API: Bouncy Castle (http://www.bouncycastle.org) lightweight API
5
6   Disclaimer:
7
8   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
9   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
10   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
11   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
12   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
13   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
14   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
15   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
16
17   (C) Copyright 2003 Gert Van Ham
18
19 */

20
21 package net.sourceforge.lightcrypto.test;
22
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import net.sourceforge.lightcrypto.*;
26
27 import java.sql.*;
28
29 /**
30  * A collection of encryption tests for HSQLDB (http://hsqldb.sourceforge.net)
31  * <P>
32  * These tests can be run using JUnit (http://www.junit.org)
33  *
34  * @author Gert Van Ham
35  * @author hamgert@users.sourceforge.net
36  * @author http://jcetaglib.sourceforge.net
37  * @version $Id: HsqldbTest.java,v 1.3 2003/10/28 20:12:54 hamgert Exp $
38  */

39
40 public class HsqldbTest extends TestCase {
41      private String JavaDoc text1;
42      private String JavaDoc text2;
43      private String JavaDoc text3;
44      private String JavaDoc text4;
45      private String JavaDoc text5;
46      private String JavaDoc text6;
47      private String JavaDoc text7;
48      private String JavaDoc text8;
49      private String JavaDoc text9;
50      private String JavaDoc text10;
51
52     Connection conn = null;
53     Statement st = null;
54     ResultSet rs = null;
55
56     int i;
57
58     /**
59      * setup test (create database)
60      *
61      * @throws ClassNotFoundException
62      * @throws SQLException
63      */

64     protected void setUp() throws ClassNotFoundException JavaDoc, SQLException {
65         text1= "The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms";
66         text2= "This software is distributed under a license based on the MIT X Consortium license";
67         text3= "found in $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE distribution";
68         text4= "Mit Project 2002 zum erfolgreichen Projektmanagement Damit Sie in Zukunft Ihre Projekte präzise und komfortabel steuern können";
69         text5="En av de största nyheterna är att det finns en .NET Enterprise Server-lösning för stora företagsomspännade projekt";
70         text6= "Lees de productinformatie en ontdek alles over de krachtige tools binnen Visual Studio .NET";
71         text7= "Vergeet even die oude tovenaars met puntige hoeden en rondborstige jonkvrouwen in nood... oké, vergeet in ieder geval even die tovenaars, want Lionheart komt met een ambitieuze rollenspelvariant";
72         text8= "An implementation of ECIES (stream mode) as described in IEEE P 1363a.";
73         text9= "This makes the entire keystore resistant to tampering and inspection, and forces verification";
74         text10= "application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature";
75
76         // create an in-process, in-memory database
77
Class.forName("org.hsqldb.jdbcDriver");
78         conn = DriverManager.getConnection("jdbc:hsqldb:.","sa","");
79
80         // create the table
81
st = conn.createStatement();
82         rs = st.executeQuery("CREATE TABLE sample_table ( id INTEGER IDENTITY, str_col VARCHAR(500))");
83
84         // create second table
85
rs = st.executeQuery("CREATE TABLE sample_table_2 ( id INTEGER IDENTITY, str_col VARCHAR(500))");
86
87         // create third table
88
rs = st.executeQuery("CREATE TABLE sample_table_3 ( id INTEGER IDENTITY, str_col VARCHAR(500))");
89
90
91         st.close();
92
93         // fill table
94
st = conn.createStatement();
95         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text1 + "')");
96         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text2 + "')");
97         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text3 + "')");
98         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text4 + "')");
99         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text5 + "')");
100         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text6 + "')");
101         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text7 + "')");
102         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text8 + "')");
103         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text9 + "')");
104         i = st.executeUpdate("INSERT INTO sample_table(str_col) VALUES('" + text10 + "')");
105
106         st.close();
107     }
108
109     /**
110      * test digest
111      *
112      * @throws Exception
113      */

114     public void testDigest() throws Exception JavaDoc {
115         // query table
116
st = conn.createStatement();
117         rs = st.executeQuery("SELECT \"net.sourceforge.lightcrypto.Hsqldb.digest\"(str_col) as digest FROM sample_table ORDER BY id");
118
119         rs.next();
120         Assert.assertEquals(rs.getString(1),"GPMnLEpblugEcs2kmFkg3Q==");
121
122         rs.next();
123         Assert.assertEquals(rs.getString(1),"LPcC8hTqwv/qBcDUQdpx4w==");
124
125         rs.next();
126         Assert.assertEquals(rs.getString(1),"1pWkqCss4OvVPCv0fcSBgQ==");
127
128         rs.next();
129         Assert.assertEquals(rs.getString(1),"6kqKaRJHN1an+j+u2fUvHA==");
130
131         rs.next();
132         Assert.assertEquals(rs.getString(1),"KJ+Ve5/s8HDv/xu49lsf3g==");
133
134         rs.next();
135         Assert.assertEquals(rs.getString(1),"a7syvGijznXELm/sOctixw==");
136
137         rs.next();
138         Assert.assertEquals(rs.getString(1),"Pw3X59NMqZEYaOyKDPXn1g==");
139
140         rs.next();
141         Assert.assertEquals(rs.getString(1),"uA+eNem45Shm+4SWImwFDw==");
142
143         rs.next();
144         Assert.assertEquals(rs.getString(1),"jMU70cRMB3LnWkWrWhczBg==");
145
146         rs.next();
147         Assert.assertEquals(rs.getString(1),"BytgzY4DVd4pFgyQLK1rMw==");
148
149         st.close();
150
151         // try another algorithm
152
rs = st.executeQuery("SELECT \"net.sourceforge.lightcrypto.Hsqldb.digest\"(str_col,'SHA1') as digest FROM sample_table WHERE id = 5");
153
154         rs.next();
155         Assert.assertEquals(rs.getString(1),"Y15d+lCFfNBbucduGH+rvoweQqw=");
156
157     }
158
159     /**
160      * test encryption
161      *
162      * @throws Exception
163      */

164     public void testEncryption() throws Exception JavaDoc {
165         // generate a key
166
Key.generatekey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
167
168         StringBuffer JavaDoc mykey = Hsqldb.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
169
170         // fill table
171
st = conn.createStatement();
172         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text1 + "','" + mykey + "'))");
173         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text2 + "','" + mykey + "'))");
174         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text3 + "','" + mykey + "'))");
175         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text4 + "','" + mykey + "'))");
176         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text5 + "','" + mykey + "'))");
177         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text6 + "','" + mykey + "'))");
178         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text7 + "','" + mykey + "'))");
179         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text8 + "','" + mykey + "'))");
180         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text9 + "','" + mykey + "'))");
181         i = st.executeUpdate("INSERT INTO sample_table_2(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.encrypt\"('" + text10 + "','" + mykey + "'))");
182
183         st.close();
184
185         // clean key after usage
186
Clean.blank(mykey);
187
188         // load key again
189
StringBuffer JavaDoc mykey2 = Hsqldb.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
190
191         st = conn.createStatement();
192         rs = st.executeQuery("SELECT \"net.sourceforge.lightcrypto.Hsqldb.decrypt\"(str_col,'" + mykey2 + "') FROM sample_table_2 ORDER BY id");
193
194         // clean key after usage
195
Clean.blank(mykey2);
196
197         rs.next();
198         Assert.assertEquals(rs.getString(1),text1);
199
200         rs.next();
201         Assert.assertEquals(rs.getString(1),text2);
202
203         rs.next();
204         Assert.assertEquals(rs.getString(1),text3);
205
206         rs.next();
207         Assert.assertEquals(rs.getString(1),text4);
208
209         rs.next();
210         Assert.assertEquals(rs.getString(1),text5);
211
212         rs.next();
213         Assert.assertEquals(rs.getString(1),text6);
214
215         rs.next();
216         Assert.assertEquals(rs.getString(1),text7);
217
218         rs.next();
219         Assert.assertEquals(rs.getString(1),text8);
220
221         rs.next();
222         Assert.assertEquals(rs.getString(1),text9);
223
224         rs.next();
225         Assert.assertEquals(rs.getString(1),text10);
226
227         st.close();
228     }
229
230      /**
231      * test encryption
232      *
233      * @throws Exception
234      */

235     public void testPBEEncryption() throws Exception JavaDoc {
236
237         // fill table
238
st = conn.createStatement();
239         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text1 + "','mypassword'))");
240         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text2 + "','mypassword'))");
241         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text3 + "','mypassword'))");
242         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text4 + "','mypassword'))");
243         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text5 + "','mypassword'))");
244         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text6 + "','mypassword'))");
245         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text7 + "','mypassword'))");
246         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text8 + "','mypassword'))");
247         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text9 + "','mypassword'))");
248         i = st.executeUpdate("INSERT INTO sample_table_3(str_col) VALUES(\"net.sourceforge.lightcrypto.Hsqldb.PBEEncrypt\"('" + text10 + "','mypassword'))");
249
250         st.close();
251
252         st = conn.createStatement();
253         rs = st.executeQuery("SELECT \"net.sourceforge.lightcrypto.Hsqldb.PBEDecrypt\"(str_col,'mypassword') FROM sample_table_3 ORDER BY id");
254
255         rs.next();
256         Assert.assertEquals(rs.getString(1),text1);
257
258         rs.next();
259         Assert.assertEquals(rs.getString(1),text2);
260
261         rs.next();
262         Assert.assertEquals(rs.getString(1),text3);
263
264         rs.next();
265         Assert.assertEquals(rs.getString(1),text4);
266
267         rs.next();
268         Assert.assertEquals(rs.getString(1),text5);
269
270         rs.next();
271         Assert.assertEquals(rs.getString(1),text6);
272
273         rs.next();
274         Assert.assertEquals(rs.getString(1),text7);
275
276         rs.next();
277         Assert.assertEquals(rs.getString(1),text8);
278
279         rs.next();
280         Assert.assertEquals(rs.getString(1),text9);
281
282         rs.next();
283         Assert.assertEquals(rs.getString(1),text10);
284
285         st.close();
286     }
287
288     /**
289      * test HMAC
290      *
291      * @throws Exception
292      */

293     public void testHMAC() throws Exception JavaDoc {
294
295         // query table
296
st = conn.createStatement();
297         rs = st.executeQuery("SELECT \"net.sourceforge.lightcrypto.Hsqldb.hmac\"(str_col) as mac FROM sample_table ORDER BY id");
298
299         rs.next();
300         Assert.assertEquals(rs.getString(1),"PhX3UR5UV8g2JSwzaIMhmbbGzcs=");
301
302         st.close();
303     }
304
305     /**
306      * test MAC
307      *
308      * @throws Exception
309      */

310     public void testMAC() throws Exception JavaDoc {
311         StringBuffer JavaDoc mykey = Hsqldb.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
312
313         SafeObject k = new SafeObject();
314         k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key", new StringBuffer JavaDoc("password"));
315
316
317         // query table
318
st = conn.createStatement();
319         rs = st.executeQuery("SELECT \"net.sourceforge.lightcrypto.Hsqldb.mac\"(str_col,'" + mykey + "') as mac FROM sample_table ORDER BY id");
320
321         rs.next();
322
323         Assert.assertTrue(Macs.macEquals(new StringBuffer JavaDoc(text1), new StringBuffer JavaDoc(rs.getString(1)), k));
324         rs.close();
325         st.close();
326     }
327
328     /**
329      * runs a the end of the tests (close database)
330      *
331      * @throws SQLException
332      */

333     protected void tearDown() throws SQLException {
334         conn.close();
335     }
336 }
337
Popular Tags