1 21 22 package net.sourceforge.jcetaglib.test; 23 24 import junit.framework.Assert; 25 import junit.framework.TestCase; 26 import net.sourceforge.jcetaglib.lib.PBECrypt; 27 28 import java.io.*; 29 30 40 public class PBETest extends TestCase { 41 private StringBuffer ciphertext = null; 42 private StringBuffer plaintext = null; 43 44 49 protected void setUp() throws IOException { 50 FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt"); 52 DataOutputStream dataStr = new DataOutputStream(outStr); 53 54 dataStr.writeBytes("This is a readable string inside a file"); 55 56 dataStr.flush(); 57 dataStr.close(); 58 59 outStr.close(); 60 } 61 62 67 public void testPBE() throws Exception { 68 for (int i = 0; i < RunTest.pbealg.length; i++) { 69 for (int j = 0; j < RunTest.text.length; j++) { 70 ciphertext = PBECrypt.encrypt(RunTest.text[j], new StringBuffer ("password"), RunTest.pbealg[i]); 71 plaintext = PBECrypt.decrypt(ciphertext, new StringBuffer ("password"), RunTest.pbealg[i]); 72 73 Assert.assertEquals(plaintext.toString(), RunTest.text[j].toString()); 74 } 75 } 76 } 77 78 83 public void testFilePBE() throws Exception { 84 for (int i = 0; i < RunTest.pbealg.length; i++) { 85 PBECrypt.encryptFile(RunTest.TEMPFOLDER + "readable.txt", RunTest.TEMPFOLDER + "readable.txt.encrypted", new StringBuffer ("password"), RunTest.pbealg[i]); 86 PBECrypt.decryptFile(RunTest.TEMPFOLDER + "readable.txt.encrypted", RunTest.TEMPFOLDER + "readable.txt.decrypted", new StringBuffer ("password"), RunTest.pbealg[i]); 87 88 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted"))); 90 91 StringBuffer line = new StringBuffer (); 92 int c; 93 94 while ((c = reader.read()) != -1) { 95 line.append((char) c); 96 } 97 98 reader.close(); 99 100 String t = line.toString(); 101 102 Assert.assertEquals("This is a readable string inside a file", t); 103 } 104 } 105 } 106 | Popular Tags |