From: Benny Baumann Date: Sun, 31 Jul 2016 16:20:10 +0000 (+0200) Subject: fix: Actually generate random IDs for use in tests X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=0931c8e7e936837334372fa3c77d44e65b3e8df7;hp=d0ee991d9ba982e43acd036c2d0592976ba9e9ff fix: Actually generate random IDs for use in tests Collission probability: 1 in 2**40 (if Random numbers are actually random) Having the createUniqueName method synchronized might be slower, but ensures that count is always incremented for every name generated; even if the PRNG unexpectedly fails. We're not using SecureRandom here though, due to issues with the Unit Tests otherwise hanging without any apparent reason. Also only 16 characters per generated ID to avoid database field limits. Change-Id: I52668471aa82e44981a3410d3f271f1a261c5dc6 --- diff --git a/tests/org/cacert/gigi/testUtils/ConfiguredTest.java b/tests/org/cacert/gigi/testUtils/ConfiguredTest.java index 1a8ce692..892dfefe 100644 --- a/tests/org/cacert/gigi/testUtils/ConfiguredTest.java +++ b/tests/org/cacert/gigi/testUtils/ConfiguredTest.java @@ -17,6 +17,7 @@ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; +import java.util.Random; import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -165,8 +166,23 @@ public abstract class ConfiguredTest { private static Link l; - public static String createUniqueName() { - return "test" + System.currentTimeMillis() + "a" + (count++) + "u"; + public static String createRandomIDString() { + final char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray(); + final int idStringLength = 16; + + Random sr; + sr = new Random(); + + StringBuilder sb = new StringBuilder(idStringLength); + for (int i = 0; i < idStringLength; i++) { + sb.append(chars[sr.nextInt(chars.length)]); + } + + return sb.toString(); + } + + public static synchronized String createUniqueName() { + return "test" + createRandomIDString() + "a" + (count++) + "u"; } public static int countRegex(String text, String pattern) {