KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sourceforge.lightcrypto.test;
2
3 import junit.framework.TestCase;
4 import junit.framework.Assert;
5
6 import java.io.*;
7
8 import net.sourceforge.lightcrypto.Key;
9 import net.sourceforge.lightcrypto.SafeObject;
10 import net.sourceforge.lightcrypto.Macs;
11
12 /**
13  * A collection of HMAC tests
14  * <P>
15  * These tests can be run using JUnit (http://www.junit.org)
16  *
17  * @author Gert Van Ham
18  * @author hamgert@users.sourceforge.net
19  * @author http://jcetaglib.sourceforge.net
20  * @version $Id: MacTest.java,v 1.1 2003/10/05 11:41:29 hamgert Exp $
21  */

22
23 public class MacTest extends TestCase {
24     private StringBuffer JavaDoc text1;
25     private StringBuffer JavaDoc text2;
26     private StringBuffer JavaDoc text3;
27     private StringBuffer JavaDoc text4;
28     private StringBuffer JavaDoc text5;
29     private StringBuffer JavaDoc text6;
30     private StringBuffer JavaDoc text7;
31     private StringBuffer JavaDoc text8;
32     private StringBuffer JavaDoc text9;
33     private StringBuffer JavaDoc text10;
34
35     StringBuffer JavaDoc mac = null;
36     StringBuffer JavaDoc plaintext = null;
37
38     /**
39      * setup test
40      *
41      * @throws java.io.IOException
42      */

43     protected void setUp() throws IOException {
44         text1 = new StringBuffer JavaDoc("The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms");
45         text2 = new StringBuffer JavaDoc("This software is distributed under a license based on the MIT X Consortium license");
46         text3 = new StringBuffer JavaDoc("found in $JAVA_HOME/jre/lib/security/java.security, where $JAVA_HOME is the location of your JDK/JRE distribution");
47         text4 = new StringBuffer JavaDoc("Mit Project 2002 zum erfolgreichen Projektmanagement Damit Sie in Zukunft Ihre Projekte präzise und komfortabel steuern können");
48         text5 = new StringBuffer JavaDoc("En av de största nyheterna är att det finns en .NET Enterprise Server-lösning för stora företagsomspännade projekt");
49         text6 = new StringBuffer JavaDoc("Lees de productinformatie en ontdek alles over de krachtige tools binnen Visual Studio .NET");
50         text7 = new StringBuffer JavaDoc("Vergeet even die oude tovenaars met puntige hoeden en rondborstige jonkvrouwen in nood... oké, vergeet in ieder geval even die tovenaars, want Lionheart komt met een ambitieuze rollenspelvariant");
51         text8 = new StringBuffer JavaDoc("An implementation of ECIES (stream mode) as described in IEEE P 1363a.");
52         text9 = new StringBuffer JavaDoc("This makes the entire keystore resistant to tampering and inspection, and forces verification");
53         text10 = new StringBuffer JavaDoc("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
54
55         // create text file
56
FileOutputStream outStr = new FileOutputStream(RunTest.TEMPFOLDER + "readable.txt");
57         DataOutputStream dataStr = new DataOutputStream(outStr);
58
59         dataStr.writeBytes("This is a readable string inside a file");
60
61         dataStr.flush();
62         dataStr.close();
63
64         outStr.close();
65     }
66
67     /**
68      * test MAC
69      *
70      * @throws Exception
71      */

72     public void testMac() throws Exception JavaDoc {
73         // generate a key
74
Key.generatekey(RunTest.TEMPFOLDER + "tempkey.key", new StringBuffer JavaDoc("password"));
75
76         SafeObject k = new SafeObject();
77         k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key", new StringBuffer JavaDoc("password"));
78
79         mac = Macs.mac(text1, k);
80         Assert.assertTrue(Macs.macEquals(text1, mac, k));
81
82         mac = Macs.mac(text2, k);
83         Assert.assertTrue(Macs.macEquals(text2, mac, k));
84
85         mac = Macs.mac(text3, k);
86         Assert.assertTrue(Macs.macEquals(text3, mac, k));
87
88         mac = Macs.mac(text4, k);
89         Assert.assertTrue(Macs.macEquals(text4, mac, k));
90
91         mac = Macs.mac(text5, k);
92         Assert.assertTrue(Macs.macEquals(text5, mac, k));
93
94         mac = Macs.mac(text6, k);
95         Assert.assertTrue(Macs.macEquals(text6, mac, k));
96
97         mac = Macs.mac(text7, k);
98         Assert.assertTrue(Macs.macEquals(text7, mac, k));
99
100         mac = Macs.mac(text8, k);
101         Assert.assertTrue(Macs.macEquals(text8, mac, k));
102
103         mac = Macs.mac(text9, k);
104         Assert.assertTrue(Macs.macEquals(text9, mac, k));
105
106         mac = Macs.mac(text10, k);
107         Assert.assertTrue(Macs.macEquals(text10, mac, k));
108
109         mac = Macs.mac(text10, k);
110         Assert.assertFalse(Macs.macEquals(text9, mac, k));
111     }
112
113     /**
114     * test file encryption
115     *
116     * @throws Exception
117     */

118    public void testFileEncryption() throws Exception JavaDoc {
119         SafeObject k = new SafeObject();
120         k = Key.loadkey(RunTest.TEMPFOLDER + "tempkey.key",new StringBuffer JavaDoc("password"));
121
122         mac = Macs.macFromFile(RunTest.TEMPFOLDER + "readable.txt",k);
123         Assert.assertTrue(Macs.macEqualsFile(RunTest.TEMPFOLDER + "readable.txt",mac,k));
124
125     }
126 }
127
Popular Tags