01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.authentication;
04:
05: import junit.framework.TestCase;
06: import java.util.Random;
07:
08: public class HashingCipherTest extends TestCase {
09: private String[] inputs = new String[] { "123", "abc",
10: "12345678901234567890", "this is a test", "!@#$%^&*()" };
11:
12: private HashingCipher crypter = new HashingCipher();
13:
14: public void setUp() throws Exception {
15: }
16:
17: public void tearDown() throws Exception {
18: }
19:
20: public void testHashReturnsDifferentValueThanPassed()
21: throws Exception {
22: String testString = "This is a test string";
23: String hash = crypter.encrypt(testString);
24: assertNotNull(hash);
25: assertFalse(hash.equals(testString));
26: }
27:
28: public void testDifferentStringHashDifferently() throws Exception {
29: String hash1 = crypter.encrypt("123456");
30: String hash2 = crypter.encrypt("abcdef");
31: assertFalse(hash1.equals(hash2));
32: }
33:
34: public void testLengthOfHash() throws Exception {
35: for (int i = 0; i < inputs.length; i++) {
36: String input = inputs[i];
37: String encryption = crypter.encrypt(input);
38: assertEquals(input, 20, encryption.length());
39: }
40: }
41:
42: public void testSameInputGivesSameOutput() throws Exception {
43: for (int i = 0; i < inputs.length; i++) {
44: String input = inputs[i];
45: String encryption1 = crypter.encrypt(input);
46: String encryption2 = crypter.encrypt(input);
47: assertEquals(input, encryption1, encryption2);
48: }
49: }
50:
51: public void testAlgorithmSpeed() throws Exception {
52: Random generator = new Random();
53: int sampleSize = 1000;
54: String[] inputs = new String[sampleSize];
55:
56: for (int i = 0; i < inputs.length; i++) {
57: int passwordSize = generator.nextInt(20) + 1;
58: byte[] passwd = new byte[passwordSize];
59: generator.nextBytes(passwd);
60: inputs[i] = new String(passwd);
61: }
62:
63: long startTime = System.currentTimeMillis();
64: for (int i = 0; i < inputs.length; i++) {
65: crypter.encrypt(inputs[i]);
66: }
67: long duration = System.currentTimeMillis() - startTime;
68:
69: assertTrue(duration < 1000);
70: }
71: }
|