001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestPasswordGenerator.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import java.util.Random;
011: import java.util.regex.Pattern;
012: import junit.framework.TestCase;
013:
014: public class TestPasswordGenerator extends TestCase {
015: public TestPasswordGenerator(String name) {
016: super (name);
017: }
018:
019: public void testIllegalArguments() {
020: try {
021: PasswordGenerator.get(0);
022: fail();
023: } catch (IllegalArgumentException e) {
024: assertTrue(true);
025: }
026:
027: try {
028: PasswordGenerator.get(1, 3);
029: fail();
030: } catch (IllegalArgumentException e) {
031: assertTrue(true);
032: }
033: }
034:
035: public void testLength() {
036: String password = null;
037:
038: password = PasswordGenerator.get(10);
039: assertEquals(10, password.length());
040: password = PasswordGenerator.get(20);
041: assertEquals(20, password.length());
042: password = PasswordGenerator.get(30);
043: assertEquals(30, password.length());
044: }
045:
046: public void testDefaultMixed() {
047: String password = null;
048:
049: Pattern mixed_pattern = null;
050:
051: password = PasswordGenerator.get(10);
052: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{10}$");
053: assertTrue(mixed_pattern.matcher(password).matches());
054: password = PasswordGenerator.get(98);
055: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{98}$");
056: assertTrue(mixed_pattern.matcher(password).matches());
057: password = PasswordGenerator.get(12);
058: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{12}$");
059: assertTrue(mixed_pattern.matcher(password).matches());
060: password = PasswordGenerator.get(32);
061: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{32}$");
062: assertTrue(mixed_pattern.matcher(password).matches());
063: password = PasswordGenerator.get(75);
064: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{75}$");
065: assertTrue(mixed_pattern.matcher(password).matches());
066: }
067:
068: public void testMixed() {
069: String password = null;
070:
071: Pattern mixed_pattern = null;
072:
073: password = PasswordGenerator.get(10, PasswordGenerator.MIXED);
074: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{10}$");
075: assertTrue(mixed_pattern.matcher(password).matches());
076: password = PasswordGenerator.get(98, PasswordGenerator.MIXED);
077: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{98}$");
078: assertTrue(mixed_pattern.matcher(password).matches());
079: password = PasswordGenerator.get(12, PasswordGenerator.MIXED);
080: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{12}$");
081: assertTrue(mixed_pattern.matcher(password).matches());
082: password = PasswordGenerator.get(32, PasswordGenerator.MIXED);
083: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{32}$");
084: assertTrue(mixed_pattern.matcher(password).matches());
085: password = PasswordGenerator.get(75, PasswordGenerator.MIXED);
086: mixed_pattern = Pattern.compile("^[a-zA-Z0-9]{75}$");
087: assertTrue(mixed_pattern.matcher(password).matches());
088: }
089:
090: public void testDigitsOnly() {
091: String password = null;
092:
093: Pattern mixed_pattern = null;
094:
095: password = PasswordGenerator.get(10,
096: PasswordGenerator.DIGITS_ONLY);
097: mixed_pattern = Pattern.compile("^[0-9]{10}$");
098: assertTrue(mixed_pattern.matcher(password).matches());
099: password = PasswordGenerator.get(98,
100: PasswordGenerator.DIGITS_ONLY);
101: mixed_pattern = Pattern.compile("^[0-9]{98}$");
102: assertTrue(mixed_pattern.matcher(password).matches());
103: password = PasswordGenerator.get(12,
104: PasswordGenerator.DIGITS_ONLY);
105: mixed_pattern = Pattern.compile("^[0-9]{12}$");
106: assertTrue(mixed_pattern.matcher(password).matches());
107: password = PasswordGenerator.get(32,
108: PasswordGenerator.DIGITS_ONLY);
109: mixed_pattern = Pattern.compile("^[0-9]{32}$");
110: assertTrue(mixed_pattern.matcher(password).matches());
111: password = PasswordGenerator.get(75,
112: PasswordGenerator.DIGITS_ONLY);
113: mixed_pattern = Pattern.compile("^[0-9]{75}$");
114: assertTrue(mixed_pattern.matcher(password).matches());
115: }
116:
117: public void testLettersOnly() {
118: String password = null;
119:
120: Pattern mixed_pattern = null;
121:
122: password = PasswordGenerator.get(10,
123: PasswordGenerator.LETTERS_ONLY);
124: mixed_pattern = Pattern.compile("^[a-zA-Z]{10}$");
125: assertTrue(mixed_pattern.matcher(password).matches());
126: password = PasswordGenerator.get(98,
127: PasswordGenerator.LETTERS_ONLY);
128: mixed_pattern = Pattern.compile("^[a-zA-Z]{98}$");
129: assertTrue(mixed_pattern.matcher(password).matches());
130: password = PasswordGenerator.get(12,
131: PasswordGenerator.LETTERS_ONLY);
132: mixed_pattern = Pattern.compile("^[a-zA-Z]{12}$");
133: assertTrue(mixed_pattern.matcher(password).matches());
134: password = PasswordGenerator.get(32,
135: PasswordGenerator.LETTERS_ONLY);
136: mixed_pattern = Pattern.compile("^[a-zA-Z]{32}$");
137: assertTrue(mixed_pattern.matcher(password).matches());
138: password = PasswordGenerator.get(75,
139: PasswordGenerator.LETTERS_ONLY);
140: mixed_pattern = Pattern.compile("^[a-zA-Z]{75}$");
141: assertTrue(mixed_pattern.matcher(password).matches());
142: }
143:
144: public void testRandom() {
145: Random random1 = new Random(System.currentTimeMillis());
146: Random random2 = new Random("the seed".hashCode());
147:
148: assertTrue(!PasswordGenerator.get(random1, 10,
149: PasswordGenerator.MIXED).equals(
150: PasswordGenerator.get(random2, 10,
151: PasswordGenerator.MIXED)));
152:
153: assertTrue(!PasswordGenerator.get(random1, 10,
154: PasswordGenerator.MIXED).equals(
155: PasswordGenerator.get(random2, 10,
156: PasswordGenerator.MIXED)));
157:
158: assertTrue(!PasswordGenerator.get(random1, 10,
159: PasswordGenerator.MIXED).equals(
160: PasswordGenerator.get(random2, 10,
161: PasswordGenerator.MIXED)));
162:
163: assertTrue(!PasswordGenerator.get(random1, 10,
164: PasswordGenerator.MIXED).equals(
165: PasswordGenerator.get(random2, 10,
166: PasswordGenerator.MIXED)));
167:
168: assertTrue(!PasswordGenerator.get(random1, 10,
169: PasswordGenerator.MIXED).equals(
170: PasswordGenerator.get(random2, 10,
171: PasswordGenerator.MIXED)));
172: }
173: }
|