]> WPIA git - gigi.git/commitdiff
fix: Actually generate random IDs for use in tests
authorBenny Baumann <BenBE1987@gmx.net>
Sun, 31 Jul 2016 16:20:10 +0000 (18:20 +0200)
committerBenny Baumann <BenBE1987@gmx.net>
Mon, 1 Aug 2016 23:36:08 +0000 (01:36 +0200)
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

tests/org/cacert/gigi/testUtils/ConfiguredTest.java

index 1a8ce6929b91387a5d1c37ed87605d224f2c0c5c..892dfefee9f48bdf45de91be5fa01fadb6aeed53 100644 (file)
@@ -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) {