KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: MacTest.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.Macs;
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 blockcipher MAC 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: MacTest.java,v 1.1 2003/12/28 19:06:57 hamgert Exp $
41  */

42 public class MacTest extends TestCase {
43     private StringBuffer JavaDoc mac1 = null;
44     private StringBuffer JavaDoc mac2 = 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 MAC
66      *
67      * @throws Exception
68      */

69     public void testMac() throws Exception JavaDoc {
70         for (int i = 0; i < RunTest.macalg.length; i++) {
71             for (int j = 0; j < RunTest.text.length; j++) {
72
73                 mac1 = Macs.generateMAC(RunTest.text[j], RunTest.TEMPFOLDER + RunTest.macalg[i][1], new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.macalg[i][0]);
74                 mac2 = Macs.generateMAC(RunTest.text[j], RunTest.TEMPFOLDER + RunTest.macalg[i][1], new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.macalg[i][0]);
75
76                 Assert.assertEquals(mac1.toString(), mac2.toString());
77             }
78         }
79     }
80
81     /**
82      * test file MAC
83      *
84      * @throws Exception
85      */

86     public void testFileMac() throws Exception JavaDoc {
87         for (int i = 0; i < RunTest.macalg.length; i++) {
88
89             mac1 = Macs.generateFileMAC(RunTest.TEMPFOLDER + "readable.txt", RunTest.TEMPFOLDER + RunTest.macalg[i][1], new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.macalg[i][0]);
90             mac2 = Macs.generateFileMAC(RunTest.TEMPFOLDER + "readable.txt", RunTest.TEMPFOLDER + RunTest.macalg[i][1], new StringBuffer JavaDoc("password"), RunTest.alg[i][0], RunTest.macalg[i][0]);
91
92             Assert.assertEquals(mac1.toString(), mac2.toString());
93
94         }
95     }
96 }
97
Popular Tags