Allow PasswordGenerator to use different alphabets

Per mcilwain's suggestion in the LRP design doc, LRP tokens should use a Base58 alphabet. I'll move PasswordGenerator out of the tools package and into a utils class in a future CL, as we'll want to use this generator in the LrpToken class itself rather than relegate the token definition to a tool.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132358363
This commit is contained in:
ctingue 2016-09-06 13:56:46 -07:00 committed by Ben McIlwain
parent dbb9ef80c5
commit daca7d65c2
14 changed files with 98 additions and 56 deletions

View file

@ -28,6 +28,7 @@ java_library(
"//third_party/java/joda_money",
"//third_party/java/joda_time",
"//third_party/java/json_simple",
"//third_party/java/jsr330_inject",
"//third_party/java/junit",
"//third_party/java/mockito",
"//third_party/java/objectify:objectify-v4_1",

View file

@ -29,7 +29,7 @@ public class CreateAnchorTenantCommandTest
@Before
public void initCommand() {
command.passwordGenerator = new FakePasswordGenerator("abcdefghijklmnopqrstuvwxyz");
command.passwordGenerator = new DeterministicStringGenerator("abcdefghijklmnopqrstuvwxyz");
}
@Test

View file

@ -23,7 +23,7 @@ public class CreateContactCommandTest extends EppToolCommandTestCase<CreateConta
@Before
public void initCommand() {
command.passwordGenerator = new FakePasswordGenerator("abcdefghijklmnopqrstuvwxyz");
command.passwordGenerator = new DeterministicStringGenerator("abcdefghijklmnopqrstuvwxyz");
}
@Test

View file

@ -23,7 +23,7 @@ public class CreateDomainCommandTest extends EppToolCommandTestCase<CreateDomain
@Before
public void initCommand() {
command.passwordGenerator = new FakePasswordGenerator("abcdefghijklmnopqrstuvwxyz");
command.passwordGenerator = new DeterministicStringGenerator("abcdefghijklmnopqrstuvwxyz");
}
@Test

View file

@ -15,21 +15,32 @@
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.Lists.charactersOf;
import com.google.common.collect.Iterators;
import java.util.Iterator;
/** A password generator that produces a password from a predefined string. */
class FakePasswordGenerator implements PasswordGenerator {
import javax.inject.Named;
/**
* A string generator that produces strings using sequential characters in its alphabet. This is
* most useful in tests as a "fake" password generator (which would otherwise use
* {@link RandomStringGenerator}.
*
* <p>Note that consecutive calls to createString will continue where the last call left off in
* the alphabet.
*/
class DeterministicStringGenerator extends StringGenerator {
private Iterator<Character> iterator;
/** Produces a password from the password source string. */
/**
* Generates a string using sequential characters in the generator's alphabet, cycling back to the
* beginning of the alphabet if necessary.
*/
@Override
public String createPassword(int length) {
checkArgument(length > 0, "Password length must be positive.");
public String createString(int length) {
checkArgument(length > 0, "String length must be positive.");
String password = "";
for (int i = 0; i < length; i++) {
password += iterator.next();
@ -37,8 +48,8 @@ class FakePasswordGenerator implements PasswordGenerator {
return password;
}
public FakePasswordGenerator(String passwordSource) {
checkArgument(!isNullOrEmpty(passwordSource), "Password source cannot be null or empty.");
iterator = Iterators.cycle(charactersOf(passwordSource));
public DeterministicStringGenerator(@Named("alphabet") String alphabet) {
super(alphabet);
iterator = Iterators.cycle(charactersOf(alphabet));
}
}

View file

@ -41,7 +41,8 @@ public class SetupOteCommandTest extends CommandTestCase<SetupOteCommand> {
ImmutableList<String> passwords = ImmutableList.of(
"abcdefghijklmnop", "qrstuvwxyzabcdef", "ghijklmnopqrstuv", "wxyzabcdefghijkl");
FakePasswordGenerator passwordGenerator = new FakePasswordGenerator("abcdefghijklmnopqrstuvwxyz");
DeterministicStringGenerator passwordGenerator =
new DeterministicStringGenerator("abcdefghijklmnopqrstuvwxyz");
@Before
public void init() {