1 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 39 40 public class HsqldbTest extends TestCase { 41 private String text1; 42 private String text2; 43 private String text3; 44 private String text4; 45 private String text5; 46 private String text6; 47 private String text7; 48 private String text8; 49 private String text9; 50 private String text10; 51 52 Connection conn = null; 53 Statement st = null; 54 ResultSet rs = null; 55 56 int i; 57 58 64 protected void setUp() throws ClassNotFoundException , 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 Class.forName("org.hsqldb.jdbcDriver"); 78 conn = DriverManager.getConnection("jdbc:hsqldb:.","sa",""); 79 80 st = conn.createStatement(); 82 rs = st.executeQuery("CREATE TABLE sample_table ( id INTEGER IDENTITY, str_col VARCHAR(500))"); 83 84 rs = st.executeQuery("CREATE TABLE sample_table_2 ( id INTEGER IDENTITY, str_col VARCHAR(500))"); 86 87 rs = st.executeQuery("CREATE TABLE sample_table_3 ( id INTEGER IDENTITY, str_col VARCHAR(500))"); 89 90 91 st.close(); 92 93 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 114 public void testDigest() throws Exception { 115 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 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 164 public void testEncryption() throws Exception { 165 Key.generatekey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("password")); 167 168 StringBuffer mykey = Hsqldb.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("password")); 169 170 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.blank(mykey); 187 188 StringBuffer mykey2 = Hsqldb.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("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.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 235 public void testPBEEncryption() throws Exception { 236 237 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 293 public void testHMAC() throws Exception { 294 295 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 310 public void testMAC() throws Exception { 311 StringBuffer mykey = Hsqldb.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer ("password")); 312 313 SafeObject k = new SafeObject(); 314 k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key", new StringBuffer ("password")); 315 316 317 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 (text1), new StringBuffer (rs.getString(1)), k)); 324 rs.close(); 325 st.close(); 326 } 327 328 333 protected void tearDown() throws SQLException { 334 conn.close(); 335 } 336 } 337 | Popular Tags |