KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > test > PBETest


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

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 /**
31  * A collection of PBE encryption/decryption tests
32  * <P>
33  * These tests can be run using JUnit (http://www.junit.org)
34  *
35  * @author Gert Van Ham
36  * @author hamgert@users.sourceforge.net
37  * @author http://jcetaglib.sourceforge.net
38  * @version $Id: PBETest.java,v 1.1 2003/12/28 19:06:57 hamgert Exp $
39  */

40 public class PBETest extends TestCase {
41     private StringBuffer JavaDoc ciphertext = null;
42     private StringBuffer JavaDoc plaintext = null;
43
44     /**
45      * setup test
46      *
47      * @throws java.io.IOException
48      */

49     protected void setUp() throws IOException {
50         // create text file
51
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     /**
63      * test PBE encryption
64      *
65      * @throws Exception
66      */

67     public void testPBE() throws Exception JavaDoc {
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 JavaDoc("password"), RunTest.pbealg[i]);
71                 plaintext = PBECrypt.decrypt(ciphertext, new StringBuffer JavaDoc("password"), RunTest.pbealg[i]);
72
73                 Assert.assertEquals(plaintext.toString(), RunTest.text[j].toString());
74             }
75         }
76     }
77
78     /**
79      * test file PBE encryption
80      *
81      * @throws Exception
82      */

83     public void testFilePBE() throws Exception JavaDoc {
84         for (int i = 0; i < RunTest.pbealg.length; i++) {
85             PBECrypt.encryptFile(RunTest.TEMPFOLDER + "readable.txt", RunTest.TEMPFOLDER + "readable.txt.encrypted", new StringBuffer JavaDoc("password"), RunTest.pbealg[i]);
86             PBECrypt.decryptFile(RunTest.TEMPFOLDER + "readable.txt.encrypted", RunTest.TEMPFOLDER + "readable.txt.decrypted", new StringBuffer JavaDoc("password"), RunTest.pbealg[i]);
87
88             // read the file
89
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt.decrypted")));
90
91             StringBuffer JavaDoc line = new StringBuffer JavaDoc();
92             int c;
93
94             while ((c = reader.read()) != -1) {
95                 line.append((char) c);
96             }
97
98             reader.close();
99
100             String JavaDoc t = line.toString();
101
102             Assert.assertEquals("This is a readable string inside a file", t);
103         }
104     }
105 }
106
Popular Tags