KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: DigestTest.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.Digesters;
27
28 import java.io.DataOutputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33  * A collection of digesters (hash) 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: DigestTest.java,v 1.1 2003/12/28 19:06:57 hamgert Exp $
41  */

42 public class DigestTest extends TestCase {
43     private StringBuffer JavaDoc digest1 = null;
44     private StringBuffer JavaDoc digest2 = null;
45
46     /**
47      * setup test
48      *
49      * @throws java.io.IOException
50      */

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

69     public void testDigest() throws Exception JavaDoc {
70         for (int i = 0; i < RunTest.digestalg.length; i++) {
71             for (int j = 0; j < RunTest.text.length; j++) {
72                 digest1 = Digesters.hash(RunTest.text[j], RunTest.digestalg[i]);
73                 digest2 = Digesters.hash(RunTest.text[j], RunTest.digestalg[i]);
74
75                 Assert.assertEquals(digest1.toString(), digest2.toString());
76             }
77         }
78
79     }
80
81
82     /**
83      * test file digesters
84      *
85      * @throws Exception
86      */

87     public void testFileDigest() throws Exception JavaDoc {
88         for (int i = 0; i < RunTest.digestalg.length; i++) {
89             digest1 = Digesters.hashFile(RunTest.TEMPFOLDER + "readable.txt", RunTest.digestalg[i]);
90             digest2 = Digesters.hashFile(RunTest.TEMPFOLDER + "readable.txt", RunTest.digestalg[i]);
91
92             Assert.assertEquals(digest1.toString(), digest2.toString());
93         }
94     }
95
96     /**
97      * test form digest
98      *
99      * @throws Exception
100      */

101     public void testFormDigest() throws Exception JavaDoc {
102         for (int i = 0; i < RunTest.digestalg.length; i++) {
103             for (int j = 0; j < RunTest.alg.length; j++) {
104                 String JavaDoc keyfile = RunTest.TEMPFOLDER + RunTest.alg[j][0] + "_" + RunTest.alg[j][1] + ".key";
105                 digest1 = Digesters.formDigest(new StringBuffer JavaDoc("field1=a&field2=b"), RunTest.digestalg[i], keyfile, new StringBuffer JavaDoc("password"), RunTest.alg[j][0]);
106                 digest2 = Digesters.formDigest(new StringBuffer JavaDoc("field1=a&field2=b"), RunTest.digestalg[i], keyfile, new StringBuffer JavaDoc("password"), RunTest.alg[j][0]);
107
108                 Assert.assertEquals(digest1.toString(), digest2.toString());
109             }
110         }
111     }
112 }
113
Popular Tags