8308803: Improve java/util/UUID/UUIDTest.java

Reviewed-by: jpai, rriggs
This commit is contained in:
Aleksey Shipilev 2023-06-01 08:55:56 +00:00
parent dfd3da3f52
commit 4460429d7a

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,22 +22,30 @@
*/ */
/* @test /* @test
* @bug 4173528 5068772 8148936 8196334 * @bug 4173528 5068772 8148936 8196334 8308803
* @summary Unit tests for java.util.UUID * @summary Unit tests for java.util.UUID
* @key randomness * @key randomness
* @run main/othervm -XX:+CompactStrings UUIDTest * @library /test/lib
* @run main/othervm -XX:-CompactStrings UUIDTest * @build jdk.test.lib.RandomFactory
* @run main/othervm -Xmx1g -XX:+CompactStrings UUIDTest
* @run main/othervm -Xmx1g -XX:-CompactStrings UUIDTest
*/ */
import java.util.*; import java.util.*;
import java.util.stream.IntStream;
import jdk.test.lib.RandomFactory;
public class UUIDTest { public class UUIDTest {
static Random generator = new Random(); // Single UUID instance is ~32 bytes, 1M instances take ~256M in the set
private static final int COUNT = 1_000_000;
static final Random generator = RandomFactory.getRandom();
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
containsTest(); negativeTest();
randomUUIDTest(); randomUUIDTest();
randomUUIDTest_Multi();
nameUUIDFromBytesTest(); nameUUIDFromBytesTest();
stringTest(); stringTest();
versionTest(); versionTest();
@ -49,56 +57,103 @@ public class UUIDTest {
compareTo(); compareTo();
} }
// Verify that list.contains detects UUID collisons private static void negativeTest() throws Exception {
private static void containsTest() throws Exception { Set<UUID> set = new HashSet<>();
List list = new LinkedList(); set.add(new UUID(4, 4));
list.add(new UUID(4,4)); if (set.add(new UUID(4, 4))) {
if (!list.contains(new UUID(4,4))) throw new Exception("Contains test does not work as expected");
throw new Exception("contains test did not work as expected"); }
} }
private static void randomUUIDTest() throws Exception { private static void randomUUIDTest() throws Exception {
List list = new LinkedList(); List<UUID> collisions = new ArrayList<>();
for (int i=0; i<100; i++) {
UUID u1 = UUID.randomUUID(); Set<UUID> set = new HashSet<>();
if (4 != u1.version()) { for (int i = 0; i < COUNT; i++) {
throw new Exception("bad version"); UUID u = UUID.randomUUID();
if (u.version() != 4) {
throw new Exception("Bad version: " + u);
} }
if (2 != u1.variant()) { if (u.variant() != 2) {
throw new Exception("bad variant"); throw new Exception("Bad variant: " + u);
} }
if (list.contains(u1)) if (!set.add(u)) {
throw new Exception("random UUID collision very unlikely"); collisions.add(u);
list.add(u1);
} }
} }
if (!collisions.isEmpty()) {
// This is extremely unlikely to happen. If you see this failure,
// this highly likely points to the implementation bug, rather than
// the odd chance.
throw new Exception("UUID collisions detected: " + collisions);
}
}
private static void randomUUIDTest_Multi() throws Exception {
List<UUID> uuids = IntStream.range(0, COUNT).parallel()
.mapToObj(i -> UUID.randomUUID())
.toList();
List<UUID> collisions = new ArrayList<>();
Set<UUID> set = new HashSet<>();
for (UUID u : uuids) {
if (u.version() != 4) {
throw new Exception("Bad version: " + u);
}
if (u.variant() != 2) {
throw new Exception("Bad variant: " + u);
}
if (!set.add(u)) {
collisions.add(u);
}
}
if (!collisions.isEmpty()) {
// This is extremely unlikely to happen. If you see this failure,
// this highly likely points to the implementation bug, rather than
// the odd chance.
throw new Exception("UUID collisions detected: " + collisions);
}
}
private static void nameUUIDFromBytesTest() throws Exception { private static void nameUUIDFromBytesTest() throws Exception {
Random byteSource = new Random(); List<UUID> collisions = new ArrayList<>();
byte[] someBytes = new byte[12]; byte[] someBytes = new byte[12];
List list = new LinkedList(); Set<UUID> set = new HashSet<>();
for (int i=0; i<100; i++) { for (int i = 0; i < COUNT; i++) {
byteSource.nextBytes(someBytes); generator.nextBytes(someBytes);
UUID u1 = UUID.nameUUIDFromBytes(someBytes); UUID u = UUID.nameUUIDFromBytes(someBytes);
if (3 != u1.version()) { if (u.version() != 3) {
throw new Exception("bad version"); throw new Exception("Bad version: " + u);
} }
if (2 != u1.variant()) { if (u.variant() != 2) {
throw new Exception("bad variant"); throw new Exception("Bad variant: " + u);
} }
if (list.contains(u1)) if (!set.add(u)) {
throw new Exception("byte UUID collision very unlikely"); collisions.add(u);
list.add(u1); }
}
if (!collisions.isEmpty()) {
// This is extremely unlikely to happen. If you see this failure,
// this highly likely points to the implementation bug, rather than
// the odd chance.
throw new Exception("UUID collisions detected: " + collisions);
} }
} }
private static void stringTest() throws Exception { private static void stringTest() throws Exception {
for (int i=0; i<100; i++) { for (int i = 0; i < COUNT; i++) {
UUID u1 = UUID.randomUUID(); UUID u1 = UUID.randomUUID();
UUID u2 = UUID.fromString(u1.toString().toLowerCase()); UUID u2 = UUID.fromString(u1.toString().toLowerCase());
UUID u3 = UUID.fromString(u1.toString().toUpperCase()); UUID u3 = UUID.fromString(u1.toString().toUpperCase());
if (!u1.equals(u2) || !u1.equals(u3)) if (!u1.equals(u2) || !u1.equals(u3)) {
throw new Exception("UUID -> string -> UUID failed"); throw new Exception("UUID -> string -> UUID failed: " + u1 + " -> " + u2 + " -> " + u3);
}
} }
testFromStringError("-0"); testFromStringError("-0");
@ -121,63 +176,91 @@ public class UUIDTest {
private static void versionTest() throws Exception { private static void versionTest() throws Exception {
UUID test = UUID.randomUUID(); UUID test = UUID.randomUUID();
if (test.version() != 4) if (test.version() != 4) {
throw new Exception("randomUUID not type 4"); throw new Exception("randomUUID not type 4: " + test);
Random byteSource = new Random(); }
byte[] someBytes = new byte[12]; byte[] someBytes = new byte[12];
byteSource.nextBytes(someBytes); generator.nextBytes(someBytes);
test = UUID.nameUUIDFromBytes(someBytes); test = UUID.nameUUIDFromBytes(someBytes);
if (test.version() != 3) if (test.version() != 3) {
throw new Exception("nameUUIDFromBytes not type 3"); throw new Exception("nameUUIDFromBytes not type 3: " + test);
}
test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda"); test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda");
if (test.version() != 1) if (test.version() != 1) {
throw new Exception("wrong version fromString 1"); throw new Exception("wrong version fromString 1");
}
test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda"); test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda");
if (test.version() != 2) if (test.version() != 2) {
throw new Exception("wrong version fromString 2"); throw new Exception("wrong version fromString 2");
}
test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda"); test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda");
if (test.version() != 3) if (test.version() != 3) {
throw new Exception("wrong version fromString 3"); throw new Exception("wrong version fromString 3");
}
test = UUID.fromString("9835451d-e2e0-4e41-8a5a-be785f17dcda"); test = UUID.fromString("9835451d-e2e0-4e41-8a5a-be785f17dcda");
if (test.version() != 4) if (test.version() != 4) {
throw new Exception("wrong version fromString 4"); throw new Exception("wrong version fromString 4");
}
test = new UUID(0x0000000000001000L, 55L); test = new UUID(0x0000000000001000L, 55L);
if (test.version() != 1) if (test.version() != 1) {
throw new Exception("wrong version from bit set to 1"); throw new Exception("wrong version from bit set to 1");
}
test = new UUID(0x0000000000002000L, 55L); test = new UUID(0x0000000000002000L, 55L);
if (test.version() != 2) if (test.version() != 2) {
throw new Exception("wrong version from bit set to 2"); throw new Exception("wrong version from bit set to 2");
}
test = new UUID(0x0000000000003000L, 55L); test = new UUID(0x0000000000003000L, 55L);
if (test.version() != 3) if (test.version() != 3) {
throw new Exception("wrong version from bit set to 3"); throw new Exception("wrong version from bit set to 3");
}
test = new UUID(0x0000000000004000L, 55L); test = new UUID(0x0000000000004000L, 55L);
if (test.version() != 4) if (test.version() != 4) {
throw new Exception("wrong version from bit set to 4"); throw new Exception("wrong version from bit set to 4");
} }
}
private static void variantTest() throws Exception { private static void variantTest() throws Exception {
UUID test = UUID.randomUUID(); UUID test = UUID.randomUUID();
if (test.variant() != 2) if (test.variant() != 2) {
throw new Exception("randomUUID not variant 2"); throw new Exception("randomUUID not variant 2");
Random byteSource = new Random(); }
byte[] someBytes = new byte[12]; byte[] someBytes = new byte[12];
byteSource.nextBytes(someBytes); generator.nextBytes(someBytes);
test = UUID.nameUUIDFromBytes(someBytes); test = UUID.nameUUIDFromBytes(someBytes);
if (test.variant() != 2) if (test.variant() != 2) {
throw new Exception("nameUUIDFromBytes not variant 2"); throw new Exception("nameUUIDFromBytes not variant 2");
}
test = new UUID(55L, 0x0000000000001000L); test = new UUID(55L, 0x0000000000001000L);
if (test.variant() != 0) if (test.variant() != 0) {
throw new Exception("wrong variant from bit set to 0"); throw new Exception("wrong variant from bit set to 0");
}
test = new UUID(55L, 0x8000000000001000L); test = new UUID(55L, 0x8000000000001000L);
if (test.variant() != 2) if (test.variant() != 2) {
throw new Exception("wrong variant from bit set to 2"); throw new Exception("wrong variant from bit set to 2");
}
test = new UUID(55L, 0xc000000000001000L); test = new UUID(55L, 0xc000000000001000L);
if (test.variant() != 6) if (test.variant() != 6) {
throw new Exception("wrong variant from bit set to 6"); throw new Exception("wrong variant from bit set to 6");
}
test = new UUID(55L, 0xe000000000001000L); test = new UUID(55L, 0xe000000000001000L);
if (test.variant() != 7) if (test.variant() != 7) {
throw new Exception("wrong variant from bit set to 7"); throw new Exception("wrong variant from bit set to 7");
} }
}
private static void timestampTest() throws Exception { private static void timestampTest() throws Exception {
UUID test = UUID.randomUUID(); UUID test = UUID.randomUUID();
@ -187,16 +270,22 @@ public class UUIDTest {
} catch (UnsupportedOperationException uoe) { } catch (UnsupportedOperationException uoe) {
// Correct result // Correct result
} }
test = UUID.fromString("00000001-0000-1000-8a5a-be785f17dcda"); test = UUID.fromString("00000001-0000-1000-8a5a-be785f17dcda");
if (test.timestamp() != 1) if (test.timestamp() != 1) {
throw new Exception("Incorrect timestamp"); throw new Exception("Incorrect timestamp");
}
test = UUID.fromString("00000400-0000-1000-8a5a-be785f17dcda"); test = UUID.fromString("00000400-0000-1000-8a5a-be785f17dcda");
if (test.timestamp() != 1024) if (test.timestamp() != 1024) {
throw new Exception("Incorrect timestamp"); throw new Exception("Incorrect timestamp");
}
test = UUID.fromString("FFFFFFFF-FFFF-1FFF-8a5a-be785f17dcda"); test = UUID.fromString("FFFFFFFF-FFFF-1FFF-8a5a-be785f17dcda");
if (test.timestamp() != Long.MAX_VALUE>>3) if (test.timestamp() != (Long.MAX_VALUE >> 3)) {
throw new Exception("Incorrect timestamp"); throw new Exception("Incorrect timestamp");
} }
}
private static void clockSequenceTest() throws Exception { private static void clockSequenceTest() throws Exception {
UUID test = UUID.randomUUID(); UUID test = UUID.randomUUID();
@ -206,19 +295,27 @@ public class UUIDTest {
} catch (UnsupportedOperationException uoe) { } catch (UnsupportedOperationException uoe) {
// Correct result // Correct result
} }
test = UUID.fromString("00000001-0000-1000-8001-be785f17dcda"); test = UUID.fromString("00000001-0000-1000-8001-be785f17dcda");
if (test.clockSequence() != 1) if (test.clockSequence() != 1) {
throw new Exception("Incorrect sequence"); throw new Exception("Incorrect sequence");
}
test = UUID.fromString("00000001-0000-1000-8002-be785f17dcda"); test = UUID.fromString("00000001-0000-1000-8002-be785f17dcda");
if (test.clockSequence() != 2) if (test.clockSequence() != 2) {
throw new Exception("Incorrect sequence"); throw new Exception("Incorrect sequence");
}
test = UUID.fromString("00000001-0000-1000-8010-be785f17dcda"); test = UUID.fromString("00000001-0000-1000-8010-be785f17dcda");
if (test.clockSequence() != 16) if (test.clockSequence() != 16) {
throw new Exception("Incorrect sequence"); throw new Exception("Incorrect sequence");
}
test = UUID.fromString("00000001-0000-1000-bFFF-be785f17dcda"); test = UUID.fromString("00000001-0000-1000-bFFF-be785f17dcda");
if (test.clockSequence() != ((2L<<13)-1)) // 2^14 - 1 if (test.clockSequence() != ((1L << 14) - 1)) {
throw new Exception("Incorrect sequence"); throw new Exception("Incorrect sequence");
} }
}
private static void nodeTest() throws Exception { private static void nodeTest() throws Exception {
UUID test = UUID.randomUUID(); UUID test = UUID.randomUUID();
@ -228,32 +325,40 @@ public class UUIDTest {
} catch (UnsupportedOperationException uoe) { } catch (UnsupportedOperationException uoe) {
// Correct result // Correct result
} }
test = UUID.fromString("00000001-0000-1000-8001-000000000001"); test = UUID.fromString("00000001-0000-1000-8001-000000000001");
if (test.node() != 1) if (test.node() != 1) {
throw new Exception("Incorrect node"); throw new Exception("Incorrect node");
}
test = UUID.fromString("00000001-0000-1000-8002-FFFFFFFFFFFF"); test = UUID.fromString("00000001-0000-1000-8002-FFFFFFFFFFFF");
if (test.node() != ((2L<<47)-1)) // 2^48 - 1 if (test.node() != ((1L << 48) - 1)) {
throw new Exception("Incorrect node"); throw new Exception("Incorrect node");
} }
}
private static void hashCodeEqualsTest() throws Exception { private static void hashCodeEqualsTest() throws Exception {
// If two UUIDs are equal they must have the same hashCode // If two UUIDs are equal they must have the same hashCode
for (int i=0; i<100; i++) { for (int i = 0; i < COUNT; i++) {
UUID u1 = UUID.randomUUID(); UUID u1 = UUID.randomUUID();
UUID u2 = UUID.fromString(u1.toString()); UUID u2 = UUID.fromString(u1.toString());
if (u1.hashCode() != u2.hashCode()) if (u1.hashCode() != u2.hashCode()) {
throw new Exception("Equal UUIDs with different hashcodes"); throw new Exception("Equal UUIDs with different hash codes: " + u1 + "(" + u1.hashCode() + ") " +
"and " + u2 + "(" + u2.hashCode() + ")");
} }
}
// Test equality of UUIDs with tampered bits // Test equality of UUIDs with tampered bits
for (int i=0; i<1000; i++) { for (int i = 0; i < COUNT; i++) {
long l = generator.nextLong(); long l = generator.nextLong();
long l2 = generator.nextLong(); long l2 = generator.nextLong();
int position = generator.nextInt(64); int position = generator.nextInt(64);
UUID u1 = new UUID(l, l2); UUID u1 = new UUID(l, l2);
l = l ^ (1L << position); l = l ^ (1L << position);
UUID u2 = new UUID(l, l2); UUID u2 = new UUID(l, l2);
if (u1.equals(u2)) if (u1.equals(u2)) {
throw new Exception("UUIDs with different bits equal"); throw new Exception("UUIDs with different bits equal: " + u1 + " and " + u2);
}
} }
} }
@ -267,18 +372,20 @@ public class UUIDTest {
if ((id.compareTo(id2) >= 0) || if ((id.compareTo(id2) >= 0) ||
(id2.compareTo(id3) >= 0) || (id2.compareTo(id3) >= 0) ||
(id3.compareTo(id4) >= 0) || (id3.compareTo(id4) >= 0) ||
(id4.compareTo(id5) >= 0)) (id4.compareTo(id5) >= 0)) {
throw new RuntimeException("compareTo failure"); throw new RuntimeException("compareTo failure");
}
if ((id5.compareTo(id4) <= 0) || if ((id5.compareTo(id4) <= 0) ||
(id4.compareTo(id3) <= 0) || (id4.compareTo(id3) <= 0) ||
(id3.compareTo(id2) <= 0) || (id3.compareTo(id2) <= 0) ||
(id2.compareTo(id) <= 0)) (id2.compareTo(id) <= 0)) {
throw new RuntimeException("compareTo failure"); throw new RuntimeException("compareTo failure");
}
if (id.compareTo(id) != 0) if (id.compareTo(id) != 0) {
throw new RuntimeException("compareTo failure"); throw new RuntimeException("compareTo failure");
}
} }
} }