KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > crypto > generators > DigestRandomTest


1 /*
2  * DigestRandomTest.java
3  * JUnit based test
4  *
5  * Created on 12. Oktober 2005, 10:29
6  */

7 /*
8  * Copyright 2005 Schlichtherle IT Services
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package de.schlichtherle.crypto.generators;
24
25 import de.schlichtherle.util.Arrays;
26 import de.schlichtherle.util.zip.ZipTestBase;
27 import java.util.logging.Logger JavaDoc;
28
29 import junit.framework.*;
30
31 import java.security.SecureRandom JavaDoc;
32 import java.util.Random JavaDoc;
33
34 import org.bouncycastle.crypto.Digest;
35 import org.bouncycastle.crypto.digests.SHA256Digest;
36
37 /**
38  *
39  * @author Christian Schlichtherle
40  */

41 public class DigestRandomTest extends TestCase {
42
43     private static final Logger JavaDoc logger
44             = Logger.getLogger(DigestRandomTest.class.getName());
45
46     private Random JavaDoc rnd1;
47     private Random JavaDoc rnd2;
48     
49     public static Test suite() {
50         TestSuite suite = new TestSuite(DigestRandomTest.class);
51         
52         return suite;
53     }
54     
55     public DigestRandomTest(String JavaDoc testName) {
56         super(testName);
57     }
58
59     protected void setUp() throws Exception JavaDoc {
60         rnd1 = new DigestRandom(new SHA256Digest());
61         rnd2 = new DigestRandom(new SHA256Digest());
62         //rnd1 = new SecureRandom();
63
//rnd2 = new SecureRandom();
64
}
65
66     protected void tearDown() throws Exception JavaDoc {
67         rnd1 = null;
68         rnd2 = null;
69     }
70
71     public void testNextBytes() {
72         logger.fine("testNextBytes");
73
74         final int n = 1024 * 1024;
75         final byte[] buf1 = new byte[n];
76         final byte[] buf2 = new byte[n];
77         for (int i = 32; --i >= 0; ) {
78             rnd1.nextBytes(buf1);
79             rnd2.nextBytes(buf2);
80             assertFalse(Arrays.equals(buf1, 0, buf2, 0, n));
81         }
82     }
83
84     public void testNext() {
85         logger.fine("testNext");
86
87         for (int i = 1024; --i >= 0; )
88             assertTrue(rnd1.nextInt() != rnd2.nextInt());
89     }
90 }
91
Popular Tags