KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: net.sourceforge.lightcrypto.test.StreamTest
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.TestCase;
24 import junit.framework.Assert;
25
26 import java.io.*;
27
28 import net.sourceforge.lightcrypto.SafeObject;
29 import net.sourceforge.lightcrypto.Key;
30 import net.sourceforge.lightcrypto.Stream;
31
32 /**
33  * A collection of stream cipher tests
34  * <P>
35  * These tests can be run using JUnit (http://www.junit.org)
36  *
37  * @author Gert Van Ham
38  * @author hamgert@users.sourceforge.net
39  * @author http://jcetaglib.sourceforge.net
40  * @version $Id: StreamTest.java,v 1.1 2003/10/28 20:12:54 hamgert Exp $
41  */

42 public class StreamTest extends TestCase {
43      /**
44      * setup test
45      *
46      * @throws java.io.IOException
47      */

48     protected void setUp() throws IOException {
49         // create text file
50
FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt");
51         DataOutputStream dataStr = new DataOutputStream(outStr);
52
53         dataStr.writeBytes("This is a readable string inside a file");
54
55         dataStr.flush();
56         dataStr.close();
57
58         outStr.close();
59     }
60
61     /**
62     * test file encryption
63     *
64     * @throws Exception
65     */

66    public void testStream() throws Exception JavaDoc {
67         // generate a key
68
Key.generatekey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
69
70         SafeObject k = new SafeObject();
71         k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
72
73         ByteArrayOutputStream bao = new ByteArrayOutputStream();
74         DataOutputStream dao = new DataOutputStream(bao);
75         ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
76         DataOutputStream dao2 = new DataOutputStream(bao2);
77
78         Stream.encrypt(new FileInputStream(RunTest.TEMPFOLDER + "readable.txt"),dao,k,64);
79         Stream.decrypt(new ByteArrayInputStream(bao.toByteArray()),dao2,k,64);
80
81
82         Assert.assertEquals("This is a readable string inside a file", new String JavaDoc(bao2.toByteArray()));
83
84         dao.close();
85         dao2.close();
86     }
87
88 }
89
Popular Tags